using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.Common;
using System.Drawing;

namespace Cosmos.CommonManager
{
    /// <summary>
    /// frmMessageBox
    /// </summary>
    public partial class frmMessageBox : Form
    {
        
        private string m_sMessageType;
        /// <summary>
        /// 메시지 형태(MSG_BOX_TYPE)
        /// </summary>
        public string PosMessageType { get { return this.m_sMessageType; } set { this.m_sMessageType = value; } }

        string m_sMessageStr;
        /// <summary>
        /// 메시지 
        /// </summary>
        public string PosMessageStr { get { return this.m_sMessageStr; } set { this.m_sMessageStr = value; } }

        string m_sMessageTitle;
        /// <summary>
        /// 메시지 타이틀
        /// </summary>
        public string PosMessageTitle { get { return this.m_sMessageTitle; } set { this.m_sMessageTitle = value; } }

        bool m_bSoundContinue = false;
        /// <summary>
        /// 경고음 계속 출력 여부
        /// </summary>
        public bool SoundContinue { get { return this.m_bSoundContinue; } set { this.m_bSoundContinue = value; } }
        
        /// <summary>
        /// 경고음 처리 변수
        /// </summary>
        public long m_nSoundCount = 0;

        /// <summary>
        /// 카운트 다운 
        /// </summary>
        public long m_nCountDown = 9;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary> frmMessageBox </summary>
        public frmMessageBox()
        {
            InitializeComponent();

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

        private void frmMessageBox_Load(object sender, EventArgs e)
        {
            //#17651 해피오더)자동주문 시 표시문구 변경 start
            //기존
            //this.BackgroundImage = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_MSGBOX);
            //변경
            if (m_sMessageType == PosConst.MSG_BOX_TYPE.AutomaticShipment)
            {
                this.BackgroundImage = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_800X320);
            }
            else
            {
                this.BackgroundImage = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_MSGBOX);
            }
            //#17651 해피오더)자동주문 시 표시문구 변경 end

            InitControl();
        }

