using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
namespace ReadCard
{
    public partial class Form1 : Form
    {
        static string GetName()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetName(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetSex()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetSex(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetNation()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetNation(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetBirthday()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsBirthday(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetAddress()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsAddress(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetStartTime()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetStartTime(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetEndTime()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetEndTime(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetIDCode()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetPeopleIDCode(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetDepartment()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetDepartment(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetGUID()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = ReadCardSDK.KsGetGuidInfo(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        static string GetIdCardImgPath()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[256];
            int dwRet = ReadCardSDK.KsGetIdCardImgPath(asciiBytes, 256);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        public Form1()
        {
            InitializeComponent();
        }

        bool isClose = false;
        private void Form1_Load(object sender, EventArgs e)
        {
            CleareData();
        }

        private void CleareData()
        {
            this.NameData.Text = "";
            this.SexData.Text = "";
            this.MinzuData.Text = "";
            this.YData.Text = "";
            this.MData.Text = "";
            this.DData.Text = "";
            this.AddressData.Text = "";
            this.startData.Text = "";
            this.JiguanData.Text = "";
            this.IDData.Text = "";
            this.ShowMsg.Text = "";
            this.simid.Text = "";
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void ReadDataOnce_Click(object sender, EventArgs e)
        {
                //CleareData();
                int ret = ReadCardSDK.KsReadIDCard();
                if (ret != 0)
                {
                    switch (ret)
	                {
                        case 0:
                            this.ShowMsg.Text = "身份证读取成功:0";
                            return;
                        case -1:
                            this.ShowMsg.Text = "读卡器连接失败,请连接读卡器:-1";
                            return;
                        case -2:
                            this.ShowMsg.Text = "云解码服务器连接异常,请重试:-2";
                            return;
                        case -3:
                            this.ShowMsg.Text = "云解码服务器异常,请重试:-3";
                            return;
                        case -4:
                            this.ShowMsg.Text = "身份证解析超时,请重试:-4";
                            return;
                        case -5:
                            this.ShowMsg.Text = "身份证最后一包数据异常,请重试:-5";
                            return;
                        case -6:
                            this.ShowMsg.Text = "接受服务器数据失败,请重试:-6";
                            return;
                        case -14:
                            this.ShowMsg.Text = "没有检测到数据,请重试:-14:";
                            return;
                        case -15:
                            this.ShowMsg.Text = "没有检测到SIMID数据,请重试:-15:";
                            return;
                        case -100:
                            this.ShowMsg.Text = "没有检测到网络,请开启网络:-100";
                            return;
                        case -200:
                            this.ShowMsg.Text = "服务器获取密钥异常,请重试:-200";
                            return;
                        case -300:
                            this.ShowMsg.Text = "没有找到指定的密钥服务器,请重试:-300";
                            return;
                        case -401:
                            this.ShowMsg.Text = "获取UID失败";
                            return;
                        case -402:
                            this.ShowMsg.Text = "申请内存不足";
                            return;
	                    default:
                            this.ShowMsg.Text = "失败";
                            return;
	                 }
                }

                pictureBox1.ImageLocation = GetIdCardImgPath();//Application.StartupPath + "\\1.bmp";
                this.NameData.Text = GetName();
                this.SexData.Text = GetSex();
                this.MinzuData.Text = GetNation();
                string birthday = GetBirthday();
                this.YData.Text = birthday.Substring(0, 4);//年
                this.MData.Text = birthday.Substring(4, 2);//月
                this.DData.Text = birthday.Substring(6, 2);//日
                this.AddressData.Text = GetAddress();
             //   this.YxData.Text = GetStartTime() + "-" + GetEndTime();
                this.startData.Text = GetStartTime();
                this.EndData.Text = GetEndTime();
                this.strguid.Text = GetGUID();
                this.JiguanData.Text = GetDepartment();
                this.IDData.Text = GetIDCode();
                this.ShowMsg.Text = "身份证读取成功";
          
        }

        private void EX_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int ret = ReadCardSDK.KsGetGuidInfo(asciiBytes, 100);
            if (ret != 0)
            {
                switch (ret)
                {
                    case 0:
                        this.ShowMsg.Text = "身份证读取成功:0";
                        return;
                    case -1:
                        this.ShowMsg.Text = "读卡器连接失败,请连接读卡器:-1";
                        return;
                    case -2:
                        this.ShowMsg.Text = "云解码服务器连接异常,请重试:-2";
                        return;
                    case -3:
                        this.ShowMsg.Text = "云解码服务器异常,请重试:-3";
                        return;
                    case -4:
                        this.ShowMsg.Text = "身份证解析超时,请重试:-4";
                        return;
                    case -5:
                        this.ShowMsg.Text = "身份证最后一包数据异常,请重试:-5";
                        return;
                    case -6:
                        this.ShowMsg.Text = "接受服务器数据失败,请重试:-6";
                        return;
                    case -14:
                        this.ShowMsg.Text = "没有检测到数据,请重试:-14:";
                        return;
                    case -15:
                        this.ShowMsg.Text = "没有检测到SIMID数据,请重试:-15:";
                        return;
                    case -100:
                        this.ShowMsg.Text = "没有检测到网络,请开启网络:-100";
                        return;
                    case -200:
                        this.ShowMsg.Text = "服务器获取密钥异常,请重试:-200";
                        return;
                    case -300:
                        this.ShowMsg.Text = "没有找到指定的密钥服务器,请重试:-300";
                        return;
                    case -401:
                        this.ShowMsg.Text = "获取UID失败";
                        return;
                    case -402:
                        this.ShowMsg.Text = "申请内存不足";
                        return;
                    default:
                        this.ShowMsg.Text = "失败";
                        return;
                }
            }
            else
            {
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, 100)];
                gb2312.GetChars(asciiBytes, 0, 100, asciiChars, 0);
                string strguid = new string(asciiChars);
                this.strguid.Text = strguid;
            }
           
            this.ShowMsg.Text = "读取UID成功";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.simid.Text = "";
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int ret = ReadCardSDK.KsGetSimIDInfo(asciiBytes, 100);
            if (ret != 0)
            {
                switch (ret)
                {
                    case 0:
                        this.ShowMsg.Text = "身份证读取成功:0";
                        return;
                    case -1:
                        this.ShowMsg.Text = "读卡器连接失败,请连接读卡器:-1";
                        return;
                    case -2:
                        this.ShowMsg.Text = "云解码服务器连接异常,请重试:-2";
                        return;
                    case -3:
                        this.ShowMsg.Text = "云解码服务器异常,请重试:-3";
                        return;
                    case -4:
                        this.ShowMsg.Text = "身份证解析超时,请重试:-4";
                        return;
                    case -5:
                        this.ShowMsg.Text = "身份证最后一包数据异常,请重试:-5";
                        return;
                    case -6:
                        this.ShowMsg.Text = "接受服务器数据失败,请重试:-6";
                        return;
                    case -14:
                        this.ShowMsg.Text = "没有检测到数据,请重试:-14:";
                        return;
                    case -15:
                        this.ShowMsg.Text = "没有检测到SIMID数据,请重试:-15:";
                        return;
                    case -100:
                        this.ShowMsg.Text = "没有检测到网络,请开启网络:-100";
                        return;
                    case -200:
                        this.ShowMsg.Text = "服务器获取密钥异常,请重试:-200";
                        return;
                    case -300:
                        this.ShowMsg.Text = "没有找到指定的密钥服务器,请重试:-300";
                        return;
                    case -401:
                        this.ShowMsg.Text = "获取UID失败";
                        return;
                    case -402:
                        this.ShowMsg.Text = "申请内存不足";
                        return;
                    default:
                        this.ShowMsg.Text = "失败";
                        return;
                }
            }
            else
            {
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, 100)];
                gb2312.GetChars(asciiBytes, 0, 100, asciiChars, 0);
                string simid = new string(asciiChars);
                this.simid.Text = simid;
            }

            this.ShowMsg.Text = "读取SIMID成功";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.icid.Text = "";
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int ret = ReadCardSDK.KsGetIcCardInfo(asciiBytes, 100);
            if (ret != 0)
            {
                switch (ret)
                {
                    case 0:
                        this.ShowMsg.Text = "身份证读取成功:0";
                        return;
                    case -1:
                        this.ShowMsg.Text = "读卡器连接失败,请连接读卡器:-1";
                        return;
                    case -2:
                        this.ShowMsg.Text = "云解码服务器连接异常,请重试:-2";
                        return;
                    case -3:
                        this.ShowMsg.Text = "云解码服务器异常,请重试:-3";
                        return;
                    case -4:
                        this.ShowMsg.Text = "身份证解析超时,请重试:-4";
                        return;
                    case -5:
                        this.ShowMsg.Text = "身份证最后一包数据异常,请重试:-5";
                        return;
                    case -6:
                        this.ShowMsg.Text = "接受服务器数据失败,请重试:-6";
                        return;
                    case -14:
                        this.ShowMsg.Text = "没有检测到数据,请重试:-14:";
                        return;
                    case -15:
                        this.ShowMsg.Text = "没有检测到SIMID数据,请重试:-15:";
                        return;
                    case -100:
                        this.ShowMsg.Text = "没有检测到网络,请开启网络:-100";
                        return;
                    case -200:
                        this.ShowMsg.Text = "服务器获取密钥异常,请重试:-200";
                        return;
                    case -300:
                        this.ShowMsg.Text = "没有找到指定的密钥服务器,请重试:-300";
                        return;
                    case -401:
                        this.ShowMsg.Text = "获取UID失败";
                        return;
                    case -402:
                        this.ShowMsg.Text = "申请内存不足";
                        return;
                    case -403:
                        this.ShowMsg.Text = "获取IC卡失败";
                        return;
                    case -404:
                        this.ShowMsg.Text = "识别为身份证不是IC卡";
                        return;
                    case -405:
                        this.ShowMsg.Text = "未知卡";
                        return;
                    default:
                        this.ShowMsg.Text = "失败";
                        return;
                }
            }
            else
            {
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, 100)];
                gb2312.GetChars(asciiBytes, 0, 100, asciiChars, 0);
                string icid = new string(asciiChars);
                this.icid.Text = icid;
            }

            this.ShowMsg.Text = "读取SIMID成功";
        }
    }
}
