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

#define _TimerClock 1

extern void * pAbc;
extern HWND g_hWnd;
extern HWND hwndOutput;
extern HWND hEditMain;
extern HWND g_hwndStatusBar;
extern BOOL isWorking;
extern BOOL timeInStatusbar;
extern UINT elapsedSeconds;

extern FILE * outStream;

extern HWND hWizardEditHelp;

extern int Cmd_CommandExecute(void * pAbc, char * sCommand);
extern void SetOutStream(void * pAbc);
extern BOOL LoadFile(LPCTSTR lpFileName, HWND hWndTo, char *commandText);
extern BOOL LoadFileToString(LPCTSTR lpFileName, char *string);
extern void EnableInputs();
extern void DisableInputs();
extern void WithAppPath(char *fileName, char *fullFileName);

HANDLE g_hThread = NULL;
char comToExe[COMMAND_SIZE];

BOOL Execute(char *prikaz, int typeOfExec)
{
	int err;
	HWND hOutEdit;
	char tmp[COMMAND_SIZE + 64];	/* +64: Rezerva */
	char fullFileName[MAX_PATH];
	
	if (isWorking)
	{
		sprintf(tmp, "Can't execute \"%s\"\nWinABC is already processing now!", prikaz);
		MessageBox(g_hWnd, tmp, TEXT("Error"), MB_ICONWARNING);
		return FALSE;
	}

	DisableInputs();
	sprintf(tmp, "Processing \"%s\"   Please wait...", prikaz);
	SetWindowText(g_hwndStatusBar, tmp);

	WithAppPath(OUTPUT_FILE_NAME, fullFileName);
	outStream = fopen(fullFileName, "w");
	if (outStream == NULL)
	{
		MessageBox(g_hWnd, TEXT("Can't write to output file!"), TEXT("Error"), MB_ICONERROR);
		return FALSE;
	}
	SetOutStream(pAbc);

	err = Cmd_CommandExecute(pAbc, prikaz);
	
	fclose(outStream);
	if (!(typeOfExec & EX_NOLOAD))
	{
		hOutEdit = GetDlgItem(hwndOutput, IDC_EDIT_OUTPUT);
		LoadFile(fullFileName, hOutEdit, prikaz);

		if (GetWindowTextLength(hOutEdit) > 0)
		{
			SendMessage(g_hWnd, WM_COMMAND, ID_SHOWOUTPUT, 0);
			SendMessage(hOutEdit, EM_SETSEL, 0, (LPARAM)-1); // Nastaveni karetu
			SendMessage(hOutEdit, EM_SETSEL, (WPARAM)-1, 0);
		}
	}
	
	if (typeOfExec & EX_WIZARD)
	{	
		LoadFile(fullFileName, hWizardEditHelp, prikaz);
	}
	
	if (err) return FALSE;

	return TRUE;
}

void ShowElapsedTime()
{
	char text[100];
	char fullFileName[MAX_PATH];
	if (!timeInStatusbar) return;
	KillTimer(g_hWnd, _TimerClock);
	if (!Execute("time", EX_NOLOAD))
		MessageBox(g_hWnd, TEXT("Chyba pri time"), TEXT("Error"), MB_ICONERROR);
	EnableInputs();
	WithAppPath(OUTPUT_FILE_NAME, fullFileName);
	LoadFileToString(fullFileName, text);
	SendMessage(g_hwndStatusBar, SB_SETTEXT, 1, (LPARAM)text);
}

DWORD WINAPI ExecuteThread(LPVOID lpParameter)
{	
	char tmp[COMMAND_SIZE + 64];
	int typeOfExec = (int)lpParameter;

	if (timeInStatusbar)
	{
		elapsedSeconds = 0;
		SetTimer(g_hWnd, _TimerClock, 1000, NULL);
	}
	if (Execute(comToExe, typeOfExec))
	{
		EnableInputs();
		if (typeOfExec & EX_OPENNOTEPAD)
		{
			char lpFile[MAX_PATH];
			WithAppPath("temp.tmp", lpFile);
			ShellExecute(g_hWnd, TEXT("open"), TEXT("notepad.exe"), lpFile, NULL, SW_SHOW);
		}
		if (typeOfExec & EX_WITHOUTPUTS)
		{
			SendMessage(g_hWnd, WM_COMMAND, ID_SHOWSTATISTICS, 0);
		}
		else
		{
			ShowElapsedTime();
		}
		sprintf(tmp, "Command \"%s\" successfully done.", comToExe);
		SetWindowText(g_hwndStatusBar, tmp);
		SendMessage(g_hWnd, UM_SETFOCUS_COMBO, 0, 0);
		CloseHandle(g_hThread);
		return 0;
	}
	EnableInputs();
	ShowElapsedTime();
	sprintf(tmp, "Command \"%s\" done.", comToExe);
	SetWindowText(g_hwndStatusBar, tmp);
	SendMessage(g_hWnd, UM_SETFOCUS_COMBO, 0, 0);
	CloseHandle(g_hThread);
	return 0;
}

void StartExecute(char *prikaz, int typeOfExec)
{
	DWORD dwThreadId;
	strcpy(comToExe, prikaz);
	g_hThread = CreateThread(NULL, 0, ExecuteThread, (LPVOID)typeOfExec, 0, &dwThreadId);
	if (!g_hThread)
		MessageBox(g_hWnd, TEXT("Nepodarilo se vytvorit vlakno pro vypocet"), TEXT("Error"), MB_ICONERROR);
}
