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

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

namespace Cosmos.Service
{
    class ICReaderUpdate : IICReaderUpdate
    {
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private DeviceStatus m_cDevStatus = new DeviceStatus();                     // 디바이스 관리
        private IICReaderUs m_cDeviceICReader = null;                               // IC리더기
        private IDataCommonUs m_cDataCommon = null;                                 // POS 공통함수 인터페이스
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출

        public ICReaderUpdate()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;        // POS 기본정보
            m_cDevStatus = (DeviceStatus)StateObject.DEVICE;

            m_cDeviceICReader = (IICReaderUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_ICREADER);
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
        }

        /// <summary>
        /// IC리더기 펌웨어 업데이트 건수
        /// </summary>
        /// <param name="sFilePath"></param>
        /// <param name="sFirmFileNm"></param>
        /// <returns></returns>
        public int ICReaderUpdateCnt(string sFilePath, ref string sFirmFileNm)
        {
            int nRet = 0;

            sFirmFileNm = "";

            try
            {
                if (System.IO.Directory.Exists(sFilePath))
                {
                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sFilePath);

                    if (di.GetFiles().Count() > 0)
                    {
                        nRet = nRet + 1;

                        FileInfo[] aFirm = di.GetFiles();

                        sFirmFileNm = aFirm[0].Name;
                    }
                }

                return nRet;
            }
            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 nRet;
        }

        /// <summary>
        /// IC리더기 모델 체크
        /// </summary>
        /// <returns></returns>
        public bool ICReaderModelCheck()
        {
            bool bRtn = false;
            string sModelDiv = "";   // 모델구분
            StringBuilder sbVer = null;

            try
            {
                // IC리더기 스펙체크
                sbVer = m_cDeviceICReader.SetAuthInfoString(PosConst.POS_CARD_READER_DEVICE_TYPE.IC_READER);
                sModelDiv = CmUtil.MidH(sbVer.ToString(), 0, 12);
                sModelDiv = sModelDiv.Replace("#", "").Trim();

                if (sModelDiv == PosConst.PGM_UPDATE_TYPE.POSDOWN_GROUP_70_DIV.IC_READER_MODEL_SR_440P)
                {
                    bRtn = true;
                }
          
            }
            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 bRtn;
        }

        /// <summary>
        /// IC리더기 펌웨어 업데이트 건수
        /// </summary>
        /// <param name="nErrCode"></param>
        /// <returns></returns>
        public string ICReaderResultCode(int nErrCode)
        {
            string sMsgCode = string.Empty;

            try
            {
                if (nErrCode > 0 && nErrCode != 1003)
                {
                    return sMsgCode = POS_MESSAGE.ERROR.MSG_0724;
                }

                switch (nErrCode)
                {
                    case 0: sMsgCode = POS_MESSAGE.ERROR.MSG_0704; break;
                    case -1: sMsgCode = POS_MESSAGE.ERROR.MSG_0705; break;
                    case -2: sMsgCode = POS_MESSAGE.ERROR.MSG_0706; break;
                    case -3: sMsgCode = POS_MESSAGE.ERROR.MSG_0707; break;
                    case -4: sMsgCode = POS_MESSAGE.ERROR.MSG_0708; break;
                    case -5: sMsgCode = POS_MESSAGE.ERROR.MSG_0709; break;
                    case -6: sMsgCode = POS_MESSAGE.ERROR.MSG_0710; break;
                    case -7: sMsgCode = POS_MESSAGE.ERROR.MSG_0711; break;
                    case -10: sMsgCode = POS_MESSAGE.ERROR.MSG_0712; break;
                    case -11: sMsgCode = POS_MESSAGE.ERROR.MSG_0713; break;
                    case -12: sMsgCode = POS_MESSAGE.ERROR.MSG_0714; break;
                    case -13: sMsgCode = POS_MESSAGE.ERROR.MSG_0715; break;
                    case -14: sMsgCode = POS_MESSAGE.ERROR.MSG_0716; break;
                    case -15: sMsgCode = POS_MESSAGE.ERROR.MSG_0717; break;
                    case -16: sMsgCode = POS_MESSAGE.ERROR.MSG_0718; break;
                    case -17: sMsgCode = POS_MESSAGE.ERROR.MSG_0719; break;
                    case -18: sMsgCode = POS_MESSAGE.ERROR.MSG_0720; break;
                    case -19: sMsgCode = POS_MESSAGE.ERROR.MSG_0721; break;
                    case -20: sMsgCode = POS_MESSAGE.ERROR.MSG_0722; break;
                    case 1003: sMsgCode = POS_MESSAGE.ERROR.MSG_0723; break;

                    default: sMsgCode = POS_MESSAGE.ERROR.MSG_0725; break;
                }

            }
            catch (Exception) 
            {
                throw; 
            }
            return sMsgCode;
        }
    }
}
