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

extern HINSTANCE g_hInstance;

int numEdits;
int numEditsTmp;
int selected;
HWND hEdits[MAX_OUTPUTS];
char command[MAX_OUTPUTS][COMMAND_SIZE];
BOOL wrapText;
LOGFONT fontTemp;

extern HWND hEditPrint[MAX_OUTPUTS];
extern HWND hCheckPrint[MAX_OUTPUTS];
extern BOOL zalohaCheck[MAX_OUTPUTS];
extern HWND hwndOutput;
extern LOGFONT logEditFont;
extern HFONT hfCourier;
extern void VytvorEdity();
extern BOOL SaveSettings(HWND hWnd);
extern void CenterWindow(HWND hWnd);

void ChooseFontDialog(HWND hWnd)
{
	CHOOSEFONT chf;
	ZeroMemory(&chf, sizeof (chf));
	chf.lStructSize = sizeof (chf);
	chf.hwndOwner = hWnd;
	chf.lpLogFont = &logEditFont;
	chf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; //CF_EFFECTS
	if (!ChooseFont(&chf))
		return;
	CopyMemory(&fontTemp, chf.lpLogFont, sizeof (LOGFONT));
}

HWND CreateEdit(HWND hwnd, int x0, int y0, int width, int height)
{
	HWND hEdit;
	hEdit = CreateWindowEx(WS_EX_RIGHTSCROLLBAR | WS_EX_STATICEDGE ,
		TEXT("EDIT"), TEXT(""),
		WS_CHILD | WS_VISIBLE |	ES_WANTRETURN |
		ES_AUTOHSCROLL | WS_TABSTOP,
		x0, y0, width, height,
		hwnd, NULL,	g_hInstance, NULL);
	if (!IsWindow(hEdit))
	{
		MessageBox(hwnd, TEXT("Nelze vytvorit edit box!"), TEXT("Error"), MB_ICONERROR);
		return NULL;
	}
	return hEdit;
}

void UpravitVelikostOkna(HWND hwndDlg)
{
	SetWindowPos(hwndDlg, NULL, 0, 0, 440, 117 + 24*((numEditsTmp-1)/2), SWP_NOMOVE);
}

/* Procedura dialogu Outputs settings */
INT_PTR CALLBACK DialogPrintSettings(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	int i;
	char text[COMMAND_SIZE];
	HWND focus;

	switch (uMsg)
	{
		case WM_COMMAND:
			switch (HIWORD(wParam))
			{
				case EN_SETFOCUS:
					focus = GetFocus();
					for (i = 0; i < numEditsTmp; i++)
					{
						if (focus == hEdits[i])
						{
							selected = i;
							break;
						}
					}
					break;
			}
			switch (LOWORD(wParam))
			{
				case IDCANCEL:
					EndDialog(hwndDlg, IDCANCEL);
					break;
				case IDOK:
					wrapText = SendDlgItemMessage(hwndDlg, IDC_CHECK_WRAP, BM_GETCHECK, 0, 0);
					logEditFont = fontTemp;
					
					for (i = 0; i < numEditsTmp; i++)
					{
						GetWindowText(hEdits[i], command[i], COMMAND_SIZE - 1);
					}
					numEdits = numEditsTmp;
					for (i = 0; i < MAX_OUTPUTS; i++)
					{
						if (IsWindow(hEditPrint[i]))
							if (!DestroyWindow(hEditPrint[i]))
								MessageBox(hwndDlg, TEXT("Nelze znicit okno Edit"), TEXT("Chyba"), MB_ICONERROR);
						if (IsWindow(hCheckPrint[i]))
							if (!DestroyWindow(hCheckPrint[i]))
								MessageBox(hwndDlg, TEXT("Nelze znicit okno Check"), TEXT("Chyba"), MB_ICONERROR);
					}
					VytvorEdity();
					SendDlgItemMessage(hwndOutput, IDC_EDIT_OUTPUT, WM_SETFONT, (WPARAM)hfCourier, (LPARAM)TRUE);
					SaveSettings(hwndDlg);
					EndDialog(hwndDlg, IDOK);
					break;
				case IDC_BUTTON_ADD:
					hEdits[numEditsTmp] = CreateEdit(hwndDlg, 10 + (numEditsTmp%2)*215, 60 + 12*(numEditsTmp - (numEditsTmp%2)), 200, 20);
					numEditsTmp++;
					for (i = numEditsTmp - 1; i > selected + 1; i--)
					{
						GetWindowText(hEdits[i - 1], text, COMMAND_SIZE - 1);
						SetWindowText(hEdits[i], text);
						zalohaCheck[i] = zalohaCheck[i - 1];
					}
					SetWindowText(hEdits[selected + 1], "");
					zalohaCheck[selected + 1] = TRUE;
					UpravitVelikostOkna(hwndDlg);
					if (numEditsTmp >= MAX_OUTPUTS)
						EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_ADD), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_REMOVE), TRUE);
					SetFocus(hEdits[++selected]);
					break;
				case IDC_BUTTON_REMOVE:
					numEditsTmp--;
					for (i = selected; i < numEditsTmp; i++)
					{
						GetWindowText(hEdits[i + 1], text, COMMAND_SIZE - 1);
						SetWindowText(hEdits[i], text);
						zalohaCheck[i] = zalohaCheck[i + 1];
					}
					DestroyWindow(hEdits[numEditsTmp]);
					UpravitVelikostOkna(hwndDlg);
					if (numEditsTmp <= 1)
						EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_REMOVE), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_ADD), TRUE);
					selected = selected > 0 ? selected - 1 : selected;
					SetFocus(hEdits[selected]);
					break;
				case IDC_BUTTON_FONT:
					ChooseFontDialog(hwndDlg);
					break;
			}
		  break;
		case WM_INITDIALOG:
			fontTemp = logEditFont;
			numEditsTmp = numEdits;
			for (i = 0; i < numEditsTmp; i++)
			{
				hEdits[i] = CreateEdit(hwndDlg, 10 + (i%2)*215, 60 + 12*(i - (i%2)), 200, 20);
				SetWindowText(hEdits[i], command[i]);
				zalohaCheck[i] = SendMessage(hCheckPrint[i], BM_GETCHECK, 0, 0);
			}
			UpravitVelikostOkna(hwndDlg);
			CenterWindow(hwndDlg);

			if (numEditsTmp >= MAX_OUTPUTS)
				EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_ADD), FALSE);
			if (numEditsTmp <= 1)
				EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_REMOVE), FALSE);
			
			if (wrapText)
				SendDlgItemMessage(hwndDlg, IDC_CHECK_WRAP, BM_SETCHECK, BST_CHECKED, 0);
			else
				SendDlgItemMessage(hwndDlg, IDC_CHECK_WRAP, BM_SETCHECK, BST_UNCHECKED, 0);

			SetFocus(hEdits[0]);
			SendMessage(hEdits[0], EM_SETSEL, 0, (LPARAM)-1); // Nastaveni karetu
			SendMessage(hEdits[0], EM_SETSEL, (WPARAM)-1, 0); // Nastaveni karetu
			selected = 0;
			break;
	}
	return FALSE;
}
