/******************************************************************************
 * 文件名称: CardReader.cs
 * 文件说明: 读卡的封装，包括社保卡，身份证，电子凭证等等
 * 当前版本: V1.0
 * 创建日期: 2022-06-20
 *
 * * 2020-06-20: 增加 CardReader 类
 * *****  2020-06-20: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-20: 增加 CardReader 方法重载，国家版电子凭证使用
 * *****  2020-06-20: 增加 Init 方法，SSCard环境初始化
 * *****  2020-06-20: 增加 ReadCardBas 方法，读社保卡
 * *****  2020-06-20: 增加 VerifyPIN 方法，验证密码
 * *****  2020-06-20: 增加 ChangePIN 方法，修改密码
 * *****  2020-06-20: 增加 ReadSFZ 方法，读身份证
 * *****  2020-06-20: 增加 GetQRBase 方法，读二维码
 * *****  2020-06-20: 增加 NationEcTrans 方法，读电子凭证（国家版）


******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using PTMedicalInsurance.Variables;

namespace PTMedicalInsurance.Common
{
    class CardReader
    {
        const string DllPath = @"D:\CardDLL\SSCard.dll";

        [DllImport("SSCard.dll", EntryPoint = "Init", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int Init(StringBuilder pURL, StringBuilder pUser);

        [DllImport("SSCard.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadCardBas(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "VerifyPIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int VerifyPIN(StringBuilder pOutBuff, int nOutBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "ChangePIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ChangePIN(StringBuilder pOutBuff, int nOutBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadSFZ(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "GetQRBase", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int GetQRBase(int nTimeout, StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);
        //电子凭证
        [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr NationEcTrans(StringBuilder strUrl, StringBuilder InData, StringBuilder OutData);


        private string URL;
        private string User;
        public CardReader()
        {

        }
        public CardReader(string URL, string User)
        {
            this.URL = URL;
            this.User = User;
        }


        public int Init()
        {
            string errMsg = "";
            int result = -1;
            try
            {
                StringBuilder sbURL = new StringBuilder(URL);
                StringBuilder sbUSER = new StringBuilder(User);

                result = Init(sbURL, sbUSER);
                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return result;
                
            }
            finally
            {
                Global.writeLog("初始化",URL +":" +User, result.ToString() + ":" + errMsg );
            }
        }




        public int ReadCardBas(out string basInfo, out string regInfo)
        {
            basInfo = ""; regInfo = "";
            int rtn = -1;
            try
            {
                StringBuilder sbBasInfo = new StringBuilder(1024);
                StringBuilder sbRegInfo = new StringBuilder(1024);

                rtn = ReadCardBas(sbBasInfo, 1024, sbRegInfo, 1024);
                basInfo = sbBasInfo.ToString();
                regInfo = sbRegInfo.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                basInfo = "ReadCardBas异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("读社保卡(" + URL + ")", regInfo, basInfo);
            }
        }

        public int VerifyPIN(out string outBuff)
        {
            outBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                rtn = VerifyPIN(sbOutBuff, 1024);
                outBuff = sbOutBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                outBuff = "VerifyPIN:" + ex.Message;
                return rtn;
            }
        }

        public int ChangePIN(out string outBuff)
        {
            outBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                rtn = ChangePIN(sbOutBuff, 1024);
                outBuff = sbOutBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                outBuff = "ChangePIN:" + ex.Message;
                return rtn;
            }
        }

        public int ReadSFZ(out string OutBuff, out string SignBuff)
        {
            OutBuff = ""; SignBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                StringBuilder sbSignBuff = new StringBuilder(1024);

                rtn = ReadSFZ(sbOutBuff, 1024, sbSignBuff, 1024);
                OutBuff = sbOutBuff.ToString();
                SignBuff = sbSignBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                OutBuff = "ReadSFZ 异常:" + ex.Message;
                return rtn;
            }
        }


        public int GetQRBase(int timeout, out string OutBuff, out string SignBuff)
        {
            OutBuff = ""; SignBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                StringBuilder sbSignBuff = new StringBuilder(1024);

                rtn = GetQRBase(timeout, sbOutBuff, 1024, sbSignBuff, 1024);
                OutBuff = sbOutBuff.ToString();
                SignBuff = sbSignBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                OutBuff = "GetQRBase 异常:" + ex.Message;
                return rtn;
            }
        }

        public string NationEcTrans(string URL, string InData, out string OutData)
        {
            OutData = "";
            try
            {
                StringBuilder sbURL = new StringBuilder(URL);
                StringBuilder sbInData = new StringBuilder(InData);
                StringBuilder sbOutData = new StringBuilder(1024);

                IntPtr pRtn = NationEcTrans(sbURL, sbInData, sbOutData);
                OutData = sbOutData.ToString();
                string rtn = Marshal.PtrToStringAnsi(pRtn);
                return rtn;
            }
            catch (Exception ex)
            {
                OutData = "NationEcTrans 异常:" + ex.Message;
                return "-1";
            }
            finally
            {
                Global.writeLog("电子凭证(" + URL +")",InData,OutData);
            }
        }

    }
}
