using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace csdemo
{
    public partial class Form1 : Form
    {

        //Bytes
        private int refsize;
        private byte[] refbuf;
        private int matsize;
        private byte[] matbuf;

        //Strings
        private byte[] refstr;
        private byte[] matstr;


        public Form1()
        {
            InitializeComponent();

            
            refsize = 0;
            refbuf = new byte[512];
            matsize = 0;
            matbuf = new byte[256];

            
            refstr = new Byte[768]; 
            matstr = new Byte[384];
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fpengine.CloseDevice();
            if(fpengine.OpenDevice(0, 0, 0)==1)
            {
                if(fpengine.LinkDevice(0)==1)
                {
                    label1.Text = "Open Device OK!";

                }
                else
                {
                    label1.Text="Link Device Fail";
                }
            }
            else
            {
                label1.Text="Open Device Fail";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fpengine.EnrollTemplate();
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            fpengine.CaptureTemplate();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int wm=fpengine.GetWorkMsg();
            int rm = fpengine.GetRetMsg();
            switch(wm)
            {
                case fpengine.FPM_DEVICE:
                    label1.Text="Not Open Device";
                    break;
                case fpengine.FPM_PLACE:
                    label1.Text="Place Finger ...";
                    break;
                case fpengine.FPM_LIFT:
                    label1.Text="Lift Finger ...";
                    break;
                case fpengine.FPM_CAPTURE:
                    break;
                case fpengine.FPM_ENROLL: 
                    {
                        if (rm == 1)
                        {                            
                            label1.Text = "Enrol OK";
                            if (checkBox1.Checked)
                            {
                                fpengine.GetBase64StrByEnl(0,refstr);
                                string un = System.Text.Encoding.Default.GetString(refstr);
                                textBox1.Text = un;
                            }
                            else
                            {
                                fpengine.GetTemplateByEnl(refbuf, ref refsize);
                            }
                        }
                        else
                        {
                            label1.Text = "Enrol Fail";
                        }
                        timer1.Enabled = false;
                    }
                    break;
                case fpengine.FPM_GENCHAR:    
                    {
                        if(rm==1)
                        {
                            label1.Text="Capture OK";
                            if (checkBox1.Checked)
                            {
                                fpengine.GetBase64StrByCap(0,matstr);
                                string un = System.Text.Encoding.Default.GetString(matstr);
                                textBox2.Text = un;
                            }
                            else
                            {
                                fpengine.GetTemplateByCap(matbuf, ref matsize);
                            }
                            if (textBox1.TextLength > 0 || refsize > 0)
                            {
                                int scope = 0;
                                if(checkBox1.Checked)
                                {
                                    if(textBox1.TextLength > 0)
                                    {
                                        refstr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
                                        matstr = System.Text.Encoding.Default.GetBytes(textBox2.Text);
                                        scope = fpengine.MatchBase64Str(matstr, refstr,0);                                           
                                    }                                    
                                }
                                else
                                {
                                    if (refsize > 0)
                                    {
                                        scope = fpengine.MatchTemplateOne(matbuf, refbuf,512);                                        
                                    }                                    
                                }                                
                                if(scope>=80)
                                {
                                    label1.Text = "Match OK::" + scope.ToString();  
                                }
                                else
                                {
                                    label1.Text = "Match Fail:"+ scope.ToString();  
                                }                                
                            }
                        }
                        else
                        {
                            label1.Text = "Capture Fail";
                        }
                        timer1.Enabled = false;
                    }                    
                    break;
                case fpengine.FPM_NEWIMAGE:		
                    {
                        System.Drawing.Bitmap bmp = new Bitmap(255, 288);
                        Graphics g = Graphics.FromImage(bmp);
                        fpengine.DrawImage(g.GetHdc(), 0, 0);
                        g.Dispose();
                        pictureBox1.Image = bmp;
                    }
                    break;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            fpengine.CloseDevice();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            /* Deivce SN

            //UInt32 sn =fpengine.GetDeviceSnNum();
            
            byte[] devsn = new byte[32];
            fpengine.GetDeviceSnStr(0, devsn);
            string devsnstr = System.Text.Encoding.Default.GetString(devsn);
            MessageBox.Show(devsnstr);


            byte[] wus = new byte[32];
            byte[] rus = new byte[32];
            wus[0] = 0x31;
            wus[1] = 0x32;
            wus[2] = 0x33;
            wus[3] = 0x34;
            wus[4] = 0x35;
            wus[5] = 0x36;
            fpengine.WriteUserInfo(wus, 32);
            fpengine.ReadUserInfo(rus, 32);
            string usstr = System.Text.Encoding.Default.GetString(rus);
            MessageBox.Show(usstr);
            */

            //WSQ
            byte[] rawdata = new byte[92160];
            byte[] wsqdata = new byte[92160];
            byte[] bmpdata = new byte[92160 + 1078];
            int wsqsize = 0;
            int rawsize = 0;
            int imgwidth = 0;
            int imgheight = 0;
            fpengine.GetImageSize(ref imgwidth, ref imgheight);
            fpengine.GetRawImage(rawdata, ref rawsize);
            fpengine.RawToWsq(rawdata, imgwidth, imgheight, 8, 500, 2.833755f, wsqdata, ref wsqsize);

            FileStream fswsq = new FileStream(".\\test.wsq", FileMode.Create);
            fswsq.Write(wsqdata, 0, wsqsize);
            fswsq.Close();

            /*
            int width = 0, height = 0, depth = 0, dpi = 0;
            fpengine.WsqToRaw(wsqdata, wsqsize, rawdata, ref width, ref height, ref depth, ref dpi);

            FileStream fsraw = new FileStream(".\\test.raw", FileMode.Create);
            fsraw.Write(rawdata, 0, width * height);
            fsraw.Close();

            fpengine.WsqToBmp(wsqdata, wsqsize, bmpdata, ref width, ref height, ref depth, ref dpi);

            FileStream fsbmp = new FileStream(".\\test.bmp", FileMode.Create);
            fsbmp.Write(bmpdata, 0, width * height + 1078);
            fsbmp.Close();

            fpengine.BmpToWsq(bmpdata, 256, 288, 8, 500, 2.833755f, wsqdata, ref wsqsize);
            FileStream fswsq2 = new FileStream(".\\test2.wsq", FileMode.Create);
            fswsq2.Write(wsqdata, 0, wsqsize);
            fswsq2.Close();
            */

        }

        private void button4_Click(object sender, EventArgs e)
        {
                int scope = 0;
            if (checkBox1.Checked)
            {
                /*
                refstr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
                matstr = System.Text.Encoding.Default.GetBytes(textBox2.Text);
                scope = fpengine.MatchBase64Str(matstr, refstr, 0); //refstr size =512
                */

                refstr = Convert.FromBase64String(textBox1.Text);
                matstr = Convert.FromBase64String(textBox2.Text);
                scope = fpengine.MatchTemplate(matstr, refstr);

                if (scope >= 80)
                {
                    label1.Text = "Match OK::" + scope.ToString();
                }
                else
                {
                    label1.Text = "Match Fail:" + scope.ToString();
                }
            }

        }
    }
}

