#include "Interface.h"

void Interface::DrawRectangle(HWND& hWnd, HDC& hdc, int index)
{
	RECT clientrect;

	GetClientRect(hWnd, &clientrect);

	SelectObject(hdc, GetStockObject(NULL_BRUSH));
	SetBkMode(hdc, TRANSPARENT);

	switch (index)
	{
	case 0:
		{
			clientrect.right -= MAINWIN_RECTANGLE_INTERFACE_RIGHT;
			clientrect.bottom = MAINWIN_RECTANGLE_INTERFACE_BOTTTOM;
			clientrect.left = MAINWIN_RECTANGLE_INTERFACE_LEFT;
			clientrect.top = MAINWIN_RECTANGLE_INTERFACE_TOP;

			Rectangle(hdc, clientrect.left, clientrect.top, clientrect.right, clientrect.bottom);
			DrawEdge(hdc, &clientrect, BDR_RAISEDOUTER | BDR_SUNKENINNER, BF_RECT);
		}
	break;
	case 1:
		{
			clientrect.right -= MAINWIN_RECTANGLE_INTERFACE_RIGHT_USER;
			clientrect.bottom = MAINWIN_RECTANGLE_INTERFACE_BOTTTOM_USER;
			clientrect.left = MAINWIN_RECTANGLE_INTERFACE_LEFT_USER;
			clientrect.top = MAINWIN_RECTANGLE_INTERFACE_TOP_USER;

			////
			//TRIVERTEX vertex[2];
			//vertex[0].x = clientrect.left;
			//vertex[0].y = clientrect.top;
			//vertex[0].Red = 0x0000;
			//vertex[0].Green = 0x8000;
			//vertex[0].Blue = 0x8000;
			//vertex[0].Alpha = 0x0000;

			//vertex[1].x = clientrect.right- clientrect.left;
			//vertex[1].y = clientrect.bottom- clientrect.top;
			//vertex[1].Red = 0x0000;
			//vertex[1].Green = 0xd000;
			//vertex[1].Blue = 0xd000;
			//vertex[1].Alpha = 0x0000;

			Rectangle(hdc, clientrect.left, clientrect.top, clientrect.right, clientrect.bottom);
			DrawEdge(hdc, &clientrect, BDR_RAISEDOUTER | BDR_SUNKENINNER, BF_RECT);
			////
			//GRADIENT_RECT gRect;
			//gRect.UpperLeft = 0;
			//gRect.LowerRight = 1;
			//GradientFill(hdc, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_H);
		}
	break;
	default:
		break;
	}
	
}

