using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Cosmos.Common;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;

namespace Cosmos.Service
{
    class DeviceService : IServiceUs
    {
        private SManager sManager = new SManager();
        private IPosPrinterUs m_objPrinter = null;                // 영수증 프린터
        private IICReaderUs m_objICReader = null;                 // IC 리더기
        private ICatTerminalUs m_objCatTerminal = null;           // CAT 단말기
        private ILabelPrinterUs m_cLabelPrinterUs = null;         // 라벨프린터
        private ICashdrawerUs m_cCashdrawerUs = null;             // 돈통
        private IScaleUs m_cScaleUs = null;                       // 저울
        private ISignPadUs m_cDeviceSignPad = null;               // 서명패드
        private IDeviceUpdate m_SignPadUpdate = null;            // 서명패드 업데이트
        //private IWirelessScaleUs m_cWirelessScaleUs = null;       // 무선저울
        //private IScannerUs m_cScannerUs = null;                   // 스캐너
        private DeviceStatus m_cDevStatus = new DeviceStatus();                     // 디바이스 관리

        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        
        public DeviceService()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cDevStatus = (DeviceStatus)StateObject.DEVICE;

            m_objPrinter = (IPosPrinterUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_POSPRINTER);
            m_objICReader = (IICReaderUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_ICREADER);
            m_objCatTerminal = (ICatTerminalUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_CATTERMINAL);
            m_cLabelPrinterUs = (ILabelPrinterUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_LABELPRINTER);
            m_cCashdrawerUs = (ICashdrawerUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_CASHDRAWER);
            m_cScaleUs = (IScaleUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_SCALE);
            m_cDeviceSignPad = (ISignPadUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_SIGNPAD);
            m_SignPadUpdate = m_SignPadUpdate = (IDeviceUpdate)sManager.InitServiceInstance(ServiceLists.BSV_BASIC.DLL, ServiceLists.BSV_BASIC.SIGNPAD_UPDATE);
            //m_cWirelessScaleUs = (IWirelessScaleUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_WIRELESSSCALE);
            //m_cScannerUs = (IScannerUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_SCANNER);
        }

