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

char favCommand[COMMAND_SIZE];

extern HWND g_hWnd;
extern HWND hEditMain;
extern HWND g_hwndStatusBar;
extern HINSTANCE g_hInstance;
extern BOOL isWorking;
extern void StartExecute(char *prikaz, int withOutputs);
extern INT_PTR CALLBACK DialogAdd(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);

int OdstranitVybrany(HWND hWnd)
{
	LRESULT vyber;
	LRESULT pocet;
	vyber = SendDlgItemMessage(hWnd, IDC_LIST_FAV, LB_GETCURSEL, 0, 0);
	if (vyber < 0) return -1;
	
	pocet = SendDlgItemMessage(hWnd, IDC_LIST_FAV, LB_DELETESTRING, vyber, 0);
	if (pocet == LB_ERR)
	{
		MessageBox(hWnd, TEXT("Nepodarilo se odebrat polozku!"), TEXT("List box"), MB_ICONERROR);
		return -1;
	}
	if (vyber >= pocet) vyber--;
	SendDlgItemMessage(hWnd, IDC_LIST_FAV, LB_SETCURSEL, vyber, 0);
	return vyber;
}

/* Procedura dialogu Favourite commands */
INT_PTR CALLBACK DialogFavourite(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT vyber;
	INT_PTR dlgResult;
	LPMINMAXINFO minmaxinfo;

	switch (uMsg)
	{
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDCANCEL:
					SendMessage(g_hWnd, WM_COMMAND, ID_HIDEFAVOURITE, 0);
					break;
				case IDC_BUTTON_FAV_ADD:
					favCommand[0] = '\0';
					dlgResult = DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG_ADDCOMMAND), hwndDlg, (DLGPROC)DialogAdd);
					if (dlgResult == IDOK && favCommand[0] != '\0')
					{
						SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_ADDSTRING, 0, (LPARAM)favCommand);
						if (SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETCOUNT, 0, 0) >= MAX_FAVOURITES)
						{
							EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_FAV_ADD), FALSE);
						}
					}
					break;
				case IDC_BUTTON_FAV_REMOVE:
					OdstranitVybrany(hwndDlg);
					if (SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETCOUNT, 0, 0) < MAX_FAVOURITES)
					{
						EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_FAV_ADD), TRUE);
					}
					break;
				case IDC_BUTTON_FAV_CHANGE:
					vyber = SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETCURSEL, 0, 0);
					if (vyber >= 0)
					{
						SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETTEXT, vyber, (LPARAM)favCommand);
						dlgResult = DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG_ADDCOMMAND), hwndDlg, (DLGPROC)DialogAdd);
						if (dlgResult == IDOK && favCommand[0] != '\0')
						{
							SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_INSERTSTRING, vyber + 1, (LPARAM)favCommand);
							OdstranitVybrany(hwndDlg);
						}
					}
					break;
				case IDC_LIST_FAV:
					if (HIWORD(wParam) == LBN_SELCHANGE && !isWorking)
					{
						vyber = SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETCURSEL, 0, 0);
						SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETTEXT, vyber, (LPARAM)favCommand);
						SetWindowText(hEditMain, favCommand);
					}
					if (HIWORD(wParam) == LBN_DBLCLK)
					{
						if (isWorking)
						{
							MessageBox(hwndDlg, TEXT("ABC is processing..."), TEXT("WinABC"), MB_ICONINFORMATION);
						}
						else
						{
							vyber = SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETCURSEL, 0, 0);
							SendDlgItemMessage(hwndDlg, IDC_LIST_FAV, LB_GETTEXT, vyber, (LPARAM)favCommand);
							SendMessage(hEditMain, CB_INSERTSTRING, 0, (LPARAM)favCommand);
							SetFocus(g_hwndStatusBar);	/* hEditMain musi ztratit focus */
							StartExecute(favCommand, EX_WITHOUTPUTS);
						}
					}
					break;
			}
			break;
		case WM_GETMINMAXINFO:
			minmaxinfo = (LPMINMAXINFO)lParam;
			minmaxinfo->ptMinTrackSize.x = 244;
			minmaxinfo->ptMinTrackSize.y = 70;
			break;
		case WM_SIZE:
			MoveWindow(GetDlgItem(hwndDlg, IDC_LIST_FAV), 0, 28, LOWORD(lParam), HIWORD(lParam)-28, TRUE);
			break;
	}
	return FALSE;
}