void Interface::InitTrackersForDisplay(HWND hWnd)
{
	if (hWnd == NULL) return;

	if (_trackersEdit.tracker.size() != 0)
	{
		for (size_t i = 0; i < _trackersEdit.tracker.size(); i++)
		{
			_trackersEdit.tracker[i].clear();
			_trackersEdit.wasUsed.clear();
		}
	}


	int left = REGION_FOR_TRACKERS_EDIT_LEFT;
	for (int i = 1; i <= NUMBER_OF_TRACKER_TO_DISPLAY; i++)
	{
		std::vector<std::pair<std::pair<HWND, bool>, std::pair<float, bool>>> CollectioneditWindow;

		left += REGION_FOR_TRACKERS_EDIT_WIDTH;
		{

			HWND hEdit = CreateWindowW(L"Edit", EDIT_REGION_TRACKER_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER | ES_READONLY,
				left, REGION_FOR_TRACKERS_EDIT_TOP, REGION_FOR_TRACKERS_EDIT_WIDTH, REGION_FOR_TRACKERS_EDIT_HEIGHT, hWnd, NULL, _hInst, NULL);
			HWND hWindow = CreateWindowW(L"Button", BUTTON_REGION_TRACKER_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
				left, REGION_FOR_TRACKERS_TOP, REGION_FOR_TRACKERS_WIDTH, REGION_FOR_TRACKERS_HEIGHT, hWnd, (HMENU)_GetIDOfFacesClick(i), _hInst, nullptr);
			HBITMAP hbit = (HBITMAP)::LoadImage(NULL, PATH_TO_BITMAP_TRACKER, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
			SendMessage(hWindow, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);

			CollectioneditWindow.push_back({ {hEdit,false},{0.f,false } });
			CollectioneditWindow.push_back({ {hWindow,false},{0.f,false} });
		}
		_trackersEdit.tracker.push_back(CollectioneditWindow);
		_trackersEdit.wasUsed.push_back(false);
	}

	HideAllTrackers();
}

void Interface::HideAllTrackers()
{
	for (int i = 0; i < _trackersEdit.tracker.size(); i++)
	{
		HideTracker(i);
	}
}

void Interface::ShowAllTrackers(bool fromMenu)
{
	for (int i = 0; i < _trackersEdit.tracker.size(); i++)
	{
		ShowTracker(i, fromMenu);
	}
}

void Interface::HideTracker(int index)
{
	if (_trackersEdit.tracker.size() > index)
	{
		for (size_t i = 0; i < _trackersEdit.tracker[index].size(); i++)
		{
			HideImage(_trackersEdit.tracker[index].at(i).first.first);
			_trackersEdit.tracker[index].at(i).first.second = false;
			_trackersEdit.tracker[index].at(i).second.second = false;
		}
	}
}

void Interface::ShowTracker(int index,bool fromMenu)
{
	if (_trackersEdit.tracker.size() > index)
	{
		if (fromMenu)
		{
			if (IsWindowVisible(_trackersEdit.tracker[index].at(0).first.first))
			{
				HideTracker(index);
				CheckMenuItem(GetMenu(_hWndMain), _GetIDOfFaces(index), MF_UNCHECKED);
				return;
			}
		}
	
		for (size_t i = 0; i < _trackersEdit.tracker[index].size(); i++)
		{
			if (_trackersEdit.wasUsed[index])
			{
				ShowImage(_trackersEdit.tracker[index].at(i).first.first);
			}			
		}
		if (fromMenu)
		{
			if(IsWindowVisible(_trackersEdit.tracker[index].at(0).first.first))
				CheckMenuItem(GetMenu(_hWndMain), _GetIDOfFaces(index), MF_CHECKED);
		}		
	}
}

void Interface::ShowTemperatureToEdit(int index)
{
	if (_trackersEdit.tracker.size() > index)
	{
		TCHAR txt[50];
		#if UNICODE
				swprintf(txt, 50, TEXT("%.1lf"), _trackersEdit.tracker[index].at(0).second.first);
		#else
				snprintf(txt, 50, "%.1lf", _trackersEdit.tracker[index].at(0).second.first);
		#endif
		SetWindowText(_hEditFalse, txt);		
	}
}

void Interface::DrawTrackers(HWND hWnd, thermoCamera::Device* device)
{
	int num = device->GetNumOfTrackers();
	if (num <= 0)
	{
		//ValidateTrackers(0,device->GetAlarmValue());
		//HideAllTrackers();
	}

	double time;
	const int maxOfChar = 50;
	TCHAR txt[maxOfChar];

	for (size_t i = 0; i < num; i++)
	{
		// was used 
		_trackersEdit.wasUsed[i] = true;

		ShowTracker(i,false);
		time = device->GetTimeFromTracker(i);
		#if UNICODE
			swprintf(txt, maxOfChar, TEXT("%.1lf"), time);
		#else
			snprintf(txt, maxOfChar, "%.1lf", time);
		#endif

		HWND hWin = _trackersEdit.tracker[i].at(0).first.first;
		SetWindowText(hWin, txt);
		_trackersEdit.tracker[i].at(0).first.second = true;

		// Temperature
		float temp = device->GetTemperatureFromTracker(i);
		_trackersEdit.tracker[i].at(0).second.first = temp;

		if (temp > device->GetAlarmValue())
		{
			if (_trackersEdit.tracker[i].at(0).second.second == false)
			{				
				TCHAR txt[MAX_PATH];
				#if UNICODE
					wcscpy_s(txt, MAX_PATH, _absolutePath);
					wcscat_s(txt, MAX_PATH, PATH_TO_ALARM_SOUND);
				#else
					strcpy_s(txt, MAX_PATH, _absolutePath);
					strcat_s(txt, MAX_PATH, PATH_TO_ALARM_SOUND);
				#endif
				PlaySound(txt, NULL, SND_ASYNC);
				_trackersEdit.tracker[i].at(0).second.second = true;
				if (device->isAlarmSavingOn())
				{
					device->SaveImageAfterAlarm();
				}
			}
		}

	}

	ValidateTrackers(num,device->GetAlarmValue());
}

void Interface::CreateInterface(HWND hWnd, thermoCamera::Device* device)
{
	_hWndMain = hWnd;

	_hEditCalibrate = CreateWindowW(L"Edit", EDIT_CALIBRATION_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER | ES_READONLY,
		EDIT_CALIBRATION_LEFT, EDIT_CALIBRATION_TOP, EDIT_CALIBRATION_WIDTH, EDIT_CALIBRATION_HEIGHT, hWnd, NULL, _hInst, NULL);
	SetWindowText(_hEditCalibrate, CALIBRATED_VALUE);
	float val = _ttof(CALIBRATED_VALUE);
	device->SetCalibratedValue(val);

	_hEditAlarm = CreateWindowW(L"Edit", EDIT_ALARM_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER,
		EDIT_ALARM_LEFT, EDIT_ALARM_TOP, EDIT_ALARM_WIDTH, EDIT_ALARM_HEIGHT, hWnd, NULL, _hInst, NULL);
	SetWindowText(_hEditAlarm, ALARM_SET);
	val = _ttof(ALARM_SET);
	device->SetAlarmThreshold(val);

	CreateWindowW(L"Static", L"Temperature", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_TEMPERATURETEXTREGION_LEFT, EDIT_TEMPERATURETEXTREGION_TOP, EDIT_TEMPERATURETEXTREGION_WIDTH,
		EDIT_TEMPERATURETEXTREGION_HEIGHT, hWnd, NULL, _hInst, NULL);

	CreateWindowW(L"Static", L"Number of", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_TEMPERATURETEXTFACE_LEFT, EDIT_TEMPERATURETEXTFACE_TOP, EDIT_TEMPERATURETEXTFACE_WIDTH,
		EDIT_TEMPERATURETEXTFACE_HEIGHT, hWnd, NULL, _hInst, NULL);

	CreateWindowW(L"Static", L"Region", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_TEMPERATUREREGIONTEXT_LEFT, EDIT_TEMPERATUREREGIONTEXT_TOP, EDIT_TEMPERATUREREGIONTEXT_WIDTH,
		EDIT_TEMPERATUREREGIONTEXT_HEIGHT, hWnd, NULL, _hInst, NULL);

	CreateWindowW(L"Static", L"Faces", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_FACETEXT_LEFT, EDIT_FACETEXT_TOP, EDIT_FACETEXT_WIDTH,
		EDIT_FACETEXT_HEIGHT, hWnd, NULL, _hInst, NULL);

	CreateWindowW(L"Static", L"Temperature", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_TEMPERATURETEXTMEASURE_LEFT, EDIT_TEMPERATURETEXTMEASURE_TOP, EDIT_TEMPERATURETEXTMEASURE_WIDTH,
		EDIT_TEMPERATURETEXTMEASURE_HEIGHT, hWnd, NULL, _hInst, NULL);

	_hEditRegion = CreateWindowW(L"Edit", EDIT_TEMPERATUREREGION_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER | ES_READONLY,
		EDIT_TEMPERATUREREGION_LEFT, EDIT_TEMPERATUREREGION_TOP, EDIT_TEMPERATUREREGION_WIDTH, EDIT_TEMPERATUREREGION_HEIGHT, hWnd, NULL, _hInst, NULL);

	_hEditFace = CreateWindowW(L"Edit", EDIT_TEMPERATUREFACE_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER | ES_READONLY,
		EDIT_TEMPERATUREFACE_LEFT, EDIT_TEMPERATUREFACE_TOP, EDIT_TEMPERATUREFACE_WIDTH, EDIT_TEMPERATUREFACE_HEIGHT, hWnd, NULL, _hInst, NULL);

	CreateWindowW(L"Static", L"Real", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_FACETRUE_LEFT, EDIT_FACETRUE_TOP, EDIT_FACETRUE_WIDTH,
		EDIT_FACETRUE_HEIGHT, hWnd, NULL, _hInst, NULL);
	_hEditTrue = CreateWindowW(L"Edit", EDIT_TRUE_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER,
		EDIT_TRUE_LEFT, EDIT_TRUE_TOP, EDIT_TRUE_WIDTH, EDIT_TRUE_HEIGHT, hWnd, NULL, _hInst, NULL);

	CreateWindowW(L"Static", L"Measured", WS_CHILD | WS_VISIBLE | SS_CENTER, EDIT_FACEFALSE_LEFT, EDIT_FACEFALSE_TOP, EDIT_FACEFALSE_WIDTH,
		EDIT_FACEFALSE_HEIGHT, hWnd, NULL, _hInst, NULL);
	_hEditFalse = CreateWindowW(L"Edit", EDIT_FALSE_NAME, WS_CHILD | ES_CENTER | WS_VISIBLE | WS_BORDER,
		EDIT_FALSE_LEFT, EDIT_FALSE_TOP, EDIT_FALSE_WIDTH, EDIT_FALSE_HEIGHT, hWnd, NULL, _hInst, NULL);
}

INT_PTR Interface::DoRecordCongig(HWND hWnd)
{
	PROPSHEETPAGE psp[1];
	PROPSHEETHEADER psh;

	//Fill out the PROPSHEETPAGE data structure for the Background Color sheet
	psp[0].dwSize = sizeof(PROPSHEETPAGE);
	psp[0].dwFlags = PSP_USETITLE;
	psp[0].hInstance = _hInst;
	psp[0].pszTemplate = MAKEINTRESOURCE(IDD_RECORDCONFIG);
	psp[0].pszIcon = NULL;
	psp[0].pfnDlgProc = RecordConfig;
	psp[0].pszTitle = TEXT("Configuration");
	psp[0].lParam = 0;

	return PropertySheet(&psh);
}

INT_PTR Interface::DoWindowConfig(HWND hWnd)
{
	PROPSHEETPAGE psp[2];
	PROPSHEETHEADER psh;

	//Fill out the PROPSHEETPAGE data structure for the Background Color sheet
	psp[0].dwSize = sizeof(PROPSHEETPAGE);
	psp[0].dwFlags = PSP_USETITLE;
	psp[0].hInstance = _hInst;
	psp[0].pszTemplate = MAKEINTRESOURCE(IDM_CONFIG_WINDOW);
	psp[0].pszIcon = NULL;
	psp[0].pfnDlgProc = WindowConfig;
	psp[0].pszTitle = TEXT("Window");
	psp[0].lParam = 0;

	return PropertySheet(&psh);
}

INT_PTR Interface::DoSettingsConfig(HWND hWnd)
{
	PROPSHEETPAGE psp[4];
	PROPSHEETHEADER psh;

	//Fill out the PROPSHEETPAGE data structure for the Background Color sheet
	psp[0].dwSize = sizeof(PROPSHEETPAGE);
	psp[0].dwFlags = PSP_USETITLE;
	psp[0].hInstance = _hInst;
	psp[0].pszTemplate = MAKEINTRESOURCE(IDD_SETTING_VIDEO);
	psp[0].pszIcon = NULL;
	psp[0].pfnDlgProc = VideoSettingDlgProc;
	psp[0].pszTitle = TEXT("Video");
	psp[0].lParam = 0;

	//Fill out the PROPSHEETPAGE data structure for the Client Area Shape sheet
	psp[1].dwSize = sizeof(PROPSHEETPAGE);
	psp[1].dwFlags = PSP_USETITLE;
	psp[1].hInstance = _hInst;
	psp[1].pszTemplate = MAKEINTRESOURCE(IDD_SETTING_FACE);
	psp[1].pszIcon = NULL;
	psp[1].pfnDlgProc = FaceSettingDlgProc;
	psp[1].pszTitle = TEXT("Faces");
	psp[1].lParam = 0;

	//Fill out the PROPSHEETPAGE data structure for the Client Area Shape sheet
	psp[2].dwSize = sizeof(PROPSHEETPAGE);
	psp[2].dwFlags = PSP_USETITLE;
	psp[2].hInstance = _hInst;
	psp[2].pszTemplate = MAKEINTRESOURCE(IDD_SETTING_ALARM);
	psp[2].pszIcon = NULL;
	psp[2].pfnDlgProc = AlarmSettingDlgProc;
	psp[2].pszTitle = TEXT("Alarm");
	psp[2].lParam = 0;

	//Fill out the PROPSHEETPAGE data structure for the Client Area Shape sheet
	psp[3].dwSize = sizeof(PROPSHEETPAGE);
	psp[3].dwFlags = PSP_USETITLE;
	psp[3].hInstance = _hInst;
	psp[3].pszTemplate = MAKEINTRESOURCE(IDD_SETTING_TEMP);
	psp[3].pszIcon = NULL;
	psp[3].pfnDlgProc = TempSettingDlgProc;
	psp[3].pszTitle = TEXT("Temp");
	psp[3].lParam = 0;

	//Fill out the PROPSHEETHEADER
	psh.dwSize = sizeof(PROPSHEETHEADER);
	psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
	psh.hwndParent = hWnd;
	psh.hInstance = _hInst;
	psh.pszIcon = MAKEINTRESOURCE(IDI_SMALL);
	psh.pszCaption = TEXT("Settings");
	psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
	psh.ppsp = (LPCPROPSHEETPAGE)&psp;
	psh.pfnCallback = (PFNPROPSHEETCALLBACK)PropSheetCallback;

	//And finally display the modal property sheet
	return PropertySheet(&psh);
}

void Interface::AddCombos(HWND hWnd)
{
	HWND hPalleteImage = CreateWindow(WC_COMBOBOX, TEXT(""),
		CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
		COMBO_PALLETE_LEFT, COMBO_PALLETE_TOP, COMBO_PALLETE_WIDTH, COMBO_PALLETE_HEIGHT, hWnd, NULL, _hInst,
		NULL);

	TCHAR Palettes[11][20] =
	{
		TEXT("AlarmBlue"), TEXT("AlarmBlueHi"), TEXT("GrayBW"), TEXT("GrayWB"),
		TEXT("AlarmGreen"), TEXT("Iron"), TEXT("IronHi"), TEXT("Medical"), TEXT("Rainbow"),
		TEXT("RainbowHi"), TEXT("AlarmRed")
	};

	TCHAR A[50];
	int  k = 0;

	memset(&A, 0, sizeof(A));
	for (k = 0; k <= 10; k += 1)
	{
		#if UNICODE
			wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Palettes[k]);
		#else
			strcpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Palettes[k]);
		#endif
			SendMessage(hPalleteImage, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
	}
 
	SendMessage(hPalleteImage, CB_SETCURSEL, (WPARAM)5, (LPARAM)0);
}

