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

using Cosmos.UserFrame;
using Cosmos.Common;

namespace Cosmos.CommonManager
{
    /// <summary>
    /// WinManager
    /// </summary>
    public class WinManager
    {
        [DllImport("user32.dll", EntryPoint = "SwitchToThisWindow")]
        private static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

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

        /// <summary>
        /// SystemParametersInfo
        /// </summary>
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);

        [DllImport("User32.dll", EntryPoint = "ShowWindowAsync")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        private const int WS_SHOWNORMAL = 1;

        /// <summary>
        /// 조회중 메시지 표시 화면
        /// </summary>
        private static frmSearchMessage m_frmSchMsg = new frmSearchMessage();

        /// <summary>
        /// 메시지박스 출력 처리
        /// </summary>
        /// <param name="sMsgType">메시지형태(PosConst.MSG_BOX_TYPE)</param>
        /// <param name="sMsgStr">메시지</param>
        /// <param name="sTitle">타이틀</param>
        /// <returns></returns>
        public static DialogResult CosmosMessageBox(string sMsgType, string sMsgStr, string sTitle)
        {
            return CosmosMessageBox(sMsgType, sMsgStr, sTitle, false);
        }
        /// <summary> CosmosMessageBox </summary>
        public static DialogResult CosmosMessageBox(string sMsgType, string sMsgStr, string sTitle, bool bSoundContinue)
        {
            string sMessage = string.Empty;

            try
            {
                if (sMsgStr.Length >= UserCom.RST_IGNORE.Length)
                {
                    if (sMsgStr.Substring(0, UserCom.RST_IGNORE.Length) == UserCom.RST_IGNORE)
                    {
                        sMsgStr = sMsgStr.Substring(UserCom.RST_IGNORE.Length, sMsgStr.Length - UserCom.RST_IGNORE.Length);
                    }
                    else if (sMsgStr.Substring(0, UserCom.RST_ERR.Length) == UserCom.RST_ERR)
                    {
                        sMsgStr = sMsgStr.Substring(UserCom.RST_ERR.Length, sMsgStr.Length - UserCom.RST_ERR.Length);
                    }
                    else if (sMsgStr.Substring(0, UserCom.RST_OK.Length) == UserCom.RST_OK)
                    {
                        sMsgStr = sMsgStr.Substring(UserCom.RST_OK.Length, sMsgStr.Length - UserCom.RST_OK.Length);
                    }
                    else if (sMsgStr.Substring(0, UserCom.RST_OK.Length) == UserCom.RST_RETRY)
                    {
                        sMsgStr = sMsgStr.Substring(UserCom.RST_OK.Length, sMsgStr.Length - UserCom.RST_OK.Length);
                    }
                }

                if (sMsgStr.Length == 4) sMessage = MessageManager.GetErrorMessage(sMsgStr).Trim();
                if (string.IsNullOrEmpty(sMessage)) sMessage = sMsgStr.Trim();
                if (sMessage.Length <= 0) sMessage = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0132).Trim();

                if (string.IsNullOrEmpty(sTitle.Trim()))
                {
                    switch (sMsgType)
                    {
                        //case PosConst.MSG_BOX_TYPE.CONFIRM:     // 확인메시지
                        //    sTitle = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0023);
                        //    break;
                        case PosConst.MSG_BOX_TYPE.QUESTION:    // 질문메시지(예,아니오)
                        case PosConst.MSG_BOX_TYPE.COMPLETE:    // 질문메시지(확정,아니오)
                        case PosConst.MSG_BOX_TYPE.COUNTDOWN:   // 질문메시지(확정,아니오)
                        //#15945 해피오더 자동주문 상세기능 start
                        case PosConst.MSG_BOX_TYPE.AutomaticShipment:
                        //#15945 해피오더 자동주문 상세기능 end
                            sTitle = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0024);
                            break;
                        default:                                // 에러메시지 -> 확인메세지 변경 2016-10-17 현업 요청!
                            sTitle = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0023); //MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0027);
                            break;
                    }
                }
                else
                {
                    // 메시지 타이틀 설정
                    if (sTitle.Length == 4)
                    {
                        string PosMessageTitle = MessageManager.GetErrorMessage(sTitle);
                        if (PosMessageTitle != "") sTitle = PosMessageTitle;
                    }
                }