        public string Execute(string[] aParam)
        {
            bool lRet = false;
            string sRet = UserCom.RST_ERR;
            string[] arRet = null;

            string sDeviceName = aParam[0];

            switch (sDeviceName)
            {
                case "POSPRINTER":
                    lRet = m_objPrinter.OpenDevice(PosConst.OPOS_DEVICE.POSPRINTER, PosConst.OPOS_LDN.POSPRINTER);
                    System.Threading.Thread.Sleep(100);
                    if (lRet == true)
                    {
                        //#20171212 프린터 상태 체크 여부 추가 start
                        //0: 체크, 1: 체크안함
                        //기존
                        /*
                        if (m_cPosStatus.Base.OlePosPrinterModel == PosConst.POS_DEVICE_LIST.POS_PRINTER._1_EPSON)
                        {
                            lRet = m_objPrinter.CheckPrinter();
                        }
                        */
                        //변경
                        if (m_cPosStatus.Base.OlePosPrinterModel == PosConst.POS_DEVICE_LIST.POS_PRINTER._1_EPSON && 
                            m_cPosStatus.Base.OlePosPrinterCheckYn == "0")
                        {
                            lRet = m_objPrinter.CheckPrinter();
                        }
                        //#20171212 프린터 상태 체크 여부 추가 end

                        System.Threading.Thread.Sleep(100);
                        if (lRet == true)
                        {
                            sRet = UserCom.RST_OK;
                        }
                    }
                    // 주변장비 체크 후 프린터 Close(2017.06.15)
                    m_objPrinter.CloseDevice();
                    System.Threading.Thread.Sleep(100);
                    break;
                case "SCANNER":
                    //lRet = m_cScannerUs.OpenDevice(PosConst.OPOS_DEVICE.SCANNER, PosConst.OPOS_LDN.SCANNER);
                    //System.Threading.Thread.Sleep(100);
                    //if (lRet == true)
                    //{
                    //    sRet = UserCom.RST_OK;
                    //}
                    //m_cScannerUs.CloseDevice();
                    //System.Threading.Thread.Sleep(100);
                    break;
                case "ICREADER":
                    arRet = m_objICReader.GetStatus_ICReader(PosConst.POS_CARD_READER_DEVICE_TYPE.IC_READER);
                    System.Threading.Thread.Sleep(100);
                    if (arRet != null)
                    {
                        if (arRet[0] == " ")
                        {
                            sRet = UserCom.RST_OK;
                        }
                    }
                    break;
                case "CATTERMINAL":
                    lRet = m_objCatTerminal.SPCNSecuCatStatus();
                    if (lRet == true)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    break;
                case "CASHDRAWER":
                    lRet = m_cCashdrawerUs.OpenDevice(PosConst.OPOS_DEVICE.CASHDRAWER, PosConst.OPOS_LDN.CASHDRAWER);
                    System.Threading.Thread.Sleep(100);
                    if (lRet == true)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    m_cCashdrawerUs.CloseDevice();
                    System.Threading.Thread.Sleep(100);
                    break;
                case "SCALE":
                    lRet = m_cScaleUs.OpenDevice(m_cPosStatus.Base.OlePosScaleSerialPortNumber, m_cPosStatus.Base.OlePosScaleSerialBaudRate);
                    System.Threading.Thread.Sleep(100);
                    if (lRet == true)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    m_cScaleUs.CloseDevice();
                    System.Threading.Thread.Sleep(100);
                    break;
                case "SCALEPOLEDISP":
                    lRet = m_cScaleUs.OpenDevice(m_cPosStatus.Base.OleScalePoleDisplayPortNumber, m_cPosStatus.Base.OleScalePoleDisplayBaudRate);
                    System.Threading.Thread.Sleep(100);
                    if (lRet == true)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    m_cScaleUs.CloseDevice();
                    System.Threading.Thread.Sleep(100);
                    break;
                case "SIGNPAD":
                    string retVal = string.Empty;
                    
                    if (m_cDeviceSignPad.GetSignPadInfo(ref retVal) == true)
                    {
                        m_SignPadUpdate.SignPadModelCheck();

                        System.Threading.Thread.Sleep(100);

                        sRet = UserCom.RST_OK;
                    }
                    System.Threading.Thread.Sleep(100);
                    break;
                case "LABELPRINTER":
                    lRet = m_cLabelPrinterUs.OpenDevice(m_cPosStatus.Base.OlePosLabelPrinterSerialPortNumber, m_cPosStatus.Base.OlePosLabelPrinterSerialBaudRate);
                    System.Threading.Thread.Sleep(100);
                    if (lRet == true)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    m_cLabelPrinterUs.CloseDevice();
                    System.Threading.Thread.Sleep(100);
                    break;
                case "MSR":
                    //lRet = m_cLabelPrinterUs.OpenDevice(aParam[1], UsUtil.GetValueToLong(aParam[2]));
                    //System.Threading.Thread.Sleep(50);
                    //if (lRet == true)
                    //{
                    //    sRet = UserCom.RST_OK;
                    //}
                    //m_cLabelPrinterUs.CloseDevice();
                    //System.Threading.Thread.Sleep(50);                    
                    sRet = m_cDevStatus.Msr.UseYn == true ? UserCom.RST_OK : UserCom.RST_ERR;
                    break;
                case "NETWORK":
                    sRet = SetServerSysDateTime();
                    break;
                default:
                    sRet = UserCom.RST_ERR;
                    break;
            }
            return sRet;
        }

        #region 서버 시간 동기화 - 서버 접속 체크용으로 사용
        /// <summary>
        /// 서버 시간 동기화
        /// </summary>
        public string SetServerSysDateTime()
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                // 서버 시간 동기화
                ISvr2Tran m_cSvr2Tran = (ISvr2Tran)sManager.InitServiceInstance(ServiceLists.BSV_SALE.DLL, ServiceLists.BSV_SALE.SVR2TRAN);
                sRet = m_cSvr2Tran.ServerSysDateTime();
            }
            catch (Exception ex)
            {
            }
            return sRet;
        }
        #endregion 

    }
}
