using PTMedicalInsurance.CardReaders;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PTMedicalInsurance.CardReaders
{
    class SFZReader
    {
        private int iPort;
        private int currBaud = 0;
        

        public SFZReader()
        { }

        public int ReadSFZ(out string msg)
        {
            msg = "";
            int iRes = 0;
            do
            {
                if (Find(out msg) != 0) 
                {
                    
                    iRes = -1;
                    break;
                }

                  
                if (Read(out msg) != 0)
                {
                    iRes = -1;
                    break;
                }
            } while (false);

            return iRes;
        }
        private int Find(out string msg)
        {
            msg = "";
            int nRet = ReadCardAPI.Syn_FindReader();
            if (nRet == 0)
            {
                msg = "没有找到读卡器";
                return -1;
            }
            else
            {
                iPort = nRet;
                if (iPort > 1000)
                {
                    msg = "读卡器连接在USB端口 " + nRet + "，" ;
                }
                else
                {
                    Thread.Sleep(200);
                    int nRet2 = ReadCardAPI.Syn_GetCOMBaud(nRet, ref currBaud);
                }
                return 0;
            }
        }

        private int Open(out string msg)
        {
            msg = "";
            return 0;
        }

        private int Read(out string msg)
        {
            msg = "";
            IDCardData idcardData = new IDCardData();
            string path = Global.curEvt.path + "\\Photos";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            byte[] ctmp = System.Text.Encoding.Default.GetBytes(path);
            Global.writeLog(Global.curEvt.path + "\\Photos");
            int nRet = ReadCardAPI.Syn_SetPhotoPath(2, ctmp);
            setupDataFormate(1);
            nRet = ReadCardAPI.Syn_OpenPort(iPort);

            if (nRet == 0)
            {
                DateTime startTime = DateTime.Now;
                nRet = ReadCardAPI.Syn_ReadMsg(iPort, 0, ref idcardData);
                int dTime = (int)(DateTime.Now - startTime).TotalMilliseconds;
                if (nRet == 0 || nRet == 1)     //0读取身份证信息成功，1读取身份证信息成功，解码照片不成功，其他.读取身份证信息失败
                {
                    msg = idcardData.ToString();
                }
                else
                {
                    msg = "读取身份证信息错误，" + dTime + "ms";
                    return -1;
                }
            }
            else
            {
                msg = "打开端口错误";
                return -1;
            }
            if (iPort > 0)
            {
                ReadCardAPI.Syn_ClosePort(iPort);
            }
            return 0;
        }

        /// <summary>
        /// 设置读取到数据的格式
        /// </summary>
        /// <param name="translateType">具体参见API说明</param>
        static void setupDataFormate(int translateType)
        {
            ReadCardAPI.Syn_SetPhotoName(2);
            ReadCardAPI.Syn_SetNationType(translateType);
            ReadCardAPI.Syn_SetSexType(translateType);
            ReadCardAPI.Syn_SetBornType(translateType);
            ReadCardAPI.Syn_SetUserLifeBType(translateType);
            ReadCardAPI.Syn_SetUserLifeEType(translateType, 0);
        }
    }
}