        private void frmMessageBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmrSoundOn.Enabled = false;
            tmrCountDown.Enabled = false;
        }

        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                tmrCountDown.Enabled = false;

                //#17651 해피오더)자동주문 시 표시문구 변경 start
                lblMessage2.Visible = false;
                //#17651 해피오더)자동주문 시 표시문구 변경 end


                //#17651 해피오더)자동주문 시 표시문구 변경 start                
                btnYes.BringToFront();
                btnNo.BringToFront();
                btnEsc.BringToFront();

                if (m_sMessageType == PosConst.MSG_BOX_TYPE.AutomaticShipment)
                {
                    base.Width = 800;
                    base.Height = 320;

                    lblMessage.Width = lblTitle.Width;
                    lblMessage.Top = lblTitle.Top + lblTitle.Height;
                    lblMessage.Height = 50;// base.Height - lblMessage.Top - 10;

                    lblMessage2.Width = lblTitle.Width;
                    lblMessage2.Top = lblMessage.Top + lblMessage.Height;
                    lblMessage2.Height = base.Height - (lblMessage.Top + lblMessage.Height) - 20;

                    string sTemp = m_sMessageStr;
                    sTemp = sTemp.Replace("\r\n", "▣").Replace("\r", "▣").Replace("\n", "▣").Replace("\\r\\n", "▣").Replace("\\r", "▣").Replace("\\n", "▣").Trim();

                    string[] arMsg = null;
                    arMsg = sTemp.Split(new string[] { "▣" }, StringSplitOptions.None);

                    lblMessage.Text = "";
                    lblMessage2.Text = "";

                    if (arMsg != null)
                    {
                        lblMessage.Text = arMsg[0];

                        string TempMsg2 = "";
                        for (int i = 1; i <= arMsg.Length - 1; i++)
                        {
                            TempMsg2 += arMsg[i] + (char)13 + (char)10;
                        }
                        lblMessage2.Text = TempMsg2;
                    }

                    lblMessage.TextAlign = ContentAlignment.TopLeft;
                    lblMessage2.TextAlign = ContentAlignment.TopLeft;
                }
                else
                {
                    base.Width = 788;
                    base.Height = 212;
                    //lblMessage.Top 56
                    //lblMessage.Width 772
                    //lblMessage.Height 101                    

                    lblMessage.TextAlign = ContentAlignment.MiddleCenter;
                }
                //#17651 해피오더)자동주문 시 표시문구 변경 end


                CmMessage m_PosConfig = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosConfig);

                string sFont = m_PosConfig.GetMessage("GLOBAL").GetMessageValue("Font");
                FormManager.SetFormAllControlFont(this, sFont);

                long nScreenSizeUser = CmUtil.LongParse(m_PosConfig.GetMessage("GLOBAL").GetMessageValue("ScreenSizeUser"));
                FormManager.MovePopUpForm(this, false, nScreenSizeUser);

                lblTitle.BackColor = Color.FromArgb(70, 86, 98);
                lblTitle.ForeColor = Color.White;   //Color.FromArgb(35, 31, 32);


                //#15945 해피오더 자동주문 상세기능 start
                btnYes.Size = new System.Drawing.Size(121, 37);
                btnNo.Size = new System.Drawing.Size(121, 37);
                btnEsc.Size = new System.Drawing.Size(121, 37);

                btnYes.Location = new System.Drawing.Point(254, 162);
                btnNo.Location = new System.Drawing.Point(410, 162);
                btnEsc.Location = new System.Drawing.Point(333, 162);
                //#15945 해피오더 자동주문 상세기능 end

                switch (m_sMessageType)
                {
                    case PosConst.MSG_BOX_TYPE.CONFIRM:     // 확인메시지
                        //#17651 해피오더)자동주문 시 표시문구 변경 start
                        lblMessage2.Visible = false;
                        //#17651 해피오더)자동주문 시 표시문구 변경 end
                        btnYes.Visible = false;
                        btnNo.Visible = false;
                        btnEsc.Visible = true;
                        btnEsc.Text = CmUtil.IsNull(MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0009), "OK");    //확인

                        break;
                    case PosConst.MSG_BOX_TYPE.QUESTION:    // 질문메시지(예,아니오)
                        //#17651 해피오더)자동주문 시 표시문구 변경 start
                        lblMessage2.Visible = false;
                        //#17651 해피오더)자동주문 시 표시문구 변경 end
                        btnEsc.Visible = false;
                        btnYes.Visible = true;
                        btnNo.Visible = true;
                        btnYes.Text = CmUtil.IsNull(MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0025), "YES");
                        btnNo.Text = CmUtil.IsNull(MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0026), "NO");

                        break;
                    case PosConst.MSG_BOX_TYPE.COMPLETE:    // 질문메시지(확정,아니오)
                        //#17651 해피오더)자동주문 시 표시문구 변경 start
                        lblMessage2.Visible = false;
                        //#17651 해피오더)자동주문 시 표시문구 변경 end
                        btnEsc.Visible = false;
                        btnYes.Visible = true;
                        btnNo.Visible = true;
                        btnYes.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0028);
                        btnNo.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0026);

                        m_nSoundCount = 0;
                        tmrSoundOn.Enabled = true;

                        break;

                    //#15945 해피오더 자동주문 상세기능 start

                    case PosConst.MSG_BOX_TYPE.AutomaticShipment:
                        //#17651 해피오더)자동주문 시 표시문구 변경 start
                        lblMessage2.Visible = true;
                        //#17651 해피오더)자동주문 시 표시문구 변경 end
                        btnEsc.Visible = true;
                        btnYes.Visible = true;
                        btnNo.Visible = true;
                        btnYes.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_1170);
                        btnNo.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_1171);
                        btnEsc.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0040);

                        btnYes.Size = new System.Drawing.Size(280, 37);
                        btnNo.Size = new System.Drawing.Size(280, 37);
                        btnEsc.Size = new System.Drawing.Size(150, 37);

                        int iWidth = btnYes.Width + btnNo.Width + btnEsc.Width;
                        int iTerm = (this.Width - iWidth) / 4;
                        
                        //#17651 해피오더)자동주문 시 표시문구 변경 start
                        //기존
                        /*
                        btnYes.Location = new System.Drawing.Point(iTerm, 162);
                        btnNo.Location = new System.Drawing.Point((iTerm * 2) + btnYes.Width, 162);
                        btnEsc.Location = new System.Drawing.Point((iTerm * 3) + btnYes.Width + btnNo.Width, 162);
                        */
                        //변경
                        btnYes.Location = new System.Drawing.Point(iTerm, 270);
                        btnNo.Location = new System.Drawing.Point((iTerm * 2) + btnYes.Width, 270);
                        btnEsc.Location = new System.Drawing.Point((iTerm * 3) + btnYes.Width + btnNo.Width, 270);
                        //#17651 해피오더)자동주문 시 표시문구 변경 end
                        
                        break;

                    //#15945 해피오더 자동주문 상세기능 end

                    case PosConst.MSG_BOX_TYPE.COUNTDOWN:    // 질문메시지(확정,아니오)
                        btnEsc.Visible = false;
                        btnYes.Visible = true;
                        btnNo.Visible = true;

                        //#20180327 영수증 출력 여부 버튼 명칭 변경 start
                        //기존
                        /*
                        btnYes.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0028);
                        btnNo.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0026);
                        */
                        //변경
                        btnYes.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_1113);
                        btnNo.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_1114);
                        //#20180327 영수증 출력 여부 버튼 명칭 변경 end

                        m_nSoundCount = 0;
                        m_nCountDown = 9;
                        tmrSoundOn.Enabled = true;
                        tmrCountDown.Interval = 100;
                        tmrCountDown.Enabled = true;
                        break;

                    //#14941 스마트영수증 대상고객 안내기능 도입 start
                    case PosConst.MSG_BOX_TYPE.COUNTDOWNCONFIRM:    // 확인메시지
                        btnYes.Visible = false;
                        btnNo.Visible = false;
                        btnEsc.Visible = true;
                        btnEsc.Text = CmUtil.IsNull(MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0009), "OK");    //확인

                        m_nSoundCount = 0;
                        m_nCountDown = 5;
                        //tmrSoundOn.Enabled = true;
                        tmrCountDown.Interval = 100;
                        tmrCountDown.Enabled = true;
                        break;
                    //#14941 스마트영수증 대상고객 안내기능 도입 end

                    default:                                // 에러메시지
                        btnYes.Visible = false;
                        btnNo.Visible = false;
                        btnEsc.Visible = true;
                        btnEsc.Text = CmUtil.IsNull(MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0009), "OK");    //확인

                        m_nSoundCount = 0;
                        tmrSoundOn.Enabled = true;
                        
                        lblTitle.BackColor = Color.DarkOrange;
                        lblTitle.ForeColor = Color.White;

                        break;
                }
                SoundOn();

                lblTitle.Text = m_sMessageTitle;
                lblMessage.Text = m_sMessageStr;

                //UserLog.WriteLogFile(UserCom.LOG_OP,
                //                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                //                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                //                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                //                     "Title : '" + m_sMessageTitle + "', Message : '" + m_sMessageStr + "'");

                this.TopMost = 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);
            }
        }

        /// <summary>
        /// 버튼 입력 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                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)
            {
                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);
            }
        }


        /// <summary>
        /// 사운드 처리
        /// </summary>
        private void SoundOn()
        {
            //try
            //{
            //    OperatingSystem os = Environment.OSVersion;

            //    if (os.Platform == PlatformID.Win32NT && os.Version.Major == 5)  // WIN XP
            //        Console.Beep();
            //    else
            //        System.Media.SystemSounds.Beep.Play();
            //}
            //catch { }
        }

        /// <summary>
        /// 사운드 타이머
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmrSoundOn_Tick(object sender, EventArgs e)
        {
            try
            {
                tmrSoundOn.Enabled = false;

                SoundOn();

                if (m_nSoundCount <= 20)
                {
                    if (m_bSoundContinue == false)
                    {
                        m_nSoundCount++;
                    }
                    else
                    {
                        this.TopLevel = true;
                    }
                    tmrSoundOn.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 lblTitle_MouseMove(object sender, MouseEventArgs e)
        {
            //try
            //{
            //    var s = sender as Cosmos.UI.CsmLabel;
            //    if (e.Button != System.Windows.Forms.MouseButtons.Left)
            //        return;

            //    if (this.Left + (e.X - ((Point)s.Tag).X) + this.Width / 2 <= 0) return;
            //    if (this.Left + (e.X - ((Point)s.Tag).X) + (this.Width / 3) >= Screen.PrimaryScreen.Bounds.Width) return;

            //    if (this.Top + (e.Y - ((Point)s.Tag).Y) <= lblTitle.Height / 2 * (-1)) return;
            //    if (this.Top + (e.Y - ((Point)s.Tag).Y) + (lblTitle.Height) >= Screen.PrimaryScreen.Bounds.Height) return;

            //    s.Parent.Left = this.Left + (e.X - ((Point)s.Tag).X);
            //    s.Parent.Top = this.Top + (e.Y - ((Point)s.Tag).Y);
            //}
            //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 lblTitle_MouseDown(object sender, MouseEventArgs e)
        {
            //try
            //{
            //    var s = sender as Cosmos.UI.CsmLabel;
            //    s.Tag = new Point(e.X, e.Y);
            //}
            //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);
            //}
        }

        /// <summary>
        /// 카운트 다운 메세지 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmrCountDown_Tick(object sender, EventArgs e)
        {
            try
            {
                tmrCountDown.Enabled = false;
                tmrCountDown.Interval = 1000;

                if (m_nCountDown == 0)
                {
                    //#20180226 영수증 출력 여부 팝업에서 미 선택시 '아니오'로 처리 start
                    //기존
                    //btnProc_Click(btnYes, null);
                    //변경
                    btnProc_Click(btnNo, null);
                    //#20180226 영수증 출력 여부 팝업에서 미 선택시 '아니오'로 처리 end
                }
                else
                {
                    m_nCountDown--;
                    lblMessage.Text = string.Format("{0}\n{1} {2}", m_sMessageStr, m_nCountDown, MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0611));
                    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 frmMessageBox_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    switch (m_sMessageType)
                    {
                        case PosConst.MSG_BOX_TYPE.CONFIRM:     // 확인메시지
                            btnProc_Click(btnEsc, null);

                            break;
                        case PosConst.MSG_BOX_TYPE.QUESTION:    // 질문메시지(예,아니오)
                            btnProc_Click(btnNo, null);

                            break;
                        case PosConst.MSG_BOX_TYPE.COMPLETE:    // 질문메시지(확정,아니오)
                            btnProc_Click(btnNo, null);

                            break;
                        case PosConst.MSG_BOX_TYPE.COUNTDOWN:    // 질문메시지(확정,아니오)
                            btnProc_Click(btnNo, null);

                            break;

                        //#15945 해피오더 자동주문 상세기능 start

                        case PosConst.MSG_BOX_TYPE.AutomaticShipment:
                            btnProc_Click(btnNo, null);

                            break;

                        //#15945 해피오더 자동주문 상세기능 end

                        default:                                // 에러메시지
                            btnProc_Click(btnNo, null);

                            break;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

    }
}