void Interface::InitRecordConfig(HWND hWnd,thermoCamera::Device* device)
{
	// Combo
	const int numID = 2;
	TCHAR Palettes[numID][20] =
	{
		TEXT("Read"), TEXT("Write")
	};

	HWND hwndList = GetDlgItem(hWnd, IDC_COMB_REC_WR);

	TCHAR A[50];
	int  k = 0;

	memset(&A, 0, sizeof(A));
	for (k = 0; k < numID; k += 1)
	{
	#if UNICODE
			wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Palettes[k]);
	#else
			strcpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Palettes[k]);
	#endif
			SendMessage(hwndList, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
	}

	int res = device->GetMethodForRecord();
	SendMessage(hwndList, CB_SETCURSEL, (WPARAM)res, (LPARAM)0);

	// Enable
	if(device->GetPlaybackStatus()) CheckDlgButton(hWnd, IDC_ENABLE_RECORD, BST_CHECKED);

	// Time
	unsigned int time = device->GetTimeFromTimer();
	HWND hText = GetDlgItem(hWnd, IDC_RECORD_TIME_EDIT);
	TCHAR text[50];
	#if UNICODE
		swprintf_s(text, 50, TEXT("%d"), time);
	#else
		sprintf_s(text, 50, TEXT("%d"), time);
	#endif
	SetWindowText(hText, text);

	//Path to record
	hText = GetDlgItem(hWnd, IDC_RECORD_PATH_EDIT);
	const TCHAR* path = device->GetPathRecord();
	if (path != TEXT("Empty"))
	{
		SetWindowText(hText, path);
	}
}

