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

HANDLE hThreadPrint = NULL;

extern HWND g_hwndStatusBar;
extern HWND g_hWnd;
extern HWND hEditPrint[MAX_OUTPUTS];
extern HWND hCheckPrint[MAX_OUTPUTS];
extern char command[MAX_OUTPUTS][COMMAND_SIZE];
extern int numEdits;
extern HWND hEditMain;
extern void StartExecute(char *prikaz, int withOutputs);
extern BOOL Execute(char *prikaz, int typeOfExec);
extern void EnableInputs();
extern void WithAppPath(char *fileName, char *fullFileName);
extern void ShowElapsedTime();

void ReplaceForbiddenChars(char *text)
{
	int i = 0;
	while (text[i] != '\0')
	{
		switch (text[i])
		{
			case '<':
			case '>':
			case '?':
			case '/':
			case '\\':
			case ':':
			case '\"':
			case '*':
			case '|':
				text[i] = '_';
				break;
			default:
				break;
		}
		i++;
	}
}

BOOL LoadFile(LPCTSTR lpFileName, HWND hWndTo, char *commandText)
{
	char newFileName[MAX_PATH + 64];
	char text[MAX_PATH];
	HANDLE hFile;
	DWORD dwSize;
	DWORD dw;
	LPBYTE lpBuffer = NULL;
	hFile = CreateFile(lpFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
		return FALSE;
	dwSize = GetFileSize(hFile, NULL);
	lpBuffer = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, dwSize + 1);
	if (!ReadFile(hFile, lpBuffer, dwSize, &dw, NULL))
	{
		CloseHandle(hFile);
		HeapFree(GetProcessHeap(), 0, lpBuffer);
		return FALSE;
	}
	CloseHandle(hFile);
	lpBuffer[dwSize] = 0;
	SetWindowText(hWndTo, (LPCTSTR)lpBuffer);

	if (dwSize >= 64000)
	{
		sprintf(text, "%s.txt", commandText);
		ReplaceForbiddenChars(text);
		WithAppPath(text, newFileName);
		CopyFile(lpFileName, newFileName, FALSE);
		sprintf(newFileName, "Output is too large! Output saved to file \"%s\"", text);
		SetWindowText(hWndTo, newFileName);
	}

	if (!HeapFree(GetProcessHeap(), 0, lpBuffer))
		return FALSE;
	return TRUE;
}

BOOL LoadFileToString(LPCTSTR lpFileName, char *string)
{
	HANDLE hFile;
	DWORD dwSize;
	DWORD dw;
	LPBYTE lpBuffer = NULL;
	hFile = CreateFile(lpFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
		return FALSE;
	dwSize = GetFileSize(hFile, NULL);
	lpBuffer = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, dwSize + 1);
	if (!ReadFile(hFile, lpBuffer, dwSize, &dw, NULL))
	{
		CloseHandle(hFile);
		HeapFree(GetProcessHeap(), 0, lpBuffer);
		return FALSE;
	}
	CloseHandle(hFile);
	lpBuffer[dwSize] = 0;
	strcpy(string, lpBuffer);

	if (!HeapFree(GetProcessHeap(), 0, lpBuffer))
		return FALSE;
	return TRUE;
}

DWORD WINAPI PrintOutputs(LPVOID lpParameter)
{
	int i;
	char fullFileName[MAX_PATH];
	WithAppPath(OUTPUT_FILE_NAME, fullFileName);  // Treba poresit ... staci nacist jen jednou pri startu

	for (i = 0; i < numEdits; i++)
	{
		if (SendMessage(hCheckPrint[i], BM_GETCHECK, 0, 0) == BST_CHECKED)
		{
			EnableWindow(hEditPrint[i], TRUE);

			Execute(command[i], EX_NOLOAD);

			EnableInputs();
			
			LoadFile(fullFileName, hEditPrint[i], command[i]);
		}
		else
		{
			EnableWindow(hEditPrint[i], FALSE);
		}
	}

	ShowElapsedTime();

	SetWindowText(hEditMain, TEXT(""));
	SetWindowText(g_hwndStatusBar, TEXT("Ready"));

	SendMessage(g_hWnd, UM_SETFOCUS_COMBO, 0, 0);

	CloseHandle(hThreadPrint);
	return 0;
}

void StartPrintOutputs(HWND hWnd)
{
	DWORD dwThreadId;
	hThreadPrint = CreateThread(NULL, 0, PrintOutputs, (LPVOID)NULL, 0, &dwThreadId);
	if (!hThreadPrint)
		MessageBox(hWnd, TEXT("Nepodarilo se vytvorit vlakno pro vystup"), TEXT("Error"), MB_ICONERROR);
}
