// SettingsDialog.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "SettingsDialog.h"
#include "FncExtractor.h"
#include "FncMatcher.h"

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

inline int Round1(double value) { return (int)(value < 0 ? value - 0.5 : value + 0.5); }

#define FARToMatchingThreshold(val, threshold)\
{\
	if(val > 100.0) val = 100.0;\
	else if(val<= 0.0) val = 1E-100;\
	threshold = Round1((-log10(val) + 2) * 12);\
}
/////////////////////////////////////////////////////////////////////////////
// CSettingsDialog dialog

IMPLEMENT_DYNAMIC(CSettingsDialog, CDialog)

CSettingsDialog::CSettingsDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CSettingsDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSettingsDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	DefaultValues();
}


void CSettingsDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSettingsDialog)
	DDX_Text(pDX, IDC_DPI, m_DPI);
	DDV_MinMaxInt(pDX, m_DPI, 100, 1000);
	DDX_CBIndex(pDX, IDC_MODE, m_mode);
	DDX_CBIndex(pDX, IDC_MODE_MATCHER , m_mode_m);
	DDX_CBIndex(pDX, IDC_GFAR, m_gfar);
	DDX_CBIndex(pDX, IDC_FAR, m_mfar);
	DDX_Text(pDX, IDC_MROTATION, m_rotation);
	DDV_MinMaxInt(pDX, m_rotation, 0, 360);
	DDX_Text(pDX, IDC_QUALITY, m_qualiti);
	DDV_MinMaxInt(pDX, m_qualiti, 0, 100);
	DDX_Check(pDX, IDC_FULLDB, m_fullsearch);
	DDX_Check(pDX, IDC_USE_QUALITY, m_use_quality);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSettingsDialog, CDialog)
	//{{AFX_MSG_MAP(CSettingsDialog)
	ON_BN_CLICKED(IDDEFAULT, OnDefault)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDCANCEL, &CSettingsDialog::OnBnClickedCancel)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSettingsDialog message handlers


void CSettingsDialog::OnDefault() 
{
	DefaultValues();
	UpdateData(FALSE);
}

void CSettingsDialog::SaveSetings()
{
	setingsBackup[0] = m_DPI;
	setingsBackup[1] = m_mode;
	setingsBackup[2] = m_mode_m;
	setingsBackup[3] = m_fextraction;
	setingsBackup[4] = m_gfar;
	setingsBackup[5] = m_mfar;
	setingsBackup[6] = m_fmatching;
	setingsBackup[7] = m_rotation;
	setingsBackup[8] = m_fullsearch;
	setingsBackup[9] = m_qualiti;
	setingsBackup[10]= m_use_quality;
}

void CSettingsDialog::LoadSetings()
{
	m_DPI = setingsBackup[0];
	m_mode = setingsBackup[1];
	m_mode_m = setingsBackup[2];
	m_fextraction = setingsBackup[3];
	m_gfar = setingsBackup[4];
	m_mfar = setingsBackup[5];
	m_fmatching = setingsBackup[6];
	m_rotation = setingsBackup[7];
	m_fullsearch = setingsBackup[8];
	m_qualiti = setingsBackup[9];
	m_use_quality = setingsBackup[10];
}

void CSettingsDialog::DefaultValues()
{
	SaveSetings();
	m_DPI = 500;
	m_mode = 0;
	m_mode_m = 0;
	m_gfar = 2;
	m_mfar = 1;

	if(pHExtractor && pHMatcher)
	{
		FnceReset(*pHExtractor);
		FncmReset(*pHMatcher);
		int rotation = 0;
		FncmGetParameter(*pHMatcher, FNCMP_MAXIMAL_ROTATION, &rotation);
		m_rotation = (int)(rotation*360.0/255.0);
		int qualityTreshold = 0;
		FnceGetParameterW(*pHExtractor, FNCEP_QUALITY_THRESHOLD, &qualityTreshold);
		m_qualiti = (int)(qualityTreshold*100.0/255.0);
	}

	m_fullsearch = 0;
	m_use_quality=0;
}