void Interface::InitLogger()
{
	TCHAR pathToDirectory[MAX_PATH];
	
	#if UNICODE
		wcscpy_s(pathToDirectory, MAX_PATH, _absolutePath);
		HRESULT results = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pathToDirectory);

		if (results != S_OK) return;

		wcscat_s(pathToDirectory, MAX_PATH, FOLDER_FOR_DOCUMENTS);
		CreateDirectory(pathToDirectory,NULL);
		wcscat_s(pathToDirectory, MAX_PATH, FOLDER_FOR_LOGGER);
		CreateDirectory(pathToDirectory, NULL);
		wcscat_s(pathToDirectory, MAX_PATH, TEXT("\\"));

		std::time_t result = std::time(nullptr);
		TCHAR * txt = _wctime(&result);
		int n = lstrlen(txt);
		txt[--n] = TEXT('\0');

		std::wstring editFileName = txt;
		std::replace(editFileName.begin(), editFileName.end(), ' ', '_');
		std::replace(editFileName.begin(), editFileName.end(), ':', '_');

		wcscat_s(pathToDirectory, MAX_PATH, FOLDER_FOR_LOGGER);
		wcscat_s(pathToDirectory, MAX_PATH, editFileName.c_str());
		wcscat_s(pathToDirectory, MAX_PATH, SUFFIX_FOR_LOGGER);
		std::wstring Path = pathToDirectory;
	#else
		strcpy_s(pathToDirectory, MAX_PATH, _absolutePath);
		HRESULT results = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pathToDirectory);

		if (results != S_OK) return;

		strcat_s(pathToDirectory, MAX_PATH, FOLDER_FOR_DOCUMENTS);
		CreateDirectory(pathToDirectory, NULL);
		strcat_s(pathToDirectory, MAX_PATH, FOLDER_FOR_LOGGER);
		CreateDirectory(pathToDirectory, NULL);
		strcat_s(pathToDirectory, MAX_PATH, "\\");

		std::time_t result = std::time(nullptr);
		TCHAR txt[MAX_PATH];
		ctime_s(txt, MAX_PATH, &result);
		int n = strlen(txt);
		txt[--n] = '\0';

		std::string editFileName = txt;
		std::replace(editFileName.begin(), editFileName.end(), ' ', '_');
		std::replace(editFileName.begin(), editFileName.end(), ':', '_');

		strcat_s(pathToDirectory, MAX_PATH, editFileName.c_str());
		strcat_s(pathToDirectory, MAX_PATH, SUFFIX_FOR_LOGGER);
		std::string Path = pathToDirectory;
	#endif

	_Logger.Init(Path);
}

