using System;
using System.IO;

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : Pos 판매정보파일 저장
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/

namespace Cosmos.Service
{
    class SaveConfigInfo : IServiceUs
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보 참조

        public SaveConfigInfo()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;                              // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;                            // POS 거래정보
        }

        /// <summary>
        /// 설정파일 저장 (PosConfig, PosSaleInfo, PosDownInfo)
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string Execute(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                if (aParam.Length == 0 || aParam[0] == PosConst.INI_FILE_NAME.PosSaleInfo) sRet = SavePosSaleInfo();
                
                if (aParam.Length == 0 || aParam[0] == PosConst.INI_FILE_NAME.PosConfig) sRet = SavePosConfig();

                if (aParam.Length == 0 || aParam[0] == PosConst.INI_FILE_NAME.PosDevice) sRet = SavePosDevice();

                if (aParam.Length == 0 || aParam[0] == PosConst.INI_FILE_NAME.CommPort) sRet = SavePosCommPort();

                //#20171227 인천공항 포스연동 start, phj
                if (aParam.Length == 0 || aParam[0] == PosConst.INI_FILE_NAME.IncheonAirPort) sRet = SaveIncheonAirPort();
                //#20171227 인천공항 포스연동 end, phj

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }

        #region SavePosSaleInfo Pos 판매정보파일 저장 (PosSaleInfo.INI)
        /// <summary>
        /// Pos 판매정보파일 저장 (PosSaleInfo.INI)
        /// </summary>
        /// <returns></returns>
        public string SavePosSaleInfo()
        {
            string sRet = UserCom.RST_ERR;
            try
            {

                CmMessage cmPosSaleInfo = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosSaleInfo);

                cmPosSaleInfo.GetMessage("POSOPEN  ".Trim()).MakeMessageOverWrite("SaleDate         ".Trim(), m_cPosStatus.Base.SaleDate);
                cmPosSaleInfo.GetMessage("POSOPEN  ".Trim()).MakeMessageOverWrite("OpenClose        ".Trim(), m_cPosStatus.Base.OpenClose);
                cmPosSaleInfo.GetMessage("POSOPEN  ".Trim()).MakeMessageOverWrite("MstDownDate      ".Trim(), m_cPosStatus.Base.MstDownDate); 

                cmPosSaleInfo.GetMessage("POSOPEN  ".Trim()).MakeMessageOverWrite("SysOpenDate      ".Trim(), m_cPosStatus.Base.SysOpenDate);

                cmPosSaleInfo.GetMessage("CASHIER  ".Trim()).MakeMessageOverWrite("CashierNo        ".Trim(), m_cPosStatus.Base.CashierNo);
                cmPosSaleInfo.GetMessage("CASHIER  ".Trim()).MakeMessageOverWrite("CashierName      ".Trim(), m_cPosStatus.Base.CashierName);
                cmPosSaleInfo.GetMessage("CASHIER  ".Trim()).MakeMessageOverWrite("CashierAuth      ".Trim(), m_cPosStatus.Base.CashierAuth);

                cmPosSaleInfo.GetMessage("MSGNO    ".Trim()).MakeMessageOverWrite("MsgSeqNo         ".Trim(), m_cPosStatus.Base.MsgSeqNo);
                cmPosSaleInfo.GetMessage("MSGNO    ".Trim()).MakeMessageOverWrite("RegNo            ".Trim(), m_cPosStatus.Base.RegNo);
                cmPosSaleInfo.GetMessage("MSGNO    ".Trim()).MakeMessageOverWrite("StartTradeNo     ".Trim(), m_cPosStatus.Base.StartTradeNo);
                cmPosSaleInfo.GetMessage("MSGNO    ".Trim()).MakeMessageOverWrite("TradeNo          ".Trim(), m_cPosStatus.Base.TradeNo);

                cmPosSaleInfo.GetMessage("CHECKOVER").MakeMessageOverWrite("MstBatchDownDate        ".Trim(), m_cPosStatus.Base.MstBatchDownDate);
                cmPosSaleInfo.GetMessage("CHECKOVER").MakeMessageOverWrite("RegularNoticeUpdDate    ".Trim(), m_cPosStatus.Base.RegularNoticeUpdateDT);
                cmPosSaleInfo.GetMessage("CHECKOVER").MakeMessageOverWrite("EmergencyNoticeUpdDate  ".Trim(), m_cPosStatus.Base.EmergencyNoticeUpdateDT);
                cmPosSaleInfo.GetMessage("CHECKOVER").MakeMessageOverWrite("SaleNoticeUpdDate1      ".Trim(), m_cPosStatus.Base.SaleNoticeUpdateDT1);
                cmPosSaleInfo.GetMessage("CHECKOVER").MakeMessageOverWrite("SaleNoticeUpdDate2      ".Trim(), m_cPosStatus.Base.SaleNoticeUpdateDT2);
                //  2019-03-21 - 1997fx11 : Kiosk 관련 신상품 마지막업데이트일자 추가
                cmPosSaleInfo.GetMessage("CHECKOVER").MakeMessageOverWrite("NewProductUpdDate      ".Trim(), m_cPosStatus.Base.NewProductUpdateDT);

                // 파일저장
                cmPosSaleInfo.MakeFileFromMessage(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosSaleInfo);

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion


        #region SavePosConfig   Pos 환경설정정보 저장 (PosConfig.INI)
        /// <summary>
        /// POS환경설정정보 파일저장 (PosConfig.INI)
        /// </summary>
        /// <returns></returns>
        public string SavePosConfig()
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                CmMessage cmPosConfig = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosConfig);

                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("CmpCd         ".Trim(), m_cPosStatus.Base.CmpCd);
                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("StoreNo       ".Trim(), m_cPosStatus.Base.StoreNo);
                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("PosNo         ".Trim(), m_cPosStatus.Base.PosNo);
                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("BrandCd       ".Trim(), m_cPosStatus.Base.BrandCd);
                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("BrandDiv      ".Trim(), m_cPosStatus.Base.BrandDiv);
                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("LogLevel      ".Trim(), Convert.ToString(m_cPosStatus.Base.LogLevel));
                cmPosConfig.GetMessage("POS      ".Trim()).MakeMessageOverWrite("LogoUpload    ".Trim(), m_cPosStatus.Base.LogoUpload);
                
                cmPosConfig.GetMessage("DATABASE ".Trim()).MakeMessageOverWrite("LocalSource   ".Trim(), m_cPosStatus.Base.LocalDbSource);
                cmPosConfig.GetMessage("DATABASE ".Trim()).MakeMessageOverWrite("LocalCatalog  ".Trim(), m_cPosStatus.Base.LocalDbCatalog);
                cmPosConfig.GetMessage("DATABASE ".Trim()).MakeMessageOverWrite("LocalUserID   ".Trim(), m_cPosStatus.Base.LocalDbUserID);
                cmPosConfig.GetMessage("DATABASE ".Trim()).MakeMessageOverWrite("LocalPassword ".Trim(), m_cPosStatus.Base.LocalDbPassword);

                cmPosConfig.GetMessage("GLOBAL   ".Trim()).MakeMessageOverWrite("Font          ".Trim(), m_cPosStatus.Base.FONT);

                cmPosConfig.GetMessage("PRINT    ".Trim()).MakeMessageOverWrite("TextLogoMsg1  ".Trim(), m_cPosStatus.Base.TextLogoMsg1);
                cmPosConfig.GetMessage("PRINT    ".Trim()).MakeMessageOverWrite("TextLogoMsg2  ".Trim(), m_cPosStatus.Base.TextLogoMsg2);

                //#16232 환경부 1회용컵 사용줄이기 관련 테이크인아웃 설정 start
                cmPosConfig.GetMessage("PRODTAKEINOUT".Trim()).MakeMessageOverWrite("ProdTakeInOutUse".Trim(), m_cPosStatus.Base.ProdTakeInOutUse);
                cmPosConfig.GetMessage("PRODTAKEINOUT".Trim()).MakeMessageOverWrite("ProdTakeInOutDiv".Trim(), m_cPosStatus.Base.ProdTakeInOutDiv);
                //#16232 환경부 1회용컵 사용줄이기 관련 테이크인아웃 설정 end
                
                // 파일저장
                cmPosConfig.MakeFileFromMessage(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosConfig);

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region SavePosDevice   Pos Device설정정보 저장 (PosDevice.INI)
        /// <summary>
        /// POS Device 파일저장 (PosDevice.INI)
        /// </summary>
        /// <returns></returns>
        public string SavePosDevice()
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                CmMessage cmPosDevice = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosDevice);

                //Controller 0 : OPOS, 1 : RS232, 2 : USB                
                cmPosDevice.GetMessage("POSPRINTER  ".Trim()).MakeMessageOverWrite("Controller        ".Trim(), m_cPosStatus.Base.OlePosPrinterController);
                cmPosDevice.GetMessage("POSPRINTER  ".Trim()).MakeMessageOverWrite("SerialPortNumber  ".Trim(), m_cPosStatus.Base.OlePosPrinterSerialPortNumber);
                cmPosDevice.GetMessage("POSPRINTER  ".Trim()).MakeMessageOverWrite("SerialBaudRate    ".Trim(), Convert.ToString(m_cPosStatus.Base.OlePosPrinterSerialBaudRate));
                cmPosDevice.GetMessage("POSPRINTER  ".Trim()).MakeMessageOverWrite("Model             ".Trim(), m_cPosStatus.Base.OlePosPrinterModel);

                //#20171212 프린터 상태 체크 여부 추가 start
                //0: 체크, 1: 체크안함
                cmPosDevice.GetMessage("POSPRINTER  ".Trim()).MakeMessageOverWrite("CheckPrinter      ".Trim(), m_cPosStatus.Base.OlePosPrinterCheckYn);
                //#20171212 프린터 상태 체크 여부 추가 end

                cmPosDevice.GetMessage("ICReader  ".Trim()).MakeMessageOverWrite("SerialPortNumber  ".Trim(), m_cPosStatus.Base.OlePosICReaderSerialPortNumber);
                cmPosDevice.GetMessage("ICReader  ".Trim()).MakeMessageOverWrite("SerialBaudRate    ".Trim(), Convert.ToString(m_cPosStatus.Base.OlePosICReaderSerialBaudRate));
                cmPosDevice.GetMessage("ICReader  ".Trim()).MakeMessageOverWrite("Model             ".Trim(), m_cPosStatus.Base.OlePosICReaderModel);

                // 파일저장
                cmPosDevice.MakeFileFromMessage(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosDevice);

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region SavePosCommPort  Pos CommPort 설정정보 저장 (CommPort.INI)
        /// <summary>
        /// POS CommPort 파일저장 (CommPort.INI)
        /// </summary>
        /// <returns></returns>
        public string SavePosCommPort()
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                CmMessage cmPosCommPort = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.CommPort);

                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("SignLogPort ".Trim(), m_cPosStatus.Base.SignLogPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("SysInfoPort ".Trim(), m_cPosStatus.Base.SysInfoPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("TranPort    ".Trim(), m_cPosStatus.Base.TranPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("JrnlPort    ".Trim(), m_cPosStatus.Base.JrnlPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("TranInqPort ".Trim(), m_cPosStatus.Base.TranInqPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("MissChkPort ".Trim(), m_cPosStatus.Base.MissChkPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("PayInqPort  ".Trim(), m_cPosStatus.Base.PayInqPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("BizInqPort  ".Trim(), m_cPosStatus.Base.BizInqPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("MstPort     ".Trim(), m_cPosStatus.Base.MstPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("PayLogPort  ".Trim(), m_cPosStatus.Base.PayLogPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("AppLogPort  ".Trim(), m_cPosStatus.Base.AppLogPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("EtcTranPort ".Trim(), m_cPosStatus.Base.EtcTranPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("DmbIfPort   ".Trim(), m_cPosStatus.Base.DmbIfPort.ToString());
                cmPosCommPort.GetMessage("SERVER").MakeMessageOverWrite("ScaleLogPort".Trim(), m_cPosStatus.Base.ScaleLogPort.ToString());
                                
                // 파일저장
                cmPosCommPort.MakeFileFromMessage(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.CommPort);

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        //#20171227 인천공항 포스연동 start, phj
        #region SaveIncheonAirPort  Pos IncheonAirport 설정정보 저장 (IncheonAirport.INI)
        /// <summary>
        /// IncheonAirport 파일저장 (IncheonAirport.INI)
        /// </summary>
        /// <returns></returns>
        public string SaveIncheonAirPort()
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                CmMessage cmIncheonAirPort = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.IncheonAirPort);

                cmIncheonAirPort.GetMessage("INCHEONAIRPORT").MakeMessageOverWrite("AirportIP".Trim(), m_cPosStatus.IncheonAirport.AirportIP.ToString());
                cmIncheonAirPort.GetMessage("INCHEONAIRPORT").MakeMessageOverWrite("AirportUser".Trim(), m_cPosStatus.IncheonAirport.AirportUser.ToString());
                cmIncheonAirPort.GetMessage("INCHEONAIRPORT").MakeMessageOverWrite("AirportPass".Trim(), m_cPosStatus.IncheonAirport.AirportPass.ToString());
                cmIncheonAirPort.GetMessage("INCHEONAIRPORT").MakeMessageOverWrite("AirportComp".Trim(), m_cPosStatus.IncheonAirport.AirportComp.ToString());
                cmIncheonAirPort.GetMessage("INCHEONAIRPORT").MakeMessageOverWrite("AirportStore".Trim(), m_cPosStatus.IncheonAirport.AirportStore.ToString());
                cmIncheonAirPort.GetMessage("INCHEONAIRPORT").MakeMessageOverWrite("AirportPosNo".Trim(), m_cPosStatus.IncheonAirport.AirportPosNo.ToString());

                // 파일저장
                cmIncheonAirPort.MakeFileFromMessage(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.IncheonAirPort);

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion
        //#20171227 인천공항 포스연동 end, phj
    }
}
