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

using UNIONCOMM.SDK.UCBioBSP;

namespace UCBioBSP.NET_DemoCS
{
    public partial class Form1 : Form
    {
        UCBioAPI m_UCBioAPI;

        UCBioAPI.Type.INIT_INFO_0 m_InitInfo0;

        short m_OpenedDeviceID;
        UCBioAPI.Type.DEVICE_INFO_EX[] m_DeviceInfoEx;

        UCBioAPI.Type.HFIR m_hEnrolledFIR;

        public Form1()
        {
            InitializeComponent();

            m_UCBioAPI = new UCBioAPI();

            m_OpenedDeviceID = UCBioAPI.Type.DEVICE_ID.NONE;
            m_hEnrolledFIR = null;
        }

        private void DisplayErrorMsg(string msg, uint ret)
        {
            textStatus.Text = msg + UCBioAPI.Error.GetErrorDescription(ret) + " [" + ret.ToString() + "]";
        }

        private void UpdateButtonStatus()
        {
            btnRemove.Enabled = (m_hEnrolledFIR != null);
            btnVerify.Enabled = (m_hEnrolledFIR != null);

            if (m_hEnrolledFIR == null)
                btnEnroll.Text = "Enroll";
            else
                btnEnroll.Text = "Modify";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UCBioAPI.Type.VERSION version = new UCBioAPI.Type.VERSION();
            m_UCBioAPI.GetVersion(out version);

            Text = String.Format("UCBioBSP.NET Demo for C# (BSP Version : v{0}.{1:D4})", version.Major, version.Minor);

            // Enumerate Device
            int i;
            uint nNumDevice;
            short[] nDeviceID;
            uint ret = m_UCBioAPI.EnumerateDevice(out nNumDevice, out nDeviceID, out m_DeviceInfoEx);
            DisplayErrorMsg("", ret);

            comboDevice.Items.Add("Auto_Detect");
            for (i = 0; i < nNumDevice; i++)
            {
                comboDevice.Items.Add(m_DeviceInfoEx[i].Name + " - " + m_DeviceInfoEx[i].Description);
            }

            // Set default value
            comboDevice.SelectedIndex = 0;

            UpdateButtonStatus();

            btnRefresh_Click("", new System.EventArgs());
            comboDevice_SelectedIndexChanged("", new System.EventArgs());
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            uint ret = m_UCBioAPI.GetInitInfo(out m_InitInfo0);
            if (ret == UCBioAPI.Error.NONE)
            {
                textMaxFinger.Text = m_InitInfo0.MaxFingersForEnroll.ToString();
                textNecessaryNum.Text = m_InitInfo0.NecessaryEnrollNum.ToString();
                textSamplesPerFinger.Text = m_InitInfo0.SamplesPerFinger.ToString();
                textDefaultTimeout.Text = m_InitInfo0.DefaultTimeout.ToString();
                textSecuLevelEnroll.Text = m_InitInfo0.SecurityLevelForEnroll.ToString();
                textSecuLevelVerify.Text = m_InitInfo0.SecurityLevelForVerify.ToString();
                textSecuLevelIdentify.Text = m_InitInfo0.SecurityLevelForIdentify.ToString();

                DisplayErrorMsg("Success to get init information! - ", ret);
            }
            else
                DisplayErrorMsg("Failed to get init information! - ", ret);
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            m_InitInfo0.StructureType = 0;
            m_InitInfo0.MaxFingersForEnroll = Convert.ToUInt32(textMaxFinger.Text);
            m_InitInfo0.NecessaryEnrollNum = Convert.ToUInt32(textNecessaryNum.Text);
            m_InitInfo0.SamplesPerFinger = Convert.ToUInt32(textSamplesPerFinger.Text);
            m_InitInfo0.DefaultTimeout = Convert.ToUInt32(textDefaultTimeout.Text);
            m_InitInfo0.SecurityLevelForEnroll = Convert.ToUInt32(textSecuLevelEnroll.Text);
            m_InitInfo0.SecurityLevelForVerify = Convert.ToUInt32(textSecuLevelVerify.Text);
            m_InitInfo0.SecurityLevelForIdentify = Convert.ToUInt32(textSecuLevelIdentify.Text);

            uint ret = m_UCBioAPI.SetInitInfo(m_InitInfo0);
            if (ret == UCBioAPI.Error.NONE)
                DisplayErrorMsg("Success to set init information! - ", ret);
            else
                DisplayErrorMsg("Failed to set init information! - ", ret);
        }

        private void comboDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            short iDeviceID = UCBioAPI.Type.DEVICE_ID.AUTO;