void Interface::InitWindowConfig(HWND hWnd, thermoCamera::Device* device)
{
	// Enable
	if (device->isAutoSizeSet()) CheckDlgButton(hWnd, IDC_ENABLE_AUTOSIZE, BST_CHECKED);
}

void Interface::InitVideoConfig(HWND hWnd, thermoCamera::Device* device)
{
	// Enable
	if(device->isSetManualTemperatureRange()) CheckDlgButton(hWnd, IDC_ENABLE_MANUAL_RANGE_TEMPERATURE, BST_CHECKED);

	std::pair<float, float> temp = device->GetMinMaxTemperature();

	// min range
	HWND hText = GetDlgItem(hWnd, IDC_MIN_VIDEO_TEMP_EDIT);
	TCHAR text[50];
	#if UNICODE
		swprintf_s(text, 50, TEXT("%.1f"), temp.first);
	#else
		sprintf_s(text, 50, TEXT("%.1f"), temp.first);
	#endif
	SetWindowText(hText, text);

	// max range
	hText = GetDlgItem(hWnd, IDC_MAX_VIDEO_TEMP_EDIT);
	#if UNICODE
		swprintf_s(text, 50, TEXT("%.1f"), temp.second);
	#else
		sprintf_s(text, 50, TEXT("%.1f"), temp.second);
	#endif
	SetWindowText(hText, text);
}

