using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;

namespace Cosmos.Win
{
    public partial class frmPosConfigurationSecu : Form
    {
        #region 변수 선언
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보 참조

        private string m_sPasswordMode = string.Empty;
        public string PasswordMode { set { m_sPasswordMode = value; } get { return m_sPasswordMode; } }
        #endregion

        #region 생성자
        public frmPosConfigurationSecu()
        {
            InitializeComponent();

            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            //this.UpdateStyles();

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보
        }
        
        private void frmPosConfigurationSecu_Load(object sender, EventArgs e)
        {
            InitControl();
        }
        #endregion

        #region 폼 컨트롤 초기화
        private void InitControl()
        {
            try
            {
                LoadIniInfo();

                // 이미지 로딩 처리
                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_S);
                this.Size = new Size(788, 402);

                FormManager.SetFormAllControlFont(this, CmUtil.IsNull(m_cPosStatus.Base.FONT, "맑은 고딕"));

                btnExit.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BTN_CLOSE);
                if (btnExit.Image != null) btnExit.Text = "";

                //테마색상 적용!
                if (m_cPosStatus.ScnMst.ThemeColor == "")
                {
                    btnEnter.BackColor = Color.FromArgb(22, 187, 238);
                    padInPut.ThemeColor = "022187238";
                }
                else
                {
                    btnEnter.BackColor = CmUtil.GetColorToString(m_cPosStatus.ScnMst.ThemeColor);
                    padInPut.ThemeColor = m_cPosStatus.ScnMst.ThemeColor;
                }                
                //padInPut.ClearBtnText = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0817);
                padInPut.ClearBtnText = "지움";

                // 타이틀 설정
                //lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0317);

                //lblPassword.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0013);
                //btnEnter.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0009);

                if (PasswordMode.Equals("TRUNCATE") == false)
                {
                    lblTitle.Text = "POS Configuration";

                    lblPassword.Text = "Password";
                    btnEnter.Text = "Enter";
                }
                else
                {
                    lblTitle.Text = "Table Truncate";

                    lblPassword.Text = "Password";
                    btnEnter.Text = "Truncate";
                }


                txtPassword.Text = "";

                //this.TopMost = true;

                txtPassword.Select();
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion

        #region 텍스트박스 입력 완료 처리
        /// <summary>
        /// 텍스트박스 입력 완료 처리
        /// </summary>
        /// <returns></returns>
        private void CompleteTxtInPut()
        {
            try
            {
                string sPassword = string.Empty;
                if (PasswordMode.Equals("TRUNCATE") == false)
                {
                    sPassword = string.Format("{0:00}{1:00}", DateTime.Now.Day, DateTime.Now.Month);
                }
                else
                {
                    sPassword = "0115";
                }

                if(txtPassword.Value.Equals(sPassword) == true)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    //WinManager.ConfirmMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0310));                    
                    WinManager.ConfirmMessage("Enter password accurately.");    //관리자 비밀번호를 정확하게 입력하여 주세요.");
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion

        #region 텍스트박스 키 입력 이벤트 처리
        /// <summary>
        /// 텍스트박스 키 입력 이벤트 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="sFuncValue"></param>
        private void txtEditBox_EditBoxKeyDownEvent(object sender, string sFuncValue)
        {
            try
            {
                if (sFuncValue == PosKey.MENU_KEY.ENTER)
                {
                    if (((Cosmos.UI.CsmPosEditBox)sender) == txtPassword)
                    {
                        CompleteTxtInPut();
                    }
                }
                else if (sFuncValue == PosKey.MENU_KEY.CLEAR)
                {
                    if (((Cosmos.UI.CsmPosEditBox)sender).ReadOnly == false) ((Cosmos.UI.CsmPosEditBox)sender).Text = "";
                }
                else if (sFuncValue == PosKey.MENU_KEY.ESC_PREVIOUS)
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion

        #region 버튼 처리
        private void btnEnter_Click(object sender, EventArgs e)
        {
            try
            {
                txtEditBox_EditBoxKeyDownEvent((Cosmos.UI.CsmPosEditBox)txtPassword, PosKey.MENU_KEY.ENTER);

                padInPut.SetActiveFocus();
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                txtEditBox_EditBoxKeyDownEvent(null, PosKey.MENU_KEY.ESC_PREVIOUS);
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);

            }
        }
        #endregion

        #region 텍스트박스 활성화 처리
        /// <summary>
        /// 텍스트박스 활성화 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtEditBox_EnterEvent(object sender, EventArgs e)
        {
            try
            {
                padInPut.SetActiveControl = (Control)sender;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion

        #region INI 파일 정보 읽기
        /// <summary>
        /// INI 파일 정보를 읽어 온다.
        /// </summary>
        private void LoadIniInfo()
        {
            try
            {
                IServiceUs cReadConfigInfo = (IServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_OPEN_CLOSE.DLL, ServiceLists.BSV_OPEN_CLOSE.READ_CONFIGINFO);
                cReadConfigInfo.Execute(null);
            }
            catch (Exception ex) { }
        }
        #endregion INI 파일 정보 읽기
    }
}
