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

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 신용카드, 포인트 승인 처리(SPCN)
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    public partial class frmVanMessage : Form
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출

        private IICReaderUs m_cICReader = null;                                     // IC리더기 

        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_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; }
        }
                
        string m_sICData;
        /// <summary>
        /// IC Chip 데이터
        /// </summary>
        public string ICData
        {
            get { return this.m_sICData; }
            set { this.m_sICData = value; }
        }

        string m_sSignData;
        /// <summary>
        /// 전자서명 데이터
        /// </summary>
        public string SignData
        {
            get { return this.m_sSignData; }
            set { this.m_sSignData = value; }
        }

        string m_sOilData;
        /// <summary>
        /// 유가보조/면세유 데이터
        /// </summary>
        public string OilData
        {
            get { return this.m_sOilData; }
            set { this.m_sOilData = value; }
        }
                
        string m_sAuthInfo;
        /// <summary>
        /// 등록제 정보 데이터
        /// </summary>
        public string AuthInfo
        {
            get { return this.m_sAuthInfo; }
            set { this.m_sAuthInfo = value; }
        }

        string m_sLastRecvData;
        /// <summary>
        /// 망취소시 마지막 응답 전문
        /// </summary>
        public string LastRecvData
        {
            get { return this.m_sLastRecvData; }
            set { this.m_sLastRecvData = value; }
        }
        

        public frmVanMessage()
        {
            InitializeComponent();

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

            m_cICReader = (IICReaderUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_ICREADER);
        }

        private void frmVanMessage_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 = "";
                
                if (string.IsNullOrEmpty(m_sSendData) == true) m_sSendData = "";
                if (string.IsNullOrEmpty(m_sICData) == true) m_sICData = "";
                if (string.IsNullOrEmpty(m_sSignData) == true) m_sSignData = "";
                if (string.IsNullOrEmpty(m_sOilData) == true) m_sOilData = "";
                if (string.IsNullOrEmpty(m_sAuthInfo) == true) m_sAuthInfo = "";
                if (string.IsNullOrEmpty(m_sLastRecvData) == true) m_sLastRecvData = "";

                this.m_nRecvRet = 0;
                this.TopMost = true;
                
                tmrStart.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 frmVanMessage_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, 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);
                }

                m_sAuthInfo = "";
            }
            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 OnNetworkIRT()
        {
            try
            {
                m_bSecondDisp = true;
                if (m_cICReader == null) m_cICReader = (IICReaderUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_ICREADER);


                if (m_sLastRecvData.Trim() != "") //망취소
                    m_nRecvRet = m_cICReader.SpcnCommPosForNetCancel(m_sSendData, m_sLastRecvData, ref m_sRecvData);
                else
                    m_nRecvRet = m_cICReader.SpcnCommPos(m_sSendData, m_sSignData, m_sICData, m_sOilData, m_sAuthInfo, ref m_sRecvData);
            }
            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);
            }
            finally
            {
                m_bSecondDisp = false;
            }
        }

        
    }
}