void Interface::InitFaceSetting(HWND hWnd, thermoCamera::Device* device)
{
	// Enable
	if (device->isFaceDetectionOn()) CheckDlgButton(hWnd, IDC_ENABLE_FACE_DETECTION, BST_CHECKED);

	//Path to classifier
	HWND hText = GetDlgItem(hWnd, IDC_CLASSIFIER_PATH_EDIT);
	TCHAR txt[MAX_PATH];

	#if UNICODE
		wcscpy_s(txt, MAX_PATH, _absolutePath);
		wcscat_s(_absolutePath, MAX_PATH, PATH_TO_CASCADE);
	#else
		strcpy_s(txt, MAX_PATH, _absolutePath);
		strcat_s(_absolutePath, MAX_PATH, PATH_TO_CASCADE);
	#endif

	SetWindowText(hText, _absolutePath);
	
}

void Interface::InitAlarmSetting(HWND hWnd, thermoCamera::Device* device)
{
	// Enable
	if (device->isAlarmSavingOn()) CheckDlgButton(hWnd, IDC_ENABLE_ALARM_SAVING, BST_CHECKED);

	//Path to record
	HWND hText = GetDlgItem(hWnd, IDC_ALARM_IMAGE_PATH_EDIT);
	const TCHAR* path = device->GetAlarmPathToSaving();
	if (device->isAlarmPathSet())
	{
		SetWindowText(hText, path);
	}
}

void Interface::InitTempConfig(HWND hWnd, thermoCamera::Device* device)
{
	// Combo
	const int numID = 2;
	TCHAR Palettes[numID][20] =
	{
		TEXT("Absolute"), TEXT("Relative")
	};

	HWND hwndList = GetDlgItem(hWnd, IDC_COMB_TEMP_ABSRELAT);

	TCHAR A[50];
	int  k = 0;

	memset(&A, 0, sizeof(A));
	for (k = 0; k < numID; k += 1)
	{
		#if UNICODE
				wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Palettes[k]);
		#else
				strcpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Palettes[k]);
		#endif
		SendMessage(hwndList, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
	}

	int res = (int)device->IsSetRelativeTemp();
	SendMessage(hwndList, CB_SETCURSEL, (WPARAM)res, (LPARAM)0);
}

void Interface::OpenAndSetRecordPathOutput(HWND hWnd, thermoCamera::Device* device)
{
	BROWSEINFO br;
	ZeroMemory(&br, sizeof(BROWSEINFO));
	br.lpfn = BrowseCallbackProc;
	br.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
	br.hwndOwner = hWnd;
	br.lpszTitle = TEXT("Choose folder");

	LPITEMIDLIST pidl = NULL;
	device->SetMethodForRecord(1);
	if ((pidl = SHBrowseForFolder(&br)) != NULL)
	{
		TCHAR buffer[MAX_PATH];
		if (SHGetPathFromIDList(pidl, buffer))
		{
			HWND hText = GetDlgItem(hWnd, IDC_RECORD_PATH_EDIT);
			SetWindowText(hText, buffer);
			#if UNICODE	
				wcscat_s(buffer, MAX_PATH, TEXT("\\"));
			#else
				strcat_s(buffer, MAX_PATH, TEXT("\\"));
			#endif
			//device->SetPathToRecord(buffer);
		}
	}
}

void Interface::OpenAndSetRecordPathInput(HWND hWnd, thermoCamera::Device* device)
{
	OPENFILENAME ofn;
	TCHAR filename[MAX_PATH];

	ZeroMemory(&ofn, sizeof(OPENFILENAME));

	TCHAR txt[] =
		TEXT("All files\0*.*\0")
		TEXT("BMP Files\0*.bmp\0\0");

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.lpstrTitle = TEXT("Choose bitmap file");
	ofn.hwndOwner = hWnd;
	ofn.lpstrFile = filename;
	ofn.lpstrFile[0] = TEXT('\0');
	ofn.nMaxFile = 100;
	ofn.lpstrFilter = txt;
	ofn.nFilterIndex = 2;

	GetOpenFileName(&ofn);

	HWND hText = GetDlgItem(hWnd, IDC_RECORD_PATH_EDIT);
	SetWindowText(hText, filename);
	//device->SetPathToRecord(filename);
}

void Interface::OpenAndSetClassifierPath(HWND hWnd, thermoCamera::Device* device)
{
	OPENFILENAME ofn;
	TCHAR filename[MAX_PATH];

	ZeroMemory(&ofn, sizeof(OPENFILENAME));

	TCHAR txt[] =
		TEXT("All files\0*.*\0")
		TEXT("XML Files\0*.xml\0\0");

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.lpstrTitle = TEXT("Choose classifier xml file"); 
	ofn.hwndOwner = hWnd;
	ofn.lpstrFile = filename;
	ofn.lpstrFile[0] = TEXT('\0');
	ofn.nMaxFile = 100;
	ofn.lpstrFilter = txt;
	ofn.nFilterIndex = 2;

	GetOpenFileName(&ofn);

	HWND hText = GetDlgItem(hWnd, IDC_CLASSIFIER_PATH_EDIT);
	SetWindowText(hText, filename);
}

