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.ServiceProvider;
using Cosmos.CommonManager;
using Cosmos.Common;
using Cosmos.UserFrame;
using Cosmos.BaseFrame;

namespace Cosmos.Win
{
    public partial class frmEtcPvc : Form
    {
        #region 변수 선언

        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private IServiceUs m_cEtcPvc = null;                                       // 테스트모드 서비스
        private DeviceStatus m_cDevStatus = new DeviceStatus();                     // 디바이스 관리

        #endregion 변수 선언

        #region 생성자 & 소멸자
        public frmEtcPvc()
        {
            InitializeComponent();

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

            m_cDevStatus = (DeviceStatus)StateObject.DEVICE;

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cEtcPvc = (IServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_ADMIN.DLL, ServiceLists.BSV_ADMIN.ETC_PVC);
        }

        private void frmEtcPvc_Load(object sender, EventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");
            // 콘트롤 초기화 처리
            InitControl(); 
        }

        private void frmEtcPvc_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", lblTitle.Text);
        }
        #endregion 생성자 & 소멸자

        #region 폼 컨트롤 초기화
        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                string sOrgDate = m_cPosStatus.Base.SaleDate;

                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);
                FormManager.SetTextBoxGlobalInfo(this, m_cPosStatus.Global.m_stCultureMaster.nGroupingDigits, m_cPosStatus.Global.m_stCultureMaster.strGroupingSymbol
                    , m_cPosStatus.Global.m_stCultureMaster.nDecimalDigits, m_cPosStatus.Global.m_stCultureMaster.strDecimalSymbol);

                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_S);

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

                //테마색상 적용!
                btnSave.BackColor = CmUtil.GetColorToString(m_cPosStatus.ScnMst.ThemeColor);
                padInPut.ThemeColor = m_cPosStatus.ScnMst.ThemeColor;
                padInPut.ClearBtnText = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0817);
                padInPut.SearchBtnText = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_1018);

                // 라벨 이름 설정
                lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0338);

                txtDate.InputTypeFormat = m_cPosStatus.Global.m_stCultureMaster.strDatePattern;
                txtDate.Text = m_cPosStatus.Global.DateToCulture(m_cPosStatus.Base.SaleDate);

                txtBoxLQty.Text = "0";
                txtBoxMQty.Text = "0";
                txtBoxSQty.Text = "0";

                // 기능 버튼 설정 체크
                FormManager.SetbtnMenu(null, m_cDevStatus.ICReader.UseYn, btnMenu1, btnMenu2, btnMenu3, btnMenu4);

                txtBoxLQty.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 Control Event
        /// <summary>
        /// 저장
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                string sSaleDate = m_cPosStatus.Global.CultureToDate(txtDate.Text);
                // 등록일자만 체크
                if (sSaleDate != m_cPosStatus.Base.SaleDate)
                {
                    if (WinManager.QuestionMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0220)) != true)
                    {
                        return;
                    }
                }

                // 전송실패시 재전송 처리
                while (true)
                {
                    // 저장
                    sRet = m_cEtcPvc.Execute(new string[] { txtBoxLQty.Text, txtBoxMQty.Text, txtBoxSQty.Text });
                    if (sRet != UserCom.RST_OK)
                    {
                        //통신에 실패하였습니다. 다시 시도 하시겠습니까?
                        if (WinManager.QuestionMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0221)) != true)
                        {
                            // 재전송을 하지 않겠다면 멈춘다.
                            break;
                        }

                    }
                    else
                    {
                        WinManager.ConfirmMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0222));
                        this.Close();
                        break;
                    }
                }
            }
            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 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);
            }
        }

        /// <summary>
        /// 텍스트박스 키 입력 이벤트 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="sFuncValue"></param>
        private void txtEditBox_EditBoxKeyDownEvent(object sender, string sFuncValue)
        {
            try
            {
                string sTxtBoxName = ((Cosmos.UI.CsmPosEditBox)sender).Name;

                if (sFuncValue == PosKey.MENU_KEY.ENTER)
                {
                    if (sTxtBoxName == "txtBoxLQty")
                    {
                        txtBoxMQty.Focus();
                    }
                    else if (sTxtBoxName == "txtBoxMQty")
                    {
                        txtBoxSQty.Focus();
                    }
                }
                else if (sFuncValue == PosKey.MENU_KEY.CLEAR)
                {
                    // CLEAR 눌렀을 경우 뭘 해줘야 할까...
                }
                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);
            }
        }

        /// <summary>
        /// 종료
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion Control Event

        private void btnDate_Click(object sender, EventArgs e)
        {
            try
            {
                string sDate = WinBasic.ShowForm(new string[] { FormManager.FORM_CALENDAR });
                if (sDate != "") txtDate.Text = m_cPosStatus.Global.DateToCulture(sDate);
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
    }
}
