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

extern void EnableInputs();
extern void CenterWindow(HWND hWnd);
extern void StartExecute(char *prikaz, int withOutputs);
extern BOOL Execute(char *prikaz, int typeOfExec);
extern void WithAppPath(char *fileName, char *fullFileName);

extern HWND hEditMain;
extern HWND g_hwndStatusBar;

HWND hWizardEditHelp;
int commCount;

typedef struct
{
	int group;
	char name[32];
} ComWz;

ComWz commWz[500];

void OdstranDvojtecku(char *text)
{
	int i = 0;

	while (text[i] != '\0')
	{
		if (text[i] == ':')
		{
			text[i] = '\0';
			break;
		}
		i++;
	}
}

int NajdiDalsi(char *text, char *token, int i)
{
	int j = 0;

	token[0] = 0;

	while (text[i] == ' ' || text[i] == '\t')
		i++;

	while ( (text[i] >= 'A' && text[i] <= 'Z') || 
			(text[i] >= 'a' && text[i] <= 'z') || 
			(text[i] >= '0' && text[i] <= '9') || text[i] == '_')
	{
		token[j++] = text[i++];
		token[j] = 0;
	}

	if (text[i] == '\0' || text[i] == 10)
		return -1;

	return i;
}

void FillComboBox(HWND hwnd)
{
	LRESULT lResult;
	FILE *file;
	char buffer[100];
	char token[100];
	char fullFileName[MAX_PATH];
	int i = 0;
	char groups[50][64];
	int grpCount = 0;

	commCount = 0;

	if (!Execute("help", EX_NOLOAD))
		MessageBox(hwnd, TEXT("Chyba pri help"), TEXT("Error"), MB_ICONERROR);
	EnableInputs();

	WithAppPath(OUTPUT_FILE_NAME, fullFileName);

	file = fopen(fullFileName, "r");

	fgets(buffer, sizeof (buffer) - 1, file);	/* Welcome to ABC! */

	while (fgets(buffer, sizeof (buffer) - 1, file) != NULL)
	{
		if (buffer[0] >= 'A' && buffer[0] <= 'Z')
		{
			grpCount++;
			strcpy(groups[grpCount], buffer);
			OdstranDvojtecku(groups[grpCount]);
		}
		else
		{
			i = 0;
			while (i >= 0)
			{
				i = NajdiDalsi(buffer, token, i);
				if (token[0] != 0)
				{
					commWz[commCount].group = grpCount;
					strncpy(commWz[commCount].name, token, sizeof (commWz[commCount].name) - 1);
					commCount++;
				}
			}
		}
	}

	fclose(file);

	SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, WM_SETREDRAW, (WPARAM)FALSE, 0); // Optimalizace
	lResult = SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, CB_ADDSTRING, 0, (LPARAM)"<All groups>");
	if (lResult == CB_ERR || lResult == CB_ERRSPACE)
		MessageBox(hwnd, TEXT("Nelze naplnit combo box"), TEXT("Chyba"), MB_ICONERROR);
	for (i = 1; i < grpCount; i++)
	{
		lResult = SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, CB_ADDSTRING, 0, (LPARAM)groups[i]);
		if (lResult == CB_ERR || lResult == CB_ERRSPACE)
			MessageBox(hwnd, TEXT("Nelze naplnit combo box"), TEXT("Chyba"), MB_ICONERROR);
	}
	SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, CB_SETCURSEL, 0, 0);
	SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, WM_SETREDRAW, (WPARAM)TRUE, 0); // Optimalizace


	SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, WM_SETREDRAW, (WPARAM)FALSE, 0); // Optimalizace
	for (i = 0; i < commCount; i++)
	{
		lResult = SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_ADDSTRING, 0, (LPARAM)commWz[i].name);
		if (lResult == CB_ERR || lResult == CB_ERRSPACE)
			MessageBox(hwnd, TEXT("Nelze naplnit combo box"), TEXT("Chyba"), MB_ICONERROR);
	}
	SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_SETCURSEL, 0, 0);
	SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, WM_SETREDRAW, (WPARAM)TRUE, 0); // Optimalizace
}

