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 frmPopupCom : Form
    {
        private SManager sManager = new SManager();                   // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = null;
        private StateServer StateObject = (StateServer)StateServer.GetInstance();


        private string m_sBizComCode;
        public string BizComCode { set { m_sBizComCode = value; } get { return m_sBizComCode; } }
        private string m_sBizMsg1;
        public string BizMsg1 { set { m_sBizMsg1 = value; } get { return m_sBizMsg1; } }
        private string m_sBizMsg2;
        public string BizMsg2 { set { m_sBizMsg2 = value; } get { return m_sBizMsg2; } }
        private string m_sBizMsg3;
        public string BizMsg3 { set { m_sBizMsg3 = value; } get { return m_sBizMsg3; } }
        
        private bool m_bOneLine;
        public bool SetOnLine { set { m_bOneLine = value; } get { return m_bOneLine; } }

        private string m_sYesMsg;
        public string SetYesMsg { set { m_sYesMsg = value; } get { return m_sYesMsg; } }

        private int m_iCountdown;
        public int SetCountDown { set { m_iCountdown = value; } get { return m_iCountdown; } }

        private string m_sTitle;
        public string SetTitle { set { m_sTitle = value; } get { return m_sTitle; } }
        
        private int m_FrmCountDown = 0;

        private string m_sCountDownMsg = "";


        public frmPopupCom()
        {
            InitializeComponent();

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

            m_cPosStatus = (PosStatus)StateObject.POS;
        }

        private void BrPopupBiz_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();
        }

        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            //this.BackgroundImage = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.EMG_MESSAGE_BOX);

            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);
            FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);

            btnYes.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0025);
            btnEsc.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0040);
            btnNo.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0026);

            rtbtext.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0746);

            lblTitle.Text = m_sTitle;

            SetRichtMsg();

            switch (m_sBizComCode)
            {
                case PosConst.MSG_BOX_TYPE.QUESTION:  // 질문
                    btnEsc.Visible = false;
                    btnNo.Visible = true;
                    btnYes.Visible = true;
                    break;
                case PosConst.MSG_BOX_TYPE.COUNTDOWN:  // 질문(카운다운)
                    btnEsc.Visible = false;
                    btnNo.Visible = true;
                    btnYes.Visible = true;
                    m_FrmCountDown = m_iCountdown == 0 ? 20 : m_iCountdown;
                    tmrCountdown.Enabled = true;
                    break;
                default:
                    btnEsc.Visible = true;
                    btnNo.Visible = false;
                    btnYes.Visible = false;
                    break;
            }

            if (m_sYesMsg != "" && btnYes.Visible == true)
            {
                btnYes.Text = m_sYesMsg;
                btnNo.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0040);
            }

            txtBox.Select();

            this.TopMost = true;
        }

        private void ClearInData()
        {
            try
            {
                m_sBizComCode = "";
                m_sBizMsg1 = "";
                m_sBizMsg2 = "";
                m_sBizMsg3 = "";
                m_bOneLine = false;
                m_sYesMsg = "";
                m_iCountdown = 20;
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                 System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.ToString());
            }
        }


        private void BrPopupBiz_Activated(object sender, EventArgs e)
        {

        }

        private void BrPopupBiz_Deactivate(object sender, EventArgs e)
        {

        }

        private void BrPopupBiz_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClearInData();
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                 System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", lblTitle.Text);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {

                //m_sOutCode = "";

                ////웹연결
                //if (m_sBizComCode == PosConst.COMMON_CODE_GROUP.Z0015)
                //{

                //    //입고처리 - 없음!
                //    //if ((UI.CsmButton)sender == btnYes) m_sOutCode = PosKey.MENU_KEY.IN_GOODS;

                //}
                //else if (m_sBizComCode == PosConst.COMMON_CODE_GROUP.Z0016)
                //{

                //}
                //else if (m_sBizComCode == PosConst.COMMON_CODE_GROUP.Z0018)
                //{

                //    //생산등록
                //    if ((UI.CsmButton)sender == btnYes) m_sOutCode = PosKey.MENU_KEY.PRODUCE_REG;

                //}
                //else if (m_sBizComCode == PosConst.COMMON_CODE_GROUP.Z0019)
                //{
                //    //월 행사 알림
                //    if ((UI.CsmButton)sender == btnNo) m_sOutCode = PosKey.MENU_KEY.PRODUCE_REG;

                //}

                //this.DialogResult = m_sOutCode == "" ? DialogResult.Cancel : DialogResult.OK;
                //this.Close();

                if (((Cosmos.UI.CsmButton)sender) == btnEsc)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                if (((Cosmos.UI.CsmButton)sender) == btnYes)
                {
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
                if (((Cosmos.UI.CsmButton)sender) == btnNo)
                {
                    this.DialogResult = DialogResult.No;
                    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>
        /// 리치박스 MOT 텍스트 설정
        /// </summary>
        /// <returns></returns>
        public bool SetRichtMsg()
        {
            try
            {

                rtbtext.Text = "";

                int iStrtNum = 0;

                if (m_bOneLine == true)
                {

                    m_sBizMsg1 = "\n\n" + m_sBizMsg1;

                    rtbtext.AppendText(m_sBizMsg1);
                    rtbtext.Select(iStrtNum, m_sBizMsg1.Length);
                    rtbtext.SelectionColor = Color.Black;
                    iStrtNum += m_sBizMsg1.Length;

                    rtbtext.AppendText(m_sBizMsg2);
                    rtbtext.Select(iStrtNum, m_sBizMsg2.Length);
                    rtbtext.SelectionColor = Color.FromArgb(220, 98, 164);
                    iStrtNum += m_sBizMsg2.Length;

                    rtbtext.AppendText(m_sBizMsg3);
                    rtbtext.Select(iStrtNum, m_sBizMsg3.Length);
                    rtbtext.SelectionColor = Color.Black;
                    iStrtNum += m_sBizMsg3.Length;
                }
                else
                {
                    m_sBizMsg1 = m_sBizMsg1.Replace("|", "\n");
                    SetDispMsg();
                    rtbtext.AppendText(m_sBizMsg1);
                }


                rtbtext.Select(0, rtbtext.TextLength);
                rtbtext.SelectionAlignment = HorizontalAlignment.Center;

                rtbtext.Select(0, 0);
                rtbtext.Refresh();

                return true;

            }
            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 false;
            }

        }

        private void SetDispMsg()
        {

            try
            {

                if (m_sBizComCode == PosConst.MSG_BOX_TYPE.COUNTDOWN)
                {
                    m_sBizMsg1 += string.Format("\n#$ {1}", "", MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0367));
                }

                string[] aStr = m_sBizMsg1.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

                int iRow = aStr.Length + 1;

                switch (iRow)
                {
                    case 1:
                    case 2:
                        m_sBizMsg1 = string.Format("\n\n{0}", m_sBizMsg1);
                        break;                        
                    case 3:
                    case 4:
                        m_sBizMsg1 = string.Format("\n{0}", m_sBizMsg1);
                        break;
                    default:
                        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);

            }
        }



        private void btnEsc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 리치텍스트 박스 포커스 방지
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rtbtext_Enter(object sender, EventArgs e)
        {

            try
            {
                this.txtBox.Focus();
            }
            catch { }
        }

        private void tmrCountdown_Tick(object sender, EventArgs e)
        {
            try
            {
                tmrCountdown.Enabled = false;

                if (m_FrmCountDown == 0)
                {
                    btnOk_Click(btnYes, null);
                }
                else
                {
                    m_FrmCountDown--;
                    rtbtext.Text = m_sBizMsg1.Replace("#$", m_FrmCountDown.ToString());

                    rtbtext.Select(0, rtbtext.TextLength);
                    rtbtext.SelectionAlignment = HorizontalAlignment.Center;
                    rtbtext.Select(0, 0);

                    tmrCountdown.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "Process Exception !!! " + ex.Message);
            }
        }

        private void txtBox_EditBoxKeyDownEvent(object sender, string sFuncValue)
        {
            try
            {
                if (sFuncValue == PosKey.MENU_KEY.ENTER)
                {
                    UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "KeyBorad Enter!");
                    btnOk_Click(btnYes, null);
                }

            }
            catch (Exception ex)
            {   
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "Process Exception !!! " + ex.Message);
            }
        }
    }
}
