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 frmExIrtMessage : Form
    {
        private SManager sManager = new SManager();             //이 객체를 통해 업무 Service 호출

        private INetworkIRT  m_cNetService = null;

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

        string m_sMessageStr;
        /// <summary>
        /// 화면표시 메시지
        /// </summary>
        public string MessageStr
        {
            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_sSendData;
        /// <summary>
        /// 송신데이터
        /// </summary>
        public string SendData
        {
            get { return this.m_sSendData; }
            set { this.m_sSendData = value; }
        }

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

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

        // 수신데이터길이 고정 여부
        bool m_bRecvLenFix;
        public bool RecvLenFix
        {
            get { return this.m_bRecvLenFix; }
            set { this.m_bRecvLenFix = value; }
        }

        int m_nRecvDataLen;
        /// <summary>
        /// 수신리턴 길이
        /// </summary>
        public int RecvDataLen
        {
            get { return this.m_nRecvDataLen; }
            set { this.m_nRecvDataLen = value; }
        }

        bool m_bFullSize;
        /// <summary>
        /// 헤더길이 설정값이 전체인지 Body만 인지
        /// </summary>
        public bool RecvFullSize
        {
            get { return this.m_bFullSize; }
            set { this.m_bFullSize = value; }
        }

        string m_sAckData;
        /// <summary>
        /// 수신 완료 응답 (ACK)
        /// </summary>
        public string AckData
        {
            get { return this.m_sAckData; }
            set { this.m_sAckData = value; }
        }

        int m_nRecvDataStartPosition;
        /// <summary>
        /// 수신리턴 길이 시작위치
        /// </summary>
        public int RecvDataStartPosition
        {
            get { return this.m_nRecvDataStartPosition; }
            set { this.m_nRecvDataStartPosition = value; }
        }

        // 수신데이터 오류 무시 
        bool m_bErrorSkip;
        public bool RecvErrorSkip
        {
            get { return this.m_bErrorSkip; }
            set { this.m_bErrorSkip = value; }
        }

        // Byte 통신 여부
        bool m_bCommByteType;
        public bool CommByteType
        {
            get { return this.m_bCommByteType; }
            set { this.m_bCommByteType = value; }
        }

        // Byte 통신 전송값
        byte[] m_bSendByteData;
        public byte[] SendByteData
        {
            get { return this.m_bSendByteData; }
            set { this.m_bSendByteData = value; }
        }

        // Byte 통신 응답값
        byte[] m_bRecvByteData;
        public byte[] RecvByteData
        {
            get { return this.m_bRecvByteData; }
            set { this.m_bRecvByteData = value; }
        }

        public frmExIrtMessage()
        {
            InitializeComponent();

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

            m_cNetService = (INetworkIRT)sManager.InitServiceInstance(ServiceLists.AGENT_NETWORK.DLL, ServiceLists.AGENT_NETWORK.NETWORK_IRT);
        }

        private void frmExIrtMessage_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_sRecvData = "";
                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 frmExIrtMessage_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 = (INetworkIRT)sManager.InitServiceInstance(ServiceLists.AGENT_NETWORK.DLL, ServiceLists.AGENT_NETWORK.NETWORK_IRT);

                if (m_bCommByteType == true)
                {
                    // Bypass Byte 통신 
                    m_nRecvRet = m_cNetService.ByPassSendReceiveData(m_sServerIp, m_nServerPort, m_nTimeOut, m_bSendByteData, ref m_bRecvByteData, m_nRecvDataLen); 
                }
                else if (m_bErrorSkip == true)
                {
                    // 수신길이 고정방식 
                    m_nRecvRet = m_cNetService.IRTSendReceiveEx(m_sServerIp, m_nServerPort, m_nTimeOut, m_sSendData, ref m_sRecvData, m_nRecvDataLen, m_sAckData, m_bErrorSkip);
                }
                else if (m_bRecvLenFix == true)
                {
                    // 수신길이 고정방식 
                    m_nRecvRet = m_cNetService.IRTSendReceiveEx(m_sServerIp, m_nServerPort, m_nTimeOut, m_sSendData, ref m_sRecvData, m_nRecvDataLen, m_sAckData);
                }
                else
                {
                    // 수신길이 가변방식
                    m_nRecvRet = m_cNetService.IRTSendReceiveLen(m_sServerIp, m_nServerPort, m_nTimeOut, m_sSendData, ref m_sRecvData, m_bFullSize, m_nRecvDataLen, m_nRecvDataStartPosition);
                }
            }
            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;
            }
        }

        
    }
}