void CSettingsDialog::OnOK() 
{
	CDialog::OnOK();

	ApplySettings();
}

void CSettingsDialog::SetExtractor(HFncExtractor *hExtractor) 
{
	pHExtractor = hExtractor;
}

void CSettingsDialog::SetMatcher(HFncMatcher * hMatcher) 
{
	pHMatcher= hMatcher;
}


void CSettingsDialog::ApplySettings()
{
	SaveSetings();
	int modes[] = 
	{
		FNCE_MODE_GENERAL,
		FNCE_MODE_DIGITALPERSONA_UAREU,
		FNCE_MODE_BIOMETRIKA_FX2000,
		FNCE_MODE_BIOMETRIKA_FX3000,
		FNCE_MODE_KEYTRONIC_SECUREDESKTOP,
		FNCE_MODE_IDENTIX_TOUCHVIEW,
		FNCE_MODE_IDENTIX_DFR2090,
		FNCE_MODE_PRECISEBIOMETRICS_100CS,
		FNCE_MODE_UPEK_TOUCHCHIP,
		FNCE_MODE_IDENTICATORTECHNOLOGY_DF90,
		FNCE_MODE_AUTHENTEC_AFS2,
		FNCE_MODE_AUTHENTEC_AES4000 ,
		FNCE_MODE_AUTHENTEC_AES2501B,
		FNCE_MODE_ATMEL_FINGERCHIP,
		FNCE_MODE_BMF_BLP100,
		FNCE_MODE_SECUGEN_HAMSTER,
		FNCE_MODE_ETHENTICA,
		FNCE_MODE_CROSSMATCH_VERIFIER300,
		FNCE_MODE_NITGEN_FINGKEY_HAMSTER,
		FNCE_MODE_TESTECH_BIOI,
		FNCE_MODE_DIGENT_IZZIX,
		FNCE_MODE_STARTEK_FM200,
		FNCE_MODE_FUJITSU_MBF200,
		FNCE_MODE_FUTRONIC_FS80,
		FNCE_MODE_LIGHTUNING_LTTC500,
		FNCE_MODE_TACOMA_CMOS
	};

	int eMode = modes[m_mode];
	FnceSetParameterW(*pHExtractor, FNCEP_MODE,&eMode);

	int mMode = modes[m_mode_m];
	FncmSetParameterW(*pHMatcher, FNCMP_MODE,&mMode);

	double rate = 0.1;
	switch(m_gfar)
	{
	case 0: //0.1%
		rate = 0.1;
		break;
	case 1: //0.01%
		rate = 0.01;
		break;
	case 2: //0.001%
		rate = 0.001;
		break;
	}

	int gtreshold = 0;
	FARToMatchingThreshold(rate, gtreshold);
	FnceSetParameterW(*pHExtractor, FNCEP_GENERALIZATION_THRESHOLD,&gtreshold);

	switch(m_mfar)
	{
	case 0: //0.1%
		rate = 0.1;
		break;
	case 1: //0.01%
		rate = 0.01;
		break;
	case 2: //0.001%
		rate = 0.001;
		break;
	}

	int mtreshold = 0;
	FARToMatchingThreshold(rate, mtreshold);
	FncmSetParameterW(*pHMatcher, FNCMP_MATCHING_THRESHOLD,&mtreshold);

	int rotation = (int)(m_rotation*255.0 /360.0);
	FncmSetParameter(*pHMatcher, FNCMP_MAXIMAL_ROTATION, &rotation);

	FnceSetParameterW(*pHExtractor, FNCEP_USE_QUALITY, &m_use_quality);

	int qualityTreshold = (int)(m_qualiti*255.0/100.0);
	FnceSetParameterW(*pHExtractor, FNCEP_QUALITY_THRESHOLD, &qualityTreshold);
}

void CSettingsDialog::OnBnClickedCancel()
{
	LoadSetings();
	ApplySettings();
	OnCancel();
}
