// test1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "test.h"
#include "protocol.h"
#include "fpengine.h"
#include "fpdevice.h"
#include <commctrl.h>
#include <strsafe.h>


#include "MfcFile.h"


#ifdef _UNICODE
	#if defined _M_IX86
		#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
	#elif defined _M_X64
		#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
	#else
		#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
	#endif
#endif

HINSTANCE	hInst;			// The current instance
BYTE		RefBuf[512];
int			RefSize = 0;
BYTE		MatBuf[512];
int			MatSize = 0;
int			g_count = 1;

LARGE_INTEGER	g_PerCounter;
LONGLONG		g_QPartBegin;
LONGLONG		g_QPartEnd;
double			g_Minus;
double			g_Freq;
double			g_Timnow;



LRESULT CALLBACK	MainDlg(HWND, UINT, WPARAM, LPARAM);
VOID WINAPI DoCommand(HWND, UINT, WPARAM, LPARAM);
VOID WINAPI DoFPCallBack(HWND hDlg,int message,int result);

void GetSCTImage(HWND hDlg);

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR    lpCmdLine, int       nCmdShow)
{
	return (int)DialogBox(hInstance, (LPCTSTR)IDD_MAINDLG, NULL, (DLGPROC)MainDlg);;
}

BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
{
	DWORD dwMinSize;
	dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
	if (dwSize < dwMinSize)
	{
		return FALSE;
	}
	WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwSize, NULL, FALSE);
	return TRUE;
}

// Mesage handler for the About box.
LRESULT CALLBACK MainDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		{
			SetMsgMainHandle(hDlg);
			//SetTimer(hDlg, 1, 100, NULL);
			SetDlgItemInt(hDlg, IDC_EDIT1, 0,FALSE);
			SetDlgItemText(hDlg, IDC_EDIT4, _T("User define data,Max 32KByte"));
		}
	return TRUE;
	case WM_NCLMESSAGE:	
		DoFPCallBack(hDlg,wParam, lParam);
		return TRUE;
	case WM_TIMER:
		//DoFPCallBack(hDlg, GetWorkMsg(), GetRetMsg());
		break;
	case WM_COMMAND:
		if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		else
		{
			DoCommand(hDlg, message, wParam,lParam);
			
		}
		break;
	}
	return FALSE;
}


VOID WINAPI DoCommand(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (LOWORD(wParam))
	{
	case IDC_OPENDEVICE:
	{
		CloseDevice();
		if (OpenDevice(0, 0, 0) == RET_OK)
		{
			if (LinkDevice(0) == RET_OK)
			{
				SetDlgItemText(hDlg, IDC_STATUS, _T("Open Device Ok"));

				TCHAR sntxt[16];
				_stprintf(sntxt, _T("%0.8X"), GetDeviceModel());
				SetDlgItemText(hDlg, IDC_LABEL1, sntxt);

				_stprintf(sntxt, _T("%0.8X"), GetDeviceSnNum());
				SetDlgItemText(hDlg, IDC_LABEL2, sntxt);

				BYTE userdata[32];
				WriteUserInfo(userdata, 32);
				ReadUserInfo(userdata, 32);

				

				if (IsSupportLcd())
					SetDeviceSystemDateTime();
			}
			else
			{
				SetDlgItemText(hDlg, IDC_STATUS, _T("Link Device Fail"));
			}
		}
		else
		{
			SetDlgItemText(hDlg, IDC_STATUS, _T("Open Device Fail"));
		}
	}
	break;
	case IDC_CLOSEDEVICE:
		CloseDevice();
		break;
	case IDC_ENROLINHOST:
		EnrollTemplate();
		break;
	case IDC_CAPTUREINHOST:
		CaptureTemplate();		
		break;
	case IDC_SAVEIMAGE:
		{
			CFile file;
			//WSQ 		
			BYTE rawdata[92160];
			BYTE wsqdata[92160];
			int rawsize = 0;
			int wsqsize = 0;
			int imgwidth, imgheight;
			GetImageSize(&imgwidth, &imgheight);
			GetRawImage(rawdata, &rawsize);
			RawToWsq(rawdata, imgwidth, imgheight, 8, 500, 2.0f, wsqdata, &wsqsize);

			file.Create(_T(".\\fp.wsq"));
			file.Write(wsqdata, wsqsize);
			file.Close();

			//BMP
			BYTE bmpdata[93238];
			int bmpsize = 0;
			GetBmpImage(bmpdata,&bmpsize);
			
			file.Create(_T(".\\fp.bmp"));
			file.Write(bmpdata, bmpsize);
			file.Close();
		}
		break;
	}
}