void Interface::OpenAndSetAlarmPath(HWND hWnd, thermoCamera::Device* device)
{
	BROWSEINFO br;
	ZeroMemory(&br, sizeof(BROWSEINFO));
	br.lpfn = BrowseCallbackProc;
	br.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
	br.hwndOwner = hWnd;
	br.lpszTitle = TEXT("Choose folder");

	LPITEMIDLIST pidl = NULL;

	if ((pidl = SHBrowseForFolder(&br)) != NULL)
	{
		TCHAR buffer[MAX_PATH];
		if (SHGetPathFromIDList(pidl, buffer))
		{
			HWND hText = GetDlgItem(hWnd, IDC_ALARM_IMAGE_PATH_EDIT);
			SetWindowText(hText, buffer);
		}
	}
}

void Interface::AddButtons(HWND hWnd)
{
	{
		HWND hStart = CreateWindowW(L"Button", BUTTON_START_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
			BUTTON_START_LEFT, BUTTON_START_TOP, BUTTON_START_WIDTH, BUTTON_START_HEIGHT, hWnd, (HMENU)START_STREAMING, _hInst, nullptr);
		HBITMAP hbit = (HBITMAP)::LoadImage(NULL, TEXT("Images\\play.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		SendMessage(hStart, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
	}
	{
		HWND hPause = CreateWindowW(L"Button", BUTTON_PAUSE_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
			BUTTON_PAUSE_LEFT, BUTTON_PAUSE_TOP, BUTTON_PAUSE_WIDTH, BUTTON_PAUSE_HEIGHT, hWnd, (HMENU)PAUSE_STREAMING, _hInst, nullptr);
		HBITMAP hbit = (HBITMAP)::LoadImage(NULL, TEXT("Images\\pause.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		SendMessage(hPause, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
	}
	{
		HWND hStop = CreateWindowW(L"Button", BUTTON_STOP_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
			BUTTON_STOP_LEFT, BUTTON_STOP_TOP, BUTTON_STOP_WIDTH, BUTTON_STOP_HEIGHT, hWnd, (HMENU)STOP_STREAMING, _hInst, nullptr);
		HBITMAP hbit = (HBITMAP)::LoadImage(NULL, TEXT("Images\\stop.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		SendMessage(hStop, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
	}
	{
		HWND hFaceDetector = CreateWindowW(L"Button", BUTTON_FACE_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
			BUTTON_FACE_LEFT, BUTTON_FACE_TOP, BUTTON_FACE_WIDTH, BUTTON_FACE_HEIGHT, hWnd, (HMENU)FACE_DETECTION, _hInst, nullptr);
		HBITMAP hbit = (HBITMAP)::LoadImage(NULL, TEXT("Images\\temp.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		SendMessage(hFaceDetector, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
	}
	{
		HWND hFaceDetector = CreateWindowW(L"Button", BUTTON_CALI_BUTTON_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
			BUTTON_CALI_BUTTON_LEFT, BUTTON_CALI_BUTTON_TOP, BUTTON_CALI_BUTTON_WIDTH, BUTTON_CALI_BUTTON_HEIGHT, hWnd, (HMENU)CALIBRATE, _hInst, nullptr);
		HBITMAP hbit = (HBITMAP)::LoadImage(NULL, TEXT("Images\\temperature.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		SendMessage(hFaceDetector, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
	}
	{
		HWND hAlarm = CreateWindowW(L"Button", BUTTON_ALARM_BUTTON_NAME, WS_CHILD | WS_VISIBLE | BS_BITMAP,
			BUTTON_ALARM_BUTTON_LEFT, BUTTON_ALARM_BUTTON_TOP, BUTTON_ALARM_BUTTON_WIDTH, BUTTON_ALARM_BUTTON_HEIGHT, hWnd, (HMENU)ALARMSET, _hInst, nullptr);
		HBITMAP hbit = (HBITMAP)::LoadImage(NULL, TEXT("Images\\warning.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		SendMessage(hAlarm, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
	}
}

void Interface::AddImages(HWND hWnd, HDC hdc)
{

	if (_image != NULL) 
	{
		delete _image;
		_image = NULL;
	}

	

	if (_hideNoVideo)
	{
		if (_isErased == false)
		{
			RECT rc = { VIDEOCAPTURE_LEFT ,VIDEOCAPTURE_TOP ,VIDEOCAPTURE_LEFT + 526 ,VIDEOCAPTURE_TOP + 527 };
			InvalidateRect(hWnd, &rc, true);
			_isErased = true;
		}
		return;
	}
	else
	{
		_isErased = false;
	}

	

	Gdiplus::GdiplusStartupInput gdiplusStartupInput;

	GdiplusStartup(&_gdiplusToken, &gdiplusStartupInput, NULL);


	Gdiplus::Rect rect(VIDEOCAPTURE_LEFT, VIDEOCAPTURE_TOP, 526, 527);
	Gdiplus::Graphics grpx(hdc);
	_image = new Gdiplus::Image(L"C:\\Users\\matej\\source\\repos\\Automatic Fever Screening\\Automatic Fever Screening\\Images\\novideo.png");
	grpx.DrawImage(_image, rect);

	
}

void Interface::SetNumberOfFaces(HWND hWnd, thermoCamera::Device* device)
{
	int val = device->GetNumOfTrackers();
	TCHAR valueTxt[50];
	#if UNICODE
		swprintf_s(valueTxt, 50, TEXT("%.d"), val);
	#else
		snprintf(valueTxt, 50, "%.d", val);
	#endif

	SetWindowText(_hEditFace, valueTxt);
}

void Interface::SetTemperatureOfRegion(HWND hWnd, thermoCamera::Device* device)
{
	if (hWnd == NULL) return;

	float val = device->GetTemperatureOfRegion();
	val += device->GetCalibratedValue();

	TCHAR valueTxt[20];
	#if UNICODE
		swprintf_s(valueTxt, 20, TEXT("%.1f"), val);
	#else
		snprintf(valueTxt, 20, "%.1f", val);
	#endif

	SetWindowText(_hEditRegion, valueTxt);
}

void Interface::SetAlarm(HWND hWnd, thermoCamera::Device* device)
{
	TCHAR txt[MAX_PATH];
	GetWindowText(_hEditAlarm, txt, MAX_PATH);
	float value = _ttof(txt);
	device->SetAlarmThreshold(value);
}

void Interface::SetCalibrateValue(HWND hWnd, thermoCamera::Device* device)
{
	if (hWnd == NULL) return;

	// True Value
	TCHAR txt[50];
	GetWindowText(_hEditTrue, txt, 50);
	float trueValue = _ttof(txt);

	// False Value
	GetWindowText(_hEditFalse, txt, 50);
	float falseValue = _ttof(txt);
	if(falseValue != 0) falseValue -= device->GetCalibratedValue();

	float calibrate = trueValue - falseValue;
	device->SetCalibratedValue(calibrate);

	falseValue += calibrate;

	#if UNICODE
		swprintf_s(txt, 50, TEXT("%.1f"), calibrate);
	#else
		snprintf(txt, 50, "%.1.f", calibrate);
	#endif

	SetWindowText(_hEditCalibrate, txt);

	#if UNICODE
		swprintf_s(txt, 50, TEXT("%.1f"), falseValue);
	#else
		snprintf(txt, 50, "%.1.f", calibrate);
	#endif

	SetWindowText(_hEditFalse, txt);
}

void Interface::Delete()
{
	if (_image) { delete _image; _image = NULL; }
	// shut down - only once per process
	Gdiplus::GdiplusShutdown(_gdiplusToken);
}

void Interface::ValidateTrackers(int num,float alarmThreshold)
{
	for (int i = 0; i < _trackersEdit.tracker.size(); i++)
	{
		if (_trackersEdit.tracker[i].at(0).first.second == true)
		{
			if (num <= i)
			{
			#if UNICODE
					std::wstring log = std::to_wstring(_trackersEdit.tracker[i].at(0).second.first);

					if (_trackersEdit.tracker[i].at(0).second.first > alarmThreshold)
					{
						_Logger.Logg(1, log);
					}
					else
					{
						_Logger.Logg(0, log);
					}
			#else
				std::string log = std::to_string(_trackersEdit.tracker[i].at(0).second.first);

				if (_trackersEdit.tracker[i].at(0).second.first > alarmThreshold)
				{
					_Logger.Logg(1, log);
				}
				else
				{
					_Logger.Logg(0, log);
				}
			#endif
				
				HideTracker(i);
			}
		}
	}
	
}

UINT Interface::_GetIDOfFaces(int index)
{
	++index;
	UINT result;
	switch (index)
	{
	case 1:
		result = IDM_FIRST;
		break;
	case 2:
		result = IDM_SECOND;
		break;
	case 3:
		result = IDM_THIRD;
		break;
	case 4:
		result = IDM_FOURTH;
		break;
	case 5:
		result = IDM_FIFTH;
		break;
	case 6:
		result = IDM_SIXTH;
		break;
	case 7:
		result = IDM_SEVENTH;
		break;
	case 8:
		result = IDM_EIGTH;
		break;
	default:
		result = IDM_FIRST;
		break;
	}

	return result;
}

UINT Interface::_GetIDOfFacesClick(int index)
{
	UINT result;
	switch (index)
	{
	case 1:
		result = ClICK_ON_TRACKER_FIRST;
		break;
	case 2:
		result = ClICK_ON_TRACKER_SECOND;
		break;
	case 3:
		result = ClICK_ON_TRACKER_THIRD;
		break;
	case 4:
		result = ClICK_ON_TRACKER_FOURTH;
		break;
	case 5:
		result = ClICK_ON_TRACKER_FIFTH;
		break;
	case 6:
		result = ClICK_ON_TRACKER_SIXTH;
		break;
	case 7:
		result = ClICK_ON_TRACKER_SEVENTH;
		break;
	case 8:
		result = ClICK_ON_TRACKER_EIGHTH;
		break;
	default:
		result = ClICK_ON_TRACKER_FIRST;
		break;
	}

	return result;
}

