/* mingw hack */
#ifdef UNICODE
#define NTDDI_VERSION 0x06000000
#define _WIN32_WINNT  0x0600
#else
#define _WIN32_WINNT  0x0400
#endif

#include <windef.h>
#include <winbase.h>
#include <shlwapi.h>

#include <stdint.h>
#include <stdio.h>

#include <tchar.h> /* tchar versions of string.h functions */

#include "common.h"

/* make sure this is defined... */
#ifndef _MAX_ULTOSTR_BASE10_COUNT
#define _MAX_ULTOSTR_BASE10_COUNT (10 + 1)
#endif

/* put all of our types in an array */
static const enum types types[] = { TYPES_VF, TYPES_VEG };

/* HWND ids */
enum {
	OPEN_FILE_BUTTON = 0,
	COMBOBOX,
	LISTBOX,
	SAVE_FILE_BUTTON,
	VERSION
};

#if UNICODE
/* use COM when `UNICODE=1` to avoid file paths being cut off */
#include <shobjidl.h>

static const DWORD COM_INITFLAGS = (COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
#endif

/* user-adjustable. set these values to whatever you want, the GUI will scale */
#define WINDOW_WIDTH  225U
#define WINDOW_HEIGHT 200U

static LPTSTR file_path = NULL;

/* these values are loaded by AddControls() if they're valid */
static uint8_t version = 11;
static enum types type = TYPES_VEG;

/* we edit this from display_file() */
static HWND hwnd_version = NULL;

static inline LPCTSTR type_to_prefix(enum types type) {
	switch (type) {
		case TYPES_VF: return TEXT("MS");
		case TYPES_VEG: return TEXT("PRO");
		case TYPES_UNKNOWN:
		default: return TEXT("UNK");
	}
}

static inline LPCTSTR type_to_string(enum types type) {
	switch (type) {
		case TYPES_VF: return TEXT("Movie Studio");
		case TYPES_VEG: return TEXT("Vegas Pro");
		case TYPES_UNKNOWN:
		default: return TEXT("Unknown");
	}
}

static inline LPCTSTR type_to_extension(enum types type) {
	switch (type) {
		case TYPES_VF: return TEXT("vf");
		case TYPES_VEG:
		case TYPES_UNKNOWN:
		default: return TEXT("veg");
	}
}

/* these functions are designed to *not* use global variables,
 * to make everything a bit more simple... */
static int display_file(LPCTSTR path) {
	/* Read the file to memory */
	FILE* file = _tfopen(path, TEXT("rb"));

	uint8_t file_version = 0;
	enum types file_type = TYPES_UNKNOWN;

	get_file_information(file, &file_version, &file_type);

	int needed = _sntprintf(NULL, 0, TEXT("File version: %s %u"), type_to_string(file_type), file_version);
	LPTSTR text = calloc(needed + 1, sizeof(TCHAR));
	if (!text) /* out of memory... lol */
		exit(1);
	_sntprintf(text, needed + 1, TEXT("File version: %s %u"), type_to_string(file_type), file_version);

	if (!SendMessage(hwnd_version, WM_SETTEXT, (WPARAM)0, (LPARAM)text)) {
		free(text);
		return -1;
	}

	free(text);
	fclose(file);

	return 0;
}

static int open_file(HWND hWnd, LPTSTR* filepath) {
#if UNICODE
	if (CoInitializeEx(NULL, COM_INITFLAGS) != S_OK)
		return -1; /* what */

	COMDLG_FILTERSPEC filters[] = {{L"Project files", L"*.veg;*.vf"}, {L"All files", L"*.*"}};

	IFileDialog* pfd = NULL;
	IShellItem* result = NULL;

	if (SUCCEEDED(CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog, (LPVOID*)&pfd))) {
		pfd->lpVtbl->SetFileTypes(pfd, ARRAYSIZE(filters), filters);
		pfd->lpVtbl->SetFileTypeIndex(pfd, 1);
		pfd->lpVtbl->Show(pfd, hWnd);

		if (!SUCCEEDED(pfd->lpVtbl->GetResult(pfd, &result)) || !result) {
			pfd->lpVtbl->Release(pfd);
			return -1;
		}

		if (!SUCCEEDED(result->lpVtbl->GetDisplayName(result, SIGDN_FILESYSPATH, filepath))) {
			pfd->lpVtbl->Release(pfd);
			result->lpVtbl->Release(result);
			*filepath = NULL; /* might memleak? */
			return -1;
		}

		result->lpVtbl->Release(result);
		pfd->lpVtbl->Release(pfd);
	} else {
#endif
		/* initialize our buffer */
		*filepath = calloc(MAX_PATH + 1, sizeof(TCHAR));

		OPENFILENAME ofn = {0};

		/* NT 4.0 compat */
		ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
		ofn.hwndOwner = hWnd;
		ofn.lpstrFile = *filepath;
		ofn.nMaxFile = MAX_PATH;
		ofn.lpstrFilter = TEXT("Project files\0*.veg;*.vf\0All files\0*.*\0");

		if (!GetOpenFileName(&ofn))
			return -1;
#if UNICODE
	}

	CoUninitialize();
#endif

	return display_file(*filepath);;
}

