#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "WinABC.h"
#include "resource.h"

extern HINSTANCE g_hInstance;
extern LOGFONT logEditFont;
extern int numEdits;
extern char command[MAX_OUTPUTS][COMMAND_SIZE];
HWND hCheckPrint[MAX_OUTPUTS];
extern BOOL zalohaCheck[MAX_OUTPUTS];
extern BOOL wrapText;
extern HWND hwndFavourite;
extern BOOL labelsInToolbar;
extern BOOL timeInStatusbar;
extern BOOL loadAbcrc;
extern BOOL confirmExit;
extern HWND g_hWnd;
extern int showWindowStatus;

void WithAppPath(char *fileName, char *fullFileName)
{
	/* http://www.codeguru.com/forum/showthread.php?t=312471 */
	char szAppPath[MAX_PATH] = "";
	char szAppDirectory[MAX_PATH] = "";

	GetModuleFileName(g_hInstance, szAppPath, sizeof(szAppPath) - 1);

	/* Extract directory */
	strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, '\\') - szAppPath);
	szAppDirectory[strlen(szAppDirectory)] = '\0';
	sprintf(fullFileName, "%s\\%s", szAppDirectory, fileName);
}

void DefaultSettings()
{
	int i;

	/* Nastaveni pisma Courier New */
	GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof (logEditFont), &logEditFont);
	logEditFont.lfHeight = -11;
	lstrcpy(logEditFont.lfFaceName, TEXT("Courier New"));

	labelsInToolbar = TRUE;
	timeInStatusbar = TRUE;
	loadAbcrc = TRUE;
	confirmExit = TRUE;

	wrapText = FALSE;
	numEdits = 4;
	strcpy(command[0], "print_stats");
	strcpy(command[1], "print_gates");
	strcpy(command[2], "print_factor");
	strcpy(command[3], "print_library");

	for (i = 0; i < numEdits; i++)
		zalohaCheck[i] = TRUE;
}

BOOL SaveSettings(HWND hWnd)
{
	int i;
	int pocet;
	WORD delka;
	BOOL commCheck;
	WINDOWPLACEMENT wpl;
	FILE *file;
	char fullFileName[MAX_PATH];
	char text[COMMAND_SIZE];
	BYTE version = SETTINGS_FILE_VERSION;
	
	WithAppPath(SETTINGS_FILE_NAME, fullFileName);
	file = fopen(fullFileName, "wb");
	if (file == NULL)
	{
		return FALSE;
	}

	wpl.length = sizeof (WINDOWPLACEMENT);
	GetWindowPlacement(g_hWnd, &wpl);

	fwrite(&version, sizeof (version), 1, file);
	fwrite(&wpl, sizeof (wpl), 1, file);

	fwrite(&labelsInToolbar, sizeof (labelsInToolbar), 1, file);
	fwrite(&timeInStatusbar, sizeof (timeInStatusbar), 1, file);
	fwrite(&loadAbcrc, sizeof (loadAbcrc), 1, file);
	fwrite(&confirmExit, sizeof (confirmExit), 1, file);

	fwrite(&logEditFont, sizeof (logEditFont), 1, file);
	fwrite(&wrapText, sizeof (wrapText), 1, file);
	fwrite(&numEdits, sizeof (numEdits), 1, file);
	for (i = 0; i < numEdits; i++)
	{
		delka = strlen(command[i]);
		fwrite(&delka, sizeof (delka), 1, file);
		fwrite(&command[i], delka, 1, file);
		commCheck = SendMessage(hCheckPrint[i], BM_GETCHECK, 0, 0);
		fwrite(&commCheck, sizeof (commCheck), 1, file);		
	}

	pocet = SendDlgItemMessage(hwndFavourite, IDC_LIST_FAV, LB_GETCOUNT, 0, 0);
	fwrite(&pocet, sizeof (pocet), 1, file);
	for (i = 0; i < pocet; i++)
	{
		SendDlgItemMessage(hwndFavourite, IDC_LIST_FAV, LB_GETTEXT, i, (LPARAM)text);
		delka = strlen(text);
		fwrite(&delka, sizeof (delka), 1, file);
		fwrite(&text, delka, 1, file);
	}
	fclose(file);
	return TRUE;
}

BOOL LoadSettings(HWND hWnd)
{
	int i;
	int pocet;
	WORD delka;
	WINDOWPLACEMENT wpl;
	FILE *file;
	char fullFileName[MAX_PATH];
	char text[COMMAND_SIZE];
	BYTE version;
	
	WithAppPath(SETTINGS_FILE_NAME, fullFileName);
	file = fopen(fullFileName, "rb");
	if (file == NULL)
	{
		DefaultSettings();
		return SaveSettings(hWnd);
	}
	fread(&version, sizeof (version), 1, file);
	if (version != SETTINGS_FILE_VERSION)
	{
		/* Jiny typ settings souboru */
		fclose(file);
		DefaultSettings();
		return SaveSettings(hWnd);
	}
	fread(&wpl, sizeof (wpl), 1, file);
	SetWindowPos(g_hWnd, NULL, wpl.rcNormalPosition.left, wpl.rcNormalPosition.top,
								wpl.rcNormalPosition.right - wpl.rcNormalPosition.left,
								wpl.rcNormalPosition.bottom - wpl.rcNormalPosition.top,
								0);
	showWindowStatus = wpl.showCmd;
	
	fread(&labelsInToolbar, sizeof (labelsInToolbar), 1, file);
	fread(&timeInStatusbar, sizeof (timeInStatusbar), 1, file);
	fread(&loadAbcrc, sizeof (loadAbcrc), 1, file);
	fread(&confirmExit, sizeof (confirmExit), 1, file);

	fread(&logEditFont, sizeof (logEditFont), 1, file);
	fread(&wrapText, sizeof (wrapText), 1, file);
	fread(&numEdits, sizeof (numEdits), 1, file);
	for (i = 0; i < numEdits; i++)
	{
		fread(&delka, sizeof (delka), 1, file);
		fread(&command[i], delka, 1, file);
		command[i][delka] = '\0';
		fread(&zalohaCheck[i], sizeof (zalohaCheck[i]), 1, file);
	}

	fread(&pocet, sizeof (pocet), 1, file);

	SendDlgItemMessage(hwndFavourite, IDC_LIST_FAV, WM_SETREDRAW, (WPARAM)FALSE, 0); // Optimalizace
	for (i = 0; i < pocet; i++)
	{
		fread(&delka, sizeof (delka), 1, file);
		fread(&text, delka, 1, file);
		text[delka] = '\0';
		SendDlgItemMessage(hwndFavourite, IDC_LIST_FAV, LB_ADDSTRING, 0, (LPARAM)text);
	}
	SendDlgItemMessage(hwndFavourite, IDC_LIST_FAV, WM_SETREDRAW, (WPARAM)TRUE, 0); // Optimalizace
	if (pocet >= MAX_FAVOURITES)
		EnableWindow(GetDlgItem(hwndFavourite, IDC_BUTTON_FAV_ADD), FALSE);

	fclose(file);
	return TRUE;
}
