using System;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 점포서버 IRT 통신 처리(제이슨 방식)
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    public partial class frmPosIrtMessage : Form
    {
        private SManager sManager = new SManager();             //이 객체를 통해 업무 Service 호출

        private INetworkJSON m_cNetService = null;

        private bool m_bSecondDisp = false;         //초 표시여부
        private int m_nSecondCount = 0;             //초(경과)

        string m_sMessageStr;
        /// <summary>
        /// 화면표시 메시지
        /// </summary>
        public string PosMessageStr
        {
            get { return this.m_sMessageStr; }
            set
            {
                this.m_sMessageStr = value;
                try
                {
                    lblMessage.Text = m_sMessageStr;
                }
                catch { }
            }
        }

        string m_sServerIp;
        /// <summary>
        /// 서버IP
        /// </summary>
        public string ServerIp
        {
            get { return this.m_sServerIp; }
            set { this.m_sServerIp = value; }
        }

        int m_nServerPort;
        /// <summary>
        /// 서버PORT
        /// </summary>
        public int ServerPort
        {
            get { return this.m_nServerPort; }
            set { this.m_nServerPort = value; }
        }

        int m_nTimeOut;
        /// <summary>
        /// 서버 Timeout
        /// </summary>
        public int TimeOut
        {
            get { return this.m_nTimeOut; }
            set { this.m_nTimeOut = value; }
        }

        string m_sCommHead;
        /// <summary>
        /// 송신데이터(공통해더)
        /// </summary>
        public string CommHead
        {
            get { return this.m_sCommHead; }
            set { this.m_sCommHead = value; }
        }

        Hashtable m_htSendData;
        /// <summary>
        /// 송신데이터
        /// </summary>
        public Hashtable SendData
        {
            get { return this.m_htSendData; }
            set { this.m_htSendData = value; }
        }

        Hashtable m_htRecvData;
        /// <summary>
        /// 수신데이터
        /// </summary>
        public Hashtable RecvData
        {
            get { return this.m_htRecvData; }
            set { this.m_htRecvData = value; }
        }

        int m_nRecvRet;
        /// <summary>
        /// 수신리턴값
        /// </summary>
        public int RecvRet
        {
            get { return this.m_nRecvRet; }
            set { this.m_nRecvRet = value; }
        }
        
        public frmPosIrtMessage()
        {
            InitializeComponent();

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

            m_cNetService = (INetworkJSON)sManager.InitServiceInstance(ServiceLists.AGENT_NETWORK.DLL, ServiceLists.AGENT_NETWORK.NETWORK_JSON);
        }

        private void frmPosIrtMessage_Load(object sender, EventArgs e)
        {
            try
            {
                UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");

                CmMessage m_PosConfig = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + "PosConfig.INI");
                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);

                picSearchMessage.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.SCH_MESSAGE_BOX);

                if (m_sMessageStr != "") lblMessage.Text = m_sMessageStr;

                this.m_htRecvData = null;
                this.m_nRecvRet = 0;
                this.TopMost = true;

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

        private void frmPosIrtMessage_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 + "()", "");
        }

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

                m_bSecondDisp = true;
                m_nSecondCount = 0;

                Thread thrSecondDisp = new Thread(new ThreadStart(OnNetworkIRT));
                thrSecondDisp.Start();

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

        private void OnSecondDisplay()
        {
            try
            {
                while(m_bSecondDisp == true)
                {
                    m_nSecondCount++;
                    if(m_nSecondCount % 10 == 0)
                    {
                        lblMessage.Text = m_sMessageStr + " (" + (int)(m_nSecondCount / 10) + ")";
                        lblMessage.Update();
                    }
                    Thread.Sleep(100);
                }
            }
            catch(Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.ERROR_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
                    "Process Exception !!! " + ex.Message);
            }
        }

        private void OnNetworkIRT()
        {
            try
            {
                m_bSecondDisp = true;
                if (m_cNetService == null) m_cNetService = (INetworkJSON)sManager.InitServiceInstance(ServiceLists.AGENT_NETWORK.DLL, ServiceLists.AGENT_NETWORK.NETWORK_IRT);
                m_nRecvRet = m_cNetService.IRTSendReceive(m_sServerIp, m_nServerPort, m_nTimeOut, m_sCommHead, m_htSendData, ref m_htRecvData);
            }
            catch(Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.ERROR_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
                    "Process Exception !!! " + ex.Message);
            }
            finally
            {
                m_bSecondDisp = false;
            }
        }

        
         
    }
}
