#include <windows.h>
#include <commctrl.h>
#include <vector>
#include "resource.h"
#include "readpak.h"
#include "gui_things.h"
#include "qbkey32.h"

BOOL CALLBACK MainWindowProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	HWND hDlg;
	MSG  msg;
	int status;

	initCRC32(); // Init CRC table

	hDlg = CreateDialog(hThisInstance, MAKEINTRESOURCE(MAINDIALOG), 0, MainWindowProc);

	while ((status = GetMessage(&msg, 0, 0, 0)) != 0)
	{
		if (status == -1)
			return -1;

		if (!IsDialogMessage (hDlg, &msg))
		{
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
	}

	return 0;
}

BOOL CALLBACK MainWindowProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static HINSTANCE hInstance;
	static PAKReturn pakInfo;
	static BOOL fileOpened;

	switch (msg)
	{
		case WM_INITDIALOG:
			{
				InitCommonControls();
				hInstance = (HINSTANCE) GetWindowLong(hDlg, GWL_HINSTANCE);
				fileOpened = FALSE;
				pakInfo.pabFile = INVALID_HANDLE_VALUE;
				return TRUE;
			}
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDC_LOADBINDBG:
					if (fileOpened == TRUE)
					{
						if (ReadDbgInfo(hDlg, &pakInfo) == TRUE)
							MakeTree(hInstance, hDlg, &pakInfo);
					}
					break;
				case IDC_UNLOAD:
					if (fileOpened == TRUE)
					{
						FreePakInfo(hDlg, &pakInfo, CLEAN_ALL);
						fileOpened = FALSE;
					}
					break;
				case IDC_EXIT:
					SendMessage(hDlg, WM_CLOSE, 0, 0);
					break;
				case IDC_ABOUTDLG:
					DialogBox(hInstance, MAKEINTRESOURCE(ABOUTDIALOG), hDlg, AboutDlgProc);
					break;
				case IDC_MAIN_OPEN: // Open file
					if (HIWORD(wParam) == BN_CLICKED)
					{
						if (fileOpened == TRUE)
						{
							FreePakInfo(hDlg, &pakInfo, CLEAN_ALL);
							fileOpened = FALSE;
						}

						if (RequestPakName(hDlg, &pakInfo, NAME_PAK) == TRUE)
						{
							if (ReadPakFiles(hDlg, &pakInfo) == TRUE)
							{
								MakeTree(hInstance, hDlg, &pakInfo);
								fileOpened = TRUE;
							}
						}
					}

					break;
				case IDC_MAIN_EXTRACT:
					if (fileOpened == TRUE && HIWORD(wParam) == BN_CLICKED)
						ExploreSelectedItem(hDlg, &pakInfo, SELECT_MODE_EXTRACT);
					break;
				case IDC_MAIN_REPLACE:
					if (fileOpened == TRUE && HIWORD(wParam) == BN_CLICKED)
						ExploreSelectedItem(hDlg, &pakInfo, SELECT_MODE_REPLACE);
					break;
				case IDC_MAIN_SAVE:
					if (fileOpened == TRUE && HIWORD(wParam) == BN_CLICKED)
					{
						if (SavePakFiles(hDlg, &pakInfo) == TRUE)
						{
							FreePakInfo(hDlg, &pakInfo, CLEAN_PAKINFO);
							if (ReOpenPakAndPab(hDlg, &pakInfo) == FALSE)
							{
								fileOpened = FALSE;
								ExitProcess(-2);
							}
							else
							{
								if (ReadPakFiles(hDlg, &pakInfo) == FALSE)
								{
									fileOpened = FALSE;
								}
								else
								{
									MakeTree(hInstance, hDlg, &pakInfo);
								}
							}
						}							
					}
					break;
				case IDC_MAIN_CLOSE: // Close window
					if (HIWORD(wParam) == BN_CLICKED)
						SendMessage(hDlg, WM_CLOSE, 0, 0);
					break;
			}
			return 0;
			break;
		case WM_CLOSE:
			if (fileOpened == TRUE)
			{
				FreePakInfo(hDlg, &pakInfo, CLEAN_ALL);
				fileOpened = FALSE;
			}
			DestroyWindow(hDlg);
			return 0;
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
			break;
	}
	return FALSE;
}

BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_INITDIALOG:
			return TRUE;
		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK && HIWORD(wParam) == BN_CLICKED)
				EndDialog(hDlg, FALSE);
			return TRUE;
		case WM_CLOSE:
			EndDialog(hDlg, FALSE);
			return TRUE;
	}
	return FALSE;
}