static int save_file(HWND hWnd, LPCTSTR input, uint8_t version, enum types type) {
	if (!input) {
		MessageBox(hWnd,
					TEXT("Please open a file first!"),
					TEXT("Invalid input file!"),
					MB_ICONEXCLAMATION);
		return -1;
	}

	LPTSTR output_template = NULL;
	LPTSTR output = NULL;

	{
		LPTSTR input_basename = PathFindFileName(input);
		int input_basename_len = PathFindExtension(input_basename) - input_basename;

		int needed = _sntprintf(NULL, 0, TEXT("%s_V%u_%.*s.%s"), type_to_prefix(type), version, input_basename_len, input_basename, type_to_extension(type));
		output_template = calloc(needed + 1, sizeof(TCHAR));
		_sntprintf(output_template, needed + 1, TEXT("%s_V%u_%.*s.%s"), type_to_prefix(type), version, input_basename_len, input_basename, type_to_extension(type));

		free(input_basename);
	}

	{
		/* File dialog */
#if UNICODE
		int com_initialized = SUCCEEDED(CoInitializeEx(NULL, COM_INITFLAGS));

		COMDLG_FILTERSPEC filters[] = {{L"Movie Studio project files", L"*.vf"}, {L"Vegas Pro project files", L"*.veg"}, {L"All files", L"*.*"}};

		IFileDialog* pfd = NULL;
		IShellItem* result = NULL;
		if (com_initialized && SUCCEEDED(CoCreateInstance(&CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, &IID_IFileSaveDialog, (LPVOID*)&pfd))) {
			pfd->lpVtbl->SetFileTypes(pfd, ARRAYSIZE(filters), filters);
			pfd->lpVtbl->SetFileTypeIndex(pfd, (type == TYPES_UNKNOWN) ? ARRAYSIZE(filters) : type - TYPES_UNKNOWN);
			pfd->lpVtbl->SetFileName(pfd, output_template);
			pfd->lpVtbl->Show(pfd, hWnd);

			if (!SUCCEEDED(pfd->lpVtbl->GetResult(pfd, &result))) {
				pfd->lpVtbl->Release(pfd);
				free(output_template);
				return -1;
			}

			if (!SUCCEEDED(result->lpVtbl->GetDisplayName(result, SIGDN_FILESYSPATH, &output))) {
				pfd->lpVtbl->Release(pfd);
				result->lpVtbl->Release(result);
				free(output_template);
				output = NULL; /* might memleak ? */
				return -1;
			}

			result->lpVtbl->Release(result);
			pfd->lpVtbl->Release(pfd);
			free(output_template);
		} else {
#endif
			/* fallback to OPENFILENAME if COM fucks up for whatever reason (or we're on ANSI)... */
			output = calloc(MAX_PATH + 1, sizeof(TCHAR));
			_tcsncpy(output, output_template, MAX_PATH);
			free(output_template);

			OPENFILENAME ofn = {0};

			ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
			ofn.hwndOwner = hWnd;
			ofn.lpstrFile = output;
			ofn.nMaxFile = MAX_PATH;
			ofn.lpstrFilter = TEXT("Movie Studio project files\0*.vf\0Vegas Pro project files\0*.veg\0All files\0*.*\0");
			ofn.nFilterIndex = (type == TYPES_UNKNOWN) ? 3 : type - TYPES_UNKNOWN;

			if (!GetSaveFileName(&ofn))
				return -1;
#if UNICODE
		}

		if (com_initialized)
			CoUninitialize();
#endif
	}

	if (!CopyFile(input, output, 0)) {
		MessageBox(hWnd, TEXT("Failed to copy original project file! Does the destination file already exist?"), TEXT("Saving project failed!"), MB_ICONEXCLAMATION | MB_OK);
		free(output);
		return -1;
	}

	FILE* output_file = _tfopen(output, TEXT("ab"));
	if (!output_file) {
		MessageBox(hWnd, TEXT("Failed to save project file!"), TEXT("Saving project failed!"), MB_ICONEXCLAMATION | MB_OK);
		free(output);
		return -1;
	}

	free(output);

	set_file_information(output_file, version, type);

	fclose(output_file);

	return 0;
}