VOID WINAPI DoFPCallBack(HWND hDlg,int message, int result)
{
	switch (message)
	{
	case FPM_DEVICE:
		SetDlgItemText(hDlg, IDC_STATUS, _T("Please Open Device"));
		break;
	case FPM_PLACE:
		SetDlgItemText(hDlg, IDC_STATUS, _T("Place Finger"));
		break;
	case FPM_LIFT:
		SetDlgItemText(hDlg, IDC_STATUS, _T("Lift Finger"));
		break;
	case FPM_TIMEOUT:
		SetDlgItemText(hDlg, IDC_STATUS, _T("Time Out"));
		break;
	case FPM_NEWIMAGE:
	{
		RECT rc;
		HWND hg = GetDlgItem(hDlg, IDC_FPIMAGE);
		GetWindowRect(hg, &rc);
		POINT pt;
		pt.x = rc.left;
		pt.y = rc.top;
		ScreenToClient(hDlg, &pt);
		int imgwidth, imgheight;
		GetImageSize(&imgwidth, &imgheight);
		DrawImage(GetDC(hDlg), pt.x + (rc.right - rc.left - imgwidth) / 2, pt.y + (rc.bottom - rc.top - imgheight) / 2);
		
	}
	break;
	case FPM_ENRFPT:
	{
		if (result == 1)
		{
			SetDlgItemText(hDlg, IDC_STATUS, _T("Enrol Ok"));
			GetTemplateByEnl(RefBuf, &RefSize);

			/*
			CFile file;
			file.Create(_T("ref.dat"));
			file.Write(RefBuf, RefSize);
			file.Close();
			*/
		}
		else
		{
			SetDlgItemText(hDlg, IDC_STATUS, _T("Enrol Fail"));
		}
	}
	break;
	case FPM_GENCHAR:
	{
		if (result == 1)
		{
			GetTemplateByCap(MatBuf, &MatSize);
			if (RefSize > 0 && MatSize > 0)
			{
				int ret = MatchTemplateOne(MatBuf, RefBuf, RefSize);
				TCHAR txt[32];
				_stprintf(txt, _T("Match Result:%d"), ret);
				SetDlgItemText(hDlg, IDC_STATUS, txt);
				/*
				if (ret >= 80)
					SetDlgItemText(hDlg, IDC_STATUS, _T("Match Succeed"));
				else
					SetDlgItemText(hDlg, IDC_STATUS, _T("Match Fail"));
				*/
			}
			else
			{
				SetDlgItemText(hDlg, IDC_STATUS, _T("Capture Ok"));
			}

			/*
			int imgwidth, imgheight;
			GetImageSize(&imgwidth, &imgheight);

			BYTE* imgdata=new BYTE[imgwidth * imgheight];
			int imgsize = 0;
			BYTE *wsqdata=new BYTE[imgwidth * imgheight];
			int wsqsize = 0;
			GetRawImage(imgdata, &imgsize);
			RawToWsq(imgdata, imgwidth, imgheight, 8, 500, 2.833755, wsqdata, &wsqsize);

			CFile file;
			file.Create(_T(".\\fp.wsq"));
			file.Write(wsqdata, wsqsize);
			file.Close();

			//CFile file;
			file.Create(_T(".\\template.dat"));
			file.Write(MatBuf,MatSize);
			file.Close();

			BYTE isotp[378];
			StdToIso(2, MatBuf, isotp);
			file.Create(_T(".\\iso.dat"));
			file.Write(isotp,378);
			file.Close();
			*/
		}
		else
		{
			SetDlgItemText(hDlg, IDC_STATUS, _T("Capture Fail"));
		}
	}
	break;
	}
}