                string sTemp = "[" + sTitle + "] " + sMessage.Replace("\r\n", "▣").Replace("\r", "▣").Replace("\n", "▣").Replace("\\r\\n", "▣").Replace("\\r", "▣").Replace("\\n", "▣").Trim();
                UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "frmMessageBox()", sTemp);
                
                frmMessageBox fMsgBox = new frmMessageBox();

                fMsgBox.PosMessageType = sMsgType;
                fMsgBox.PosMessageStr = sMessage.Replace("\\r\\n", "\r\n").Replace("\\r", "\r").Replace("\\n", "\n");
                fMsgBox.PosMessageTitle = sTitle;
                fMsgBox.SoundContinue = bSoundContinue; 

                for (int i = 0; i < Screen.AllScreens.Length; i++)
                {
                    if (Screen.AllScreens[i].Primary == true)                       // 캐셔화면
                    {
                        int nWidth = Screen.AllScreens[i].WorkingArea.Width;
                        int nHeight = Screen.AllScreens[i].WorkingArea.Height;

                        fMsgBox.Location = new Point((nWidth - fMsgBox.Width) / 2, (nHeight - fMsgBox.Height) / 2);
                        fMsgBox.StartPosition = FormStartPosition.Manual;
                        break;
                    }
                }

                WinManager.HideSearchMessage(null);

                return fMsgBox.ShowDialog();
            }
            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 + "()", "Exception. MsgStr=[" + sMessage.Replace("\r\n", "▣").Replace("\r", "▣").Replace("\n", "▣").Trim() + "]");
                return DialogResult.None;
            }
        }

        /// <summary>
        /// 메시지박스 출력 처리
        /// </summary>
        /// <param name="sMsgType">메시지형태(PosConst.MSG_BOX_TYPE)</param>
        /// <param name="sMsgStr">메시지</param>
        /// <returns></returns>
        public static DialogResult CosmosMessageBox(string sMsgType, string sMsgStr)
        {
            return CosmosMessageBox(sMsgType, sMsgStr, "");
        }

        /// <summary>
        /// 예외 에러 메시지 처리
        /// </summary>
        /// <param name="sProject"></param>
        /// <param name="sFunction"></param>
        /// <param name="sMsgStr"></param>
        public static void ExceptionMessage(string sProject, string sFunction, string sMsgStr)
        {
            try
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR, sProject, sFunction, "Exception." + sMsgStr);

                //CosmosMessageBox(PosConst.MSG_BOX_TYPE.ERROR, MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0009));
            }
            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 + "()", ex.Message);
            }
        }

        /// <summary>
        /// 에러 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <param name="sTitle"></param>
        public static void ErrorMessage(string sMsgStr, string sTitle)
        {
            CosmosMessageBox(PosConst.MSG_BOX_TYPE.ERROR, sMsgStr, sTitle);
        }
        /// <summary> ErrorMessage </summary>
        public static void ErrorMessage(string sMsgStr, string sTitle, bool bSoundContinue)
        {
            CosmosMessageBox(PosConst.MSG_BOX_TYPE.ERROR, sMsgStr, sTitle, bSoundContinue);
        }

        /// <summary>
        /// 에러 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        public static void ErrorMessage(string sMsgStr)
        {
            CosmosMessageBox(PosConst.MSG_BOX_TYPE.ERROR, sMsgStr);
        }

        //#15945 해피오더 자동주문 상세기능 start
        /// <summary>
        /// 질문 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <param name="sTitle"></param>

        //#15945 해피오더 자동주문 상세기능 start - 20180820
        //기존
        //public static bool AutomaticShipmentQuestionMessage(string sMsgStr, string sTitle)
        //변경
        public static DialogResult AutomaticShipmentQuestionMessage(string sMsgStr, string sTitle)
        //#15945 해피오더 자동주문 상세기능 end - 20180820 
        {
            try
            {
                //#15945 해피오더 자동주문 상세기능 start - 20180820
                //기존
                //if (CosmosMessageBox(PosConst.MSG_BOX_TYPE.AutomaticShipment, sMsgStr, sTitle) == DialogResult.Yes)
                //{
                //    return true;
                //}

                //변경
                return CosmosMessageBox(PosConst.MSG_BOX_TYPE.AutomaticShipment, sMsgStr, sTitle);

                //if (CosmosMessageBox(PosConst.MSG_BOX_TYPE.AutomaticShipment, sMsgStr, sTitle) == DialogResult.Yes)
                //{
                //    //예
                //    return DialogResult.Yes;
                //}
                //else if (CosmosMessageBox(PosConst.MSG_BOX_TYPE.AutomaticShipment, sMsgStr, sTitle) == DialogResult.No)
                //{
                //    //아니오
                //    return DialogResult.No;
                //}
                //else if (CosmosMessageBox(PosConst.MSG_BOX_TYPE.AutomaticShipment, sMsgStr, sTitle) == DialogResult.OK)
                //{
                //    //닫기
                //    return DialogResult.OK;
                //}
                //#15945 해피오더 자동주문 상세기능 end - 20180820
            }
            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 + "()", 
                                     ex.Message);

                return DialogResult.OK;
            }
            
        }
        //#15945 해피오더 자동주문 상세기능 end

        /// <summary>
        /// 질문 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <param name="sTitle"></param>
        public static bool QuestionMessage(string sMsgStr, string sTitle)
        {
            try
            {
                if (CosmosMessageBox(PosConst.MSG_BOX_TYPE.QUESTION, sMsgStr, sTitle) == DialogResult.Yes)
                {
                    return 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 + "()", ex.Message);
            }
            return false;
        }
        /// <summary>
        /// 질문 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <returns></returns>
        public static bool QuestionMessage(string sMsgStr)
        {
            return QuestionMessage(sMsgStr, "");
        }
        
        /// <summary>
        /// 카운트 다운 질문 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <param name="sTitle"></param>
        public static bool CountDownQuestionMessage(string sMsgStr, string sTitle)
        {
            try
            {
                if (CosmosMessageBox(PosConst.MSG_BOX_TYPE.COUNTDOWN, sMsgStr, sTitle) == DialogResult.Yes)
                {
                    return 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 + "()", ex.Message);
            }
            return false;
        }
        /// <summary>
        /// 카운트 다운 질문 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <returns></returns>
        public static bool CountDownQuestionMessage(string sMsgStr)
        {
            return CountDownQuestionMessage(sMsgStr, "");
        }

        //#14941 스마트영수증 대상고객 안내기능 도입 start
        /// <summary>
        /// 카운트 다운 확인 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <returns></returns>
        public static void CountDownConfirmMessage(string sMsgStr)
        {
            CosmosMessageBox(PosConst.MSG_BOX_TYPE.COUNTDOWNCONFIRM, sMsgStr);
        }
        //#14941 스마트영수증 대상고객 안내기능 도입 end

        /// <summary>
        /// 확인 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        /// <param name="sTitle"></param>
        public static void ConfirmMessage(string sMsgStr, string sTitle)
        {
            CosmosMessageBox(PosConst.MSG_BOX_TYPE.CONFIRM, sMsgStr, sTitle);
        }
        /// <summary>
        /// 확인 메시지 처리
        /// </summary>
        /// <param name="sMsgStr"></param>
        public static void ConfirmMessage(string sMsgStr)
        {
            ConfirmMessage(sMsgStr, "");
        }

        /// <summary>
        /// 조회중 메시지 보이기
        /// </summary>
        /// <param name="sMsg"></param>
        public static void ShowSearchMessage(string sMsg)
        {
            ShowHideSearchMessage(true, sMsg);
        }
        /// <summary> ShowSearchMessage </summary>
        public static void ShowSearchMessage(string sMsg, Form frmOwner)
        {
            ShowHideSearchMessage(true, sMsg, frmOwner);
        }

        /// <summary>
        /// 조회중 메시지 숨기기
        /// </summary>
        public static void HideSearchMessage()
        {
            ShowHideSearchMessage(false, "");
        }
        /// <summary> HideSearchMessage </summary>
        public static void HideSearchMessage(Form frmOwner)
        {
            ShowHideSearchMessage(false, "", frmOwner);
        }

        /// <summary> ShowHideMessageDelegate </summary>
        public delegate void ShowHideMessageDelegate(bool bShow, string pText);

        /// <summary>
        /// 조회중 메시지 처리
        /// </summary>
        /// <param name="bShow"></param>
        /// <param name="sMsg"></param>
        private static void ShowHideSearchMessage(bool bShow, string sMsg)
        {
            ShowHideSearchMessage(bShow, sMsg, null);
        }
        private static void ShowHideSearchMessage(bool bShow, string sMsg, Form frmOwner)
        {
            try
            {
                if(m_frmSchMsg.InvokeRequired)
                {
                    ShowHideMessageDelegate delegateSearchMessage = new ShowHideMessageDelegate(ShowHideSearchMessage);
                    m_frmSchMsg.Invoke(delegateSearchMessage, new object[] { bShow, sMsg });
                }
                else
                {
                    if(bShow == true)
                    {
                        //for (int i = 0; i < Screen.AllScreens.Length; i++)
                        //{
                        //    if (Screen.AllScreens[i].Primary == true)                       // 캐셔화면
                        //    {
                        //        int nWidth = Screen.AllScreens[i].WorkingArea.Width;
                        //        int nHeight = Screen.AllScreens[i].WorkingArea.Height;

                        //        m_frmSchMsg.Location = new Point((nWidth - m_frmSchMsg.Width) / 2, (nHeight - m_frmSchMsg.Height) / 2);
                        //        m_frmSchMsg.StartPosition = FormStartPosition.Manual;
                        //        break;
                        //    }
                        //}

                        m_frmSchMsg.TopMost = true;
                        m_frmSchMsg.MessageString = sMsg;
                        if (frmOwner != null) m_frmSchMsg.Owner = frmOwner;

                        m_frmSchMsg.Show();
                        m_frmSchMsg.Update();
                    }
                    else
                    {
                        if (frmOwner != null) m_frmSchMsg.Owner = frmOwner;
                        m_frmSchMsg.Hide();
                    }
                    //m_frmSchMsg.Refresh();
                }
            }
            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 + "()", ex.Message);
            }
        }
        
    }
}