/* TODO: use a resource file instead... */
void AddControls(HWND hWnd) {
	/* Open File */
	HWND open_button = CreateWindow(TEXT("Button"), TEXT("Open"), WS_VISIBLE | WS_CHILD, WINDOW_WIDTH * 7 / 18, WINDOW_HEIGHT / 40, WINDOW_WIDTH * 2 / 9, WINDOW_HEIGHT / 10, hWnd, (HMENU)OPEN_FILE_BUTTON, NULL, NULL);

	/* Versions */
	HWND combobox = CreateWindow(TEXT("ComboBox"), NULL,
								 CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | WS_VSCROLL,
								 WINDOW_WIDTH * 7 / 18, WINDOW_HEIGHT * 3 / 20, WINDOW_WIDTH * 2 / 9, WINDOW_HEIGHT, 
								 hWnd, (HMENU)COMBOBOX, NULL, NULL);

	for (size_t v = 8; v <= 21; v++) {
		TCHAR v_str[_MAX_ULTOSTR_BASE10_COUNT] = {'\0'};

		_ultot(v, v_str, 10);

		LRESULT pos = SendMessage(combobox, CB_ADDSTRING, 0, (LPARAM)v_str);
		SendMessage(combobox, CB_SETITEMDATA, pos, v);
		if (v == version)
			SendMessage(combobox, CB_SETCURSEL, pos, 0);
	}

	/* Type */
	HWND listbox = CreateWindow(TEXT("Listbox"), NULL, WS_VISIBLE | WS_CHILD | LBS_STANDARD | LBS_NOTIFY, WINDOW_WIDTH * 5 / 18, WINDOW_HEIGHT * 11 / 40, WINDOW_WIDTH * 4 / 9, WINDOW_HEIGHT / 5, hWnd, (HMENU)LISTBOX, NULL, NULL);

	for (size_t i = 0; i < ARRAYSIZE(types); i++) {
		LRESULT pos = SendMessage(listbox, LB_ADDSTRING, i, (LPARAM)type_to_string(types[i]));
		SendMessage(listbox, LB_SETITEMDATA, pos, types[i]);
		if (types[i] == type)
			SendMessage(listbox, LB_SETCURSEL, pos, 0);
	}

	/* Save File */
	HWND save_button = CreateWindow(TEXT("Button"), TEXT("Save"), WS_VISIBLE | WS_CHILD, WINDOW_WIDTH * 7 / 18, WINDOW_HEIGHT * 9 / 20, WINDOW_WIDTH * 2 / 9, WINDOW_HEIGHT / 10, hWnd, (HMENU)SAVE_FILE_BUTTON, NULL, NULL);

	/* Version and Type display */
	hwnd_version = CreateWindow(TEXT("Edit"), TEXT("No file opened!"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY | ES_CENTER | ES_MULTILINE | SS_CENTER, WINDOW_WIDTH / 6, WINDOW_HEIGHT * 3 / 5, WINDOW_WIDTH * 2 / 3, WINDOW_HEIGHT / 5, hWnd, (HMENU)VERSION, NULL, NULL);
	if (!open_button || !save_button || !listbox || !combobox || !hwnd_version)
		MessageBox(hWnd, TEXT("how did you even trigger this"), TEXT("GUI could not be initialized!"), MB_ICONEXCLAMATION); 
}

/* Make the fonts not as ugly */
BOOL CALLBACK SetFont(HWND child, LPARAM font) {
	SendMessage(child, WM_SETFONT, font, 1);
	return 1;
}

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch(msg) {
		case WM_COMMAND:
			if (HIWORD(wParam) == CBN_SELCHANGE) {
				switch (LOWORD(wParam)) {
					case COMBOBOX: {
						LRESULT i = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
						version = (uint8_t)SendMessage((HWND)lParam, CB_GETITEMDATA, i, 0);
						break;
					}
					case LISTBOX: {
						LRESULT i = SendMessage((HWND)lParam, LB_GETCURSEL, 0, 0);
						type = (enum types)SendMessage((HWND)lParam, LB_GETITEMDATA, i, 0);
						break;
					}
					default:
						break;
				}
			}

			switch (wParam) {
				case OPEN_FILE_BUTTON:
					/* free(NULL) == no-op */
					free(file_path);
					open_file(hWnd, &file_path);
					break;
				case SAVE_FILE_BUTTON:
					save_file(hWnd, file_path, version, type);
					break;
				default:
					break;
			}
			break;
		case WM_CREATE:
			AddControls(hWnd);
			EnumChildWindows(hWnd, SetFont, (LPARAM)GetStockObject(DEFAULT_GUI_FONT));
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		case WM_DROPFILES: {
			/* Drag and drop support */
			free(file_path);

			HDROP drop = (HDROP)wParam;

			int needed = DragQueryFile(drop, 0, NULL, 0);
			file_path = malloc((needed + 1) * sizeof(TCHAR));
			DragQueryFile(drop, 0, file_path, needed + 1);
			file_path[needed] = L'\0';

			display_file(file_path);
			break;
		}
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int ncmdshow) {
	WNDCLASS wc = {0};

	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hInstance = hInstance;
	wc.lpszClassName = TEXT("msvpvf");
	wc.lpfnWndProc = WindowProcedure;

	if (!RegisterClass(&wc)) return -1;

	CreateWindowEx(WS_EX_ACCEPTFILES, TEXT("msvpvf"), TEXT("Movie Studio / Vegas Pro version spoofer"), WS_OVERLAPPED | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);

	MSG msg = {0};

	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
