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 frmPosLoad : 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 IServiceUs m_cPosConfig = null;                                     // 환경설정 정보 송/수신

        private bool bChageEvent = false;

        #endregion

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

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

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보
            m_cPosConfig = (IServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_ADMIN.DLL, ServiceLists.BSV_ADMIN.POS_CONFIG);

        }

        private void frmPosLoad_Load(object sender, EventArgs e)
        {
            InitControl();
        }
        #endregion

        #region 폼 컨트롤 초기화
        private void InitControl()
        {
            try
            {
                // 이미지 로딩 처리
                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_S);
                this.Size = new Size(788, 402);

                FormManager.SetFormAllControlFont(this, SystemFonts.DefaultFont.Name);

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

                //테마색상 적용!
                if (m_cPosStatus.ScnMst.ThemeColor != "") btnAll.BackColor = CmUtil.GetColorToString(m_cPosStatus.ScnMst.ThemeColor);

                bChageEvent = false;

            }
            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

        /// <summary>
        /// 종료
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                if (bChageEvent == true)
                    this.DialogResult = DialogResult.OK;
                else
                    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);

            }
        }

        /// <summary>
        /// 버튼 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                if ((UI.CsmButton)sender == btnCommPort)        //포트정보
                {
                    GetConfig(btnCommPort.Tag.ToString(), btnCommPort.Text);
                }
                else if ((UI.CsmButton)sender == btnPosConfig)  //환경설정
                {
                    GetConfig(btnPosConfig.Tag.ToString(), btnPosConfig.Text);
                }
                else if ((UI.CsmButton)sender == btnPosDevice)  //주변장치
                {
                    GetConfig(btnPosDevice.Tag.ToString(), btnPosDevice.Text);
                }
                else if ((UI.CsmButton)sender == btnAll)        //전체
                {
                    GetConfig("", btnAll.Text);
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);

            }
        }

        /// <summary>
        /// 데이터 수신
        /// </summary>
        /// <param name="sFileName"></param>
        /// <param name="sShowName"></param>
        /// <returns></returns>
        private string GetConfig(string sFileName, string sShowName)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                sRet = m_cPosConfig.Execute(new string[] { ItemConst.POS_CONFIG_TYPE.LOAD, sFileName });

                if (sRet == UserCom.RST_OK)
                {
                    WinManager.ErrorMessage(sShowName + " Complete to recv data.");     //수신이 성공 했습니다.");
                    bChageEvent = true;
                }
                else if (sRet == UserCom.RST_IGNORE)
                {
                    WinManager.ErrorMessage(sShowName + " Not recv data.");     //수신할 정보가 없습니다.");
                }
                else
                {
                    WinManager.ErrorMessage(sShowName + " Fail to recv data."); //수신이 실패 했습니다.");
                }

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


    }
}