void ZmenaCommandCombo(HWND hwnd)
{
	char prikaz[64];
	char tmp[64];
	int vyber;

	vyber = SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_GETCURSEL, 0, 0);
	if (vyber == CB_ERR)
		MessageBox(hwnd, TEXT("Combo box vyber"), TEXT("Error"), MB_ICONERROR);

	if (SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_GETLBTEXT, vyber, (LPARAM)tmp) == CB_ERR)
		MessageBox(hwnd, TEXT("Combo box"), TEXT("Error"), MB_ICONERROR);
	else
	{
		sprintf(prikaz, "%s -h", tmp);
		Execute(prikaz, EX_WIZARD | EX_NOLOAD);
		EnableInputs();
		
		SetWindowText(g_hwndStatusBar, TEXT("Ready"));

		sprintf(prikaz, "%s ", tmp);
		SetDlgItemText(hwnd, IDC_EDIT_COMMANDHELP, prikaz);
		SetFocus(GetDlgItem(hwnd, IDC_EDIT_COMMANDHELP));
		SendDlgItemMessage(hwnd, IDC_EDIT_COMMANDHELP, EM_SETSEL, 0, (LPARAM)-1); // Nastaveni karetu
		SendDlgItemMessage(hwnd, IDC_EDIT_COMMANDHELP, EM_SETSEL, (WPARAM)-1, 0); // Nastaveni karetu
	}
}

void ZmenaGroupCombo(HWND hwnd)
{
	int vyber;
	int i;
	LRESULT lResult;

	SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_RESETCONTENT, 0, 0);

	vyber = SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, CB_GETCURSEL, 0, 0);
	if (vyber == CB_ERR)
		MessageBox(hwnd, TEXT("Combo group box vyber"), TEXT("Error"), MB_ICONERROR);
	
	for (i = 0; i < commCount; i++)
	{
		if (commWz[i].group == vyber || vyber == 0)
		{
			lResult = SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_ADDSTRING, 0, (LPARAM)commWz[i].name);
			if (lResult == CB_ERR || lResult == CB_ERRSPACE)
				MessageBox(hwnd, TEXT("Nelze naplnit combo box"), TEXT("Chyba"), MB_ICONERROR);
		}
	}
	SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_SETCURSEL, 0, 0);
}

void ResetContent(HWND hwnd)
{
	SendDlgItemMessage(hwnd, IDC_COMBO_GROUPHELP, CB_RESETCONTENT, 0, 0);
	SendDlgItemMessage(hwnd, IDC_COMBO_COMMANDHELP, CB_RESETCONTENT, 0, 0);
}

/* Procedura dialogu Wizard */
INT_PTR CALLBACK DialogWizard(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char prikaz[COMMAND_SIZE];

	switch (uMsg)
	{
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDCANCEL:
					ResetContent(hwndDlg);
					EndDialog(hwndDlg, IDCANCEL);
					break;
				case IDOK:
					GetDlgItemText(hwndDlg, IDC_EDIT_COMMANDHELP, prikaz, COMMAND_SIZE - 1);
					SendMessage(hEditMain, CB_INSERTSTRING, 0, (LPARAM)prikaz);
					SetFocus(g_hwndStatusBar);	/* hEditMain musi ztratit focus */
					StartExecute(prikaz, EX_WITHOUTPUTS);

					ResetContent(hwndDlg);
					EndDialog(hwndDlg, IDOK);					
					break;
				case IDC_BUTTON_SELECT:
					ZmenaCommandCombo(hwndDlg);					
					break;
				case IDC_COMBO_COMMANDHELP:
					if (HIWORD(wParam) == CBN_SELCHANGE)
					{
						//ZmenaCommandCombo(hwndDlg);
					}
					break;
				case IDC_COMBO_GROUPHELP:
					if (HIWORD(wParam) == CBN_SELCHANGE)
					{
						ZmenaGroupCombo(hwndDlg);
						//ZmenaCommandCombo(hwndDlg);
					}
					break;
			}
			break;
		case WM_INITDIALOG:
			hWizardEditHelp = GetDlgItem(hwndDlg, IDC_EDIT_HELP);
			CenterWindow(hwndDlg);

			FillComboBox(hwndDlg);

			ZmenaCommandCombo(hwndDlg);

			break;
	}
	return FALSE;
}
