using System;
using System.Windows.Forms;
using cn.mineki.Reader;
namespace testReadCardForm
{
    public partial class MainForm : Form
    {
        public string name;     //姓名
        public string sex;      //性别
        public string people;    //民族，护照识别时此项为空
        public string birthday;   //出生日期
        public string address;  //地址，在识别护照时导出的是国籍简码
        public string number;  //地址，在识别护照时导出的是国籍简码
        public string signdate;   //签发日期，在识别护照时导出的是有效期至 
        public string validtermOfStart;  //有效起始日期，在识别护照时为空
        public string validtermOfEnd;  //有效截止日期，在识别护照时为空        
        int nReaderPort = 0;
        bool isClose = false;
        private int nErr;
        private int nOK;

        public MainForm()
        {
            InitializeComponent();
            cmbReaderPort.SelectedIndex = 2;
            Text = "身份证阅读器演示程序 " + Program.Version;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label12.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.AutoReadCard();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int isSuccess = IDCardReader.CloseComm();
                this.Close();
                isClose = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.AutoReadCard();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isClose)
            {
                try
                {
                    if(timer1.Enabled)
                    {
                        timer1.Stop();
                    }
                    IDCardReader.CloseComm();
                    System.IO.File.Delete(Application.StartupPath + "\\zp.bmp");
                    System.IO.File.Delete(Application.StartupPath + "\\wz.txt");
                    System.IO.File.Delete(Application.StartupPath + "\\xp.wlt");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void AutoReadCard()
        {
            if (nReaderPort == 0)
            {   
                int authenticate = IDCardReader.Authenticate();
                if (authenticate == 0 || chkReadContinue.Checked)
                {
                    int mode = 1;
                    if (chkFpr.Checked) mode = 6;
                    int readContent = IDCardReader.Read_Content(mode);
                    if (readContent != 0)
                    {
                        nErr++;
                        return;
                    }
                    else nOK++;

                    System.Media.SystemSounds.Beep.Play();

                    if (IDCardReader.IsFPRIDCard() == 1)
                    {
                        lblCardType.Text = "外国人永久居留证";
                        lblCHNNameMeg.Visible = true;
                        lblCHNName.Visible = true;
                        lblAddress.Visible = false;
                        lblDept.Visible = false;//机关代码
                        label6.Visible = false;
                        label5.Text = "证件号：";
                        label7.Visible = false;
                        label2.Text = "国籍：";
                        label4.Text = "出生日期：";
                        label12.Text = "";

                        if (readContent == 0)
                        {
                            byte[] arrTmp = new byte[100];
                            int nLen = 100;
                            label10.Text = "读卡操作成功！" + "[" + nOK + "][" + nErr + "]";

                            //姓名
                            IDCardReader.GetFPR_ENName(arrTmp, ref nLen);
                            string str = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //Console.WriteLine(str);
                            lblName.Text = str.Replace(" ", "");
                            //中文姓名
                            IDCardReader.GetFPR_CHNName(arrTmp, ref nLen);
                            lblCHNName.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //证件号码
                            IDCardReader.GetFPR_IDNo(arrTmp, ref nLen);
                            lblIdCard.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //性别
                            IDCardReader.GetFPR_SEX(arrTmp, ref nLen);
                            lblSex.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //国籍
                            IDCardReader.GetFPR_Nation(arrTmp, ref nLen);
                            lblNation.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            IDCardReader.GetFPR_NationCode(arrTmp, ref nLen);
                            lblNation.Text += "/" + System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //出生日期
                            IDCardReader.GetFPR_Birthday(arrTmp, ref nLen);
                            lblBirthday.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //签发时间
                            IDCardReader.GetFPR_ValidStartDate(arrTmp, ref nLen);
                            lblValidDate.Text = "";
                            lblValidDate.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            lblValidDate2.Text = "至  ";
                            //终止时间
                            IDCardReader.GetFPR_ValidEndDate(arrTmp, ref nLen);
                            lblValidDate2.Text += System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);                            
                            //照片
                            if (System.IO.File.Exists("ZP.bmp"))
                            {
                                pictureBox1.ImageLocation = Application.StartupPath + "\\zp.bmp";
                            }
                        }
                    }
                    else if (IDCardReader.IsGATIDCard() == 1)
                    {
                        lblCardType.Text = "港澳台居民居住证";

                        lblCHNNameMeg.Visible = false;
                        lblCHNName.Visible = false;
                        lblAddress.Visible = true;
                        lblDept.Visible = true;
                        label6.Visible = true;
                        label7.Visible = true;
                        label5.Text = "身份证号：";
                        label4.Text = "通行证号：";
                        label2.Text = "签次：";
                        if (readContent == 0)
                        {
                            label10.Text = "读卡操作成功！" + "[" + nOK + "][" + nErr + "]";

                            byte[] arrTmp = new byte[100];
                            int nLen = 100;
                            //详细地址
                            IDCardReader.GetGAT_Address(arrTmp, ref nLen);
                            lblAddress.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //性别
                            IDCardReader.GetGAT_SEX(arrTmp, ref nLen);
                            lblSex.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //通行证号码
                            IDCardReader.GetGAT_PassportNo(arrTmp, ref nLen);
                            lblBirthday.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //签发机关
                            IDCardReader.GetGAT_IssuingAuthority(arrTmp, ref nLen);
                            lblDept.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //公民身份证号码
                            IDCardReader.GetGAT_IDNo(arrTmp, ref nLen);
                            lblIdCard.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //姓名
                            IDCardReader.GetGAT_Name(arrTmp, ref nLen);
                            lblName.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //签发次数
                            IDCardReader.GetGAT_IssuanceCount(arrTmp, ref nLen);
                            lblNation.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetGAT_ValidStartDate(arrTmp, ref nLen);
                            lblValidDate.Text = "";
                            lblValidDate.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            lblValidDate2.Text = "至  ";
                            IDCardReader.GetGAT_ValidEndDate(arrTmp, ref nLen);
                            lblValidDate2.Text += System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //指纹信息
                            bool flag = false;
                            byte[] arrFinger = new byte[1024];
                            int fingerLen = 100;
                            flag = IDCardReader.GetFingerPrint(arrFinger, ref fingerLen);
                            if (flag)
                            {
                                label12.Text = "已获取指纹";
                            }
                            else
                            {
                                label12.Text = "无指纹信息";
                            }

                            //照片
                            if (System.IO.File.Exists("ZP.bmp"))
                            {
                                pictureBox1.ImageLocation = Application.StartupPath + "\\zp.bmp";
                            }
                        }
                    }
                    else if (IDCardReader.IsFPR2IDCard() == 1)
                    {
                        lblCardType.Text = "新版外国人永久居留证";
                        lblCHNNameMeg.Visible = true;
                        lblCHNName.Visible = true;
                        lblAddress.Visible = false;
                        lblDept.Visible = false;//机关代码
                        label6.Visible = false;
                        label5.Text = "证件号：";
                        label7.Visible = false;
                        label2.Text = "国籍：";
                        label4.Text = "出生日期：";
                        label12.Text = "换证次数：";

                        if (readContent == 0)
                        {
                            byte[] arrTmp = new byte[100];
                            int nLen = 100;
                            label10.Text = "读卡操作成功！" + "[" + nOK + "][" + nErr + "]";

                            //姓名
                            IDCardReader.GetFPR2_ENName(arrTmp, ref nLen);
                            string str = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            IDCardReader.GetFPR2_ENNameB(arrTmp, ref nLen);
                            str += System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            lblName.Text = str.Replace(" ", "");
                            //中文姓名
                            IDCardReader.GetFPR2_CHNName(arrTmp, ref nLen);
                            lblCHNName.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //证件号码
                            IDCardReader.GetFPR2_IDNo(arrTmp, ref nLen);
                            lblIdCard.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //性别
                            IDCardReader.GetFPR2_Sex(arrTmp, ref nLen);
                            lblSex.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //国籍
                            IDCardReader.GetFPR2_Nation(arrTmp, ref nLen);
                            lblNation.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            IDCardReader.GetFPR2_NationCode(arrTmp, ref nLen);
                            lblNation.Text += "/" + System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //出生日期
                            IDCardReader.GetFPR2_Birthday(arrTmp, ref nLen);
                            lblBirthday.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //签发时间
                            IDCardReader.GetFPR2_ValidStartDate(arrTmp, ref nLen);
                            lblValidDate.Text = "";
                            lblValidDate.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            lblValidDate2.Text = "至  ";
                            //终止时间
                            IDCardReader.GetFPR2_ValidEndDate(arrTmp, ref nLen);
                            lblValidDate2.Text += System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //换证次数
                            IDCardReader.GetFPR2_RenewalTimes(arrTmp, ref nLen);
                            label12.Text += System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //照片
                            if (System.IO.File.Exists("ZP.bmp"))
                            {
                                pictureBox1.ImageLocation = Application.StartupPath + "\\zp.bmp";
                            }
                        }
                    }
                    else
                    {
                        lblCardType.Text = "中华人民共和国居民身份证";

                        lblCHNNameMeg.Visible = false;
                        lblCHNName.Visible = false;
                        lblAddress.Visible = true;
                        lblDept.Visible = true;
                        label6.Visible = true;
                        label7.Visible = true;
                        label5.Text = "身份证号：";
                        label2.Text = "民族：";
                        label4.Text = "出生日期：";

                        if (readContent == 0)
                        {
                            this.label10.Text = "读卡操作成功！" + "[" + nOK + "][" + nErr + "]";

                            byte[] arrTmp = new byte[100];
                            int nLen = 100;

                            IDCardReader.GetAddress(arrTmp, ref nLen);
                            lblAddress.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp,0, nLen);

                            IDCardReader.GetSex(arrTmp, ref nLen);
                            lblSex.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetBornDate(arrTmp, ref nLen);
                            lblBirthday.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetSignGov(arrTmp, ref nLen);
                            lblDept.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetIDNo(arrTmp, ref nLen);
                            lblIdCard.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetName(arrTmp, ref nLen);
                            lblName.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetNation(arrTmp, ref nLen);
                            lblNation.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);

                            IDCardReader.GetStartDate(arrTmp, ref nLen);
                            lblValidDate.Text = "";
                            lblValidDate.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            lblValidDate2.Text = "至  ";
                            IDCardReader.GetEndDate(arrTmp, ref nLen);
                            lblValidDate2.Text += System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp, 0, nLen);
                            //指纹信息
                            Boolean flag = false;
                            byte[] arrFinger = new byte[1024];
                            int fingerLen = 100;
                            flag = IDCardReader.GetFingerPrint(arrFinger, ref fingerLen);
                            if (flag)
                            {
                                label12.Text = "已获取指纹";
                            }
                            else
                            {
                                label12.Text = "无指纹信息";
                            }

                            //照片
                            if (System.IO.File.Exists("ZP.bmp"))
                            {
                                pictureBox1.ImageLocation = Application.StartupPath + "\\zp.bmp";
                            }
                        }
                        else
                        {
                            this.label10.Text = "读卡操作失败！";
                        }
                    }
                }
                else
                {
                    this.label10.Text = "请放身份证!";
                }
            }
            else
            {
                MessageBox.Show("初始化失败！");
            }
        }

        private void setting_Click_1(object sender, EventArgs e)
        {
            //端口判断
            int nPort = GetReaderPortNo();
            if (nPort == 0)
            {
                MessageBox.Show("请先设置读卡器端口");
                return;
            }

            //判断是否在运行,运行 先停止
            if (timer1.Enabled)
            {
                timer1.Stop();
                timer1.Dispose();
                btnSwitch.Text = "开始读卡";
                return;
            }            

            if (nPort != 0)
            {
                int isSuccess = IDCardReader.CloseComm();
                label9.Text = "";
                try
                {
                    nReaderPort = IDCardReader.InitComm((ReaderPortNum)nPort);
                    if (nReaderPort == 0)
                    {
                        byte[] tmp = new byte[64];
                        nReaderPort = IDCardReader.GetSAMID(tmp);
                        
                        if(nReaderPort != 0)
                        {
                            MessageBox.Show("初始化失败,请选择设备");
                            return;
                        }
                        this.label9.Text = "设备初始化成功！";
                        //开启请求
                        timer1.Start();
                        btnSwitch.Text = "停止读卡";
                        nErr = nOK = 0;
                    }
                    else
                    {
                        MessageBox.Show("初始化失败,请选择设备");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            
            
         }

        private void cmbReaderPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbPortNo.Items.Clear();
            string type = this.cmbReaderPort.Text;
            switch (type)
            {
                case "":
                    MessageBox.Show("请选择类型");
                    break;
                case "COM":
                    this.cmbPortNo.Items.AddRange(new object[] {
                    "COM-1",
                    "COM-2",
                    "COM-3",
                    "COM-4",
                    "COM-5","COM-6","COM-7","COM-8"});
                    this.cmbPortNo.SelectedIndex = 0;
                    break;
                case "USB":
                case "USB-HID":
                    this.cmbPortNo.Items.AddRange(new object[] {
                    "USB",
                    });
                    this.cmbPortNo.SelectedIndex = 0;
                    break;
                default:
                    break;
            }
        }

        private int GetReaderPortNo()
        {
            string type = cmbReaderPort.Text;
            switch (type)
            {
                case "":
                    return 0;
                case "COM":
                    string no = cmbPortNo.Text;
                    switch (no)
                    {
                        case "COM-1":    return 1;
                        case "COM-2":    return 2;
                        case "COM-3":    return 3;
                        case "COM-4":    return 4;
                        case "COM-5":    return 5;
                        case "COM-6": return 6;
                        case "COM-7": return 7;
                        case "COM-8": return 8;
                    }
                    break;
                case "USB":
                    return 1001;
                case "USB-HID":
                    return 2401;
                default:
                    break;
            }
            return 0;
        }        
    }
}