// UCBioBSP_ExportDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UCBioBSP_ExportDemo.h"
#include "UCBioBSP_ExportDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUCBioBSP_ExportDemoDlg dialog

CUCBioBSP_ExportDemoDlg::CUCBioBSP_ExportDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUCBioBSP_ExportDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUCBioBSP_ExportDemoDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	m_hUCBioBSP = UCBioAPI_INVALID_HANDLE;
}

void CUCBioBSP_ExportDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUCBioBSP_ExportDemoDlg)
	DDX_Control(pDX, IDC_RADIO_IMPORT_ISO600, m_radioImportISO600);
	DDX_Control(pDX, IDC_RADIO_IMPORT_ISO500, m_radioImportISO500);
	DDX_Control(pDX, IDC_RADIO_IMPORT_400, m_radioImport400);
	DDX_Control(pDX, IDC_RADIO_IMPORT_800, m_radioImport800);
	DDX_Control(pDX, IDC_RADIO_IMPORT_320, m_radioImport320);
	DDX_Control(pDX, IDC_RADIO_IMPORT_256, m_radioImport256);
	DDX_Control(pDX, IDC_RADIO_EXPORT_ISO600, m_radioExportISO600);
	DDX_Control(pDX, IDC_RADIO_EXPORT_ISO500, m_radioExportISO500);
	DDX_Control(pDX, IDC_RADIO_EXPORT_400, m_radioExport400);
	DDX_Control(pDX, IDC_RADIO_EXPORT_800, m_radioExport800);
	DDX_Control(pDX, IDC_RADIO_EXPORT_320, m_radioExport320);
	DDX_Control(pDX, IDC_RADIO_EXPORT_256, m_radioExport256);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUCBioBSP_ExportDemoDlg, CDialog)
//{{AFX_MSG_MAP(CUCBioBSP_ExportDemoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_EXPORT, OnButtonExport)
ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUCBioBSP_ExportDemoDlg message handlers

BOOL CUCBioBSP_ExportDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	UCBioAPI_RETURN ret = UCBioAPI_Init(&m_hUCBioBSP);
	if (ret == UCBioAPIERROR_NONE) {
		UCBioAPI_VERSION ver;
		ret = UCBioAPI_GetVersion(m_hUCBioBSP, &ver);
		if (ret == UCBioAPIERROR_NONE) {
			CString szMsg;
			szMsg.Format(_T("UCBioBSP_ExportDemo - BSP Version : v%d.%04d"), ver.Major, ver.Minor);
			SetWindowText(szMsg);
		}
	}
	
	m_radioExport400.SetCheck(TRUE);
	m_radioImport400.SetCheck(TRUE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUCBioBSP_ExportDemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	if (m_hUCBioBSP) {
		UCBioAPI_Terminate(m_hUCBioBSP);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CUCBioBSP_ExportDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CUCBioBSP_ExportDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUCBioBSP_ExportDemoDlg::OnButtonExport() 
{
	UCBioAPI_TEMPLATE_TYPE nType;
	
	if (m_radioExport800.GetCheck())
	{
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE800;
	}
	else if (m_radioExport320.GetCheck())
	{
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE320;
	}
	else if (m_radioExport256.GetCheck())
	{
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE256;
	}
	else if (m_radioExportISO500.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE500;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_ISO500);
	}
	else if (m_radioExportISO600.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE600;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_ISO600);
	}
	else
	{
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE400;
	}
	
	//   nType = UCBioAPI_TEMPLATE_TYPE_SIZE600;
	
	UCBioAPI_RETURN err;
	
	err = UCBioAPI_OpenDevice(m_hUCBioBSP, UCBioAPI_DEVICE_ID_AUTO);
	if (err == UCBioAPIERROR_NONE) {
		UCBioAPI_FIR_HANDLE hFIR;
		err = UCBioAPI_Capture(m_hUCBioBSP, 
			UCBioAPI_FIR_PURPOSE_VERIFY, 
			&hFIR, 
			UCBioAPI_USE_DEFAULT_TIMEOUT,
			NULL, NULL);
		
		if (err == UCBioAPIERROR_NONE) {
			UCBioAPI_INPUT_FIR inputFIR;
			inputFIR.Form = UCBioAPI_FIR_FORM_HANDLE;
			inputFIR.InputFIR.FIRinBSP = &hFIR;
			
			UCBioAPI_EXPORT_DATA exportData;
			err = UCBioAPI_FIRToTemplate(m_hUCBioBSP, &inputFIR, &exportData, nType);
			if (err == UCBioAPIERROR_NONE) {
				CFileDialog dlgFile(FALSE, _T("Template.uct"), _T("*.uct"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					_T("Template File|*.uct|All Files (*.*)|*.*||"));
				
				UINT nID = dlgFile.DoModal();
				if (nID == IDOK) {
					CString szPath = dlgFile.GetPathName();
					
					FILE* fp = _tfopen(szPath, _T("wb"));
					if (fp) {
						fwrite(exportData.FingerInfo[0].TemplateInfo[0].Data, 1, exportData.FingerInfo[0].TemplateInfo[0].Length, fp);
						fclose(fp);
						AfxMessageBox(_T("Succeeded to save template!"));
					} else {
						AfxMessageBox(_T("Failed to open file!"), MB_ICONERROR);
					}
				}
				
				UCBioAPI_FreeExportData(&exportData);
			}
			
			UCBioAPI_FreeFIRHandle(hFIR);
		}
		
		UCBioAPI_CloseDevice(m_hUCBioBSP, UCBioAPI_DEVICE_ID_AUTO);
	}
	
	if (err != UCBioAPIERROR_NONE) {
		CString szErr;
		szErr.Format(_T("Error code : %d(0x%x)"), err, err);
		AfxMessageBox(szErr, MB_ICONERROR);
	}
}

void CUCBioBSP_ExportDemoDlg::OnButtonImport() 
{
	UCBioAPI_TEMPLATE_TYPE nType;
	
	if (m_radioImport800.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE800;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
	}
	else if (m_radioImport320.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE320;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
	}
	else if (m_radioImport256.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE256;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
	}
	else if (m_radioImportISO500.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE500;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_ISO500);
	}
	else if (m_radioImportISO600.GetCheck())
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE600;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_ISO600);
	}
	else
	{
		nType = UCBioAPI_TEMPLATE_TYPE_SIZE400;
		UCBioAPI_SetTemplateFormat(m_hUCBioBSP, UCBioAPI_TEMPLATE_FORMAT_UNION400);
	}
	
	UCBioAPI_RETURN err = 0;
	
	CFileDialog dlgFile(TRUE, _T("Template.uct"), _T("*.uct"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("Template File|*.uct|All Files (*.*)|*.*||"));
	
	UINT nID = dlgFile.DoModal();
	if (nID == IDOK) {
		CString szPath = dlgFile.GetPathName();
		
		FILE* fp = _tfopen(szPath, _T("rb"));
		if (fp) {
			fseek(fp, 0L, SEEK_END);
			long len = ftell(fp);
			fseek(fp, 0L, SEEK_SET);
			
			UCBioAPI_UINT8* pTemplate = new UCBioAPI_UINT8 [len];
			fread(pTemplate, 1, len, fp);
			fclose(fp);
			
			UCBioAPI_FIR_HANDLE hFIR;
			err = UCBioAPI_TemplateToFIR(m_hUCBioBSP, pTemplate, len, nType, UCBioAPI_FIR_PURPOSE_VERIFY, &hFIR);
			if (err == UCBioAPIERROR_NONE) {
				err = UCBioAPI_OpenDevice(m_hUCBioBSP, UCBioAPI_DEVICE_ID_AUTO);
				if (err == UCBioAPIERROR_NONE) {
					UCBioAPI_INPUT_FIR inputFIR;
					inputFIR.Form = UCBioAPI_FIR_FORM_HANDLE;
					inputFIR.InputFIR.FIRinBSP = &hFIR;
					
					UCBioAPI_BOOL bResult;
					err = UCBioAPI_Verify(m_hUCBioBSP, &inputFIR, &bResult, NULL, UCBioAPI_USE_DEFAULT_TIMEOUT, NULL, NULL);
					if (err == UCBioAPIERROR_NONE) {
						if (bResult)
							AfxMessageBox(_T("Succeeded to verify!"));
						else
							AfxMessageBox(_T("Failed to verify!"));
					}
					
					UCBioAPI_CloseDevice(m_hUCBioBSP, UCBioAPI_DEVICE_ID_AUTO);
				}
				
				UCBioAPI_FreeFIRHandle(hFIR);
			}
			
			if (pTemplate)
				delete[] pTemplate;
			
		} else {
			AfxMessageBox(_T("Failed to open file!"), MB_ICONERROR);
		}
	}
	
	if (err != UCBioAPIERROR_NONE) {
		CString szErr;
		szErr.Format(_T("Error code : %d(0x%x)"), err, err);
		AfxMessageBox(szErr, MB_ICONERROR);
	}
}