            // Select device type
            if (comboDevice.SelectedIndex > 0)
                iDeviceID = (short)(m_DeviceInfoEx[comboDevice.SelectedIndex - 1].NameID + (m_DeviceInfoEx[comboDevice.SelectedIndex - 1].Instance << 8));
            else
                iDeviceID = UCBioAPI.Type.DEVICE_ID.AUTO;

            // Close Device if before opened
            m_UCBioAPI.CloseDevice(m_OpenedDeviceID);

            // Open device
            uint ret = m_UCBioAPI.OpenDevice(iDeviceID);
            if (ret == UCBioAPI.Error.NONE)
            {
                m_OpenedDeviceID = iDeviceID;
                btnEnroll.Enabled = true;
                textStatus.Text = "Device Open Success! - ";

                if (comboDevice.SelectedIndex > 0)
                    textStatus.Text += m_DeviceInfoEx[comboDevice.SelectedIndex - 1].Description;
                else
                    textStatus.Text += "Auto Detect Device";

                UCBioAPI.Type.DEVICE_INFO_0 deviceInfo0;
                ret = m_UCBioAPI.GetDeviceInfo(iDeviceID, out deviceInfo0);
                if (ret == UCBioAPI.Error.NONE)
                {
                    textWidth.Text = deviceInfo0.ImageWidth.ToString();
                    textHeight.Text = deviceInfo0.ImageHeight.ToString();
                }

            }
            else
            {
                btnEnroll.Enabled = false;
                btnRemove.Enabled = false;
                DisplayErrorMsg("Device Open Failed! - ", ret);
            }

            UpdateButtonStatus();
        }

        private void btnEnroll_Click(object sender, EventArgs e)
        {
            UCBioAPI.Type.FIR_PAYLOAD myPayload = new UCBioAPI.Type.FIR_PAYLOAD();
            myPayload.Data = textPayload.Text;

            UCBioAPI.Type.HFIR hNewFIR;

            if (chbAutoDetect.Checked)
            {
                m_UCBioAPI.SetAutoDetect(1);
            }
            else
            {
                m_UCBioAPI.SetAutoDetect(0);
            }

            uint ret = m_UCBioAPI.Enroll(ref m_hEnrolledFIR, out hNewFIR, myPayload, UCBioAPI.Type.TIMEOUT.DEFAULT, null, null);
            if (ret == UCBioAPI.Error.NONE)
            {
                // If you want to save fir data then this FIR data write to DB or File.
                m_hEnrolledFIR = hNewFIR;
                textStatus.Text = "Enroll Function Success!";
            }
            else
            {
                DisplayErrorMsg("Enroll Function Failed! - ", ret);
            }

            UpdateButtonStatus();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            m_hEnrolledFIR = null;
            UpdateButtonStatus();
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            // Check exist enrolled fingerprint
            if (m_hEnrolledFIR == null)
            {
                MessageBox.Show("Cannot find enrolled fingerprint!");
                return;
            }

            UCBioAPI.Type.FIR biFIR;
            UCBioAPI.Type.FIR_TEXTENCODE textFIR;

            // Get binary encoded FIR data
            m_UCBioAPI.GetFIRFromHandle(m_hEnrolledFIR, out biFIR);

            // Get text encoded FIR data
            m_UCBioAPI.GetTextFIRFromHandle(m_hEnrolledFIR, out textFIR, true);

            uint ret;
            bool result;
            UCBioAPI.Type.FIR_PAYLOAD myPayload = new UCBioAPI.Type.FIR_PAYLOAD();


            // Verify
            if (radioBinary.Checked)
                ret = m_UCBioAPI.Verify(biFIR, out result, myPayload);			// Verify with binary FIR
            else if (radioText.Checked)
                ret = m_UCBioAPI.Verify(textFIR, out result, myPayload);	// Verify with text encoded FIR
            else
                ret = m_UCBioAPI.Verify(m_hEnrolledFIR, out result, myPayload);

            if (ret != UCBioAPI.Error.NONE)
            {
                DisplayErrorMsg("Verify Function Failed! - ", ret);
                return;
            }

            // Check result of verify
            if (result)
            {
                // Check payload
                if (myPayload.Data != null)
                {
                    MessageBox.Show("Verify Success! [Payload: " + myPayload.Data + "]", "BSP Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textPayload.Text = myPayload.Data;
                }
                else
                {
                    MessageBox.Show("Verify Success!", "BSP Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                textStatus.Text = "Verify Success!";
            }
            else
            {
                // Show fail message.
                MessageBox.Show("Verify Failed!", "BSP Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textStatus.Text = "Verify Failed!";
            }
        }

    }
}