#include "msDexiExporter.hpp"
#include "resource.h"

#pragma warning (push, 3)
#include <windows.h>

#pragma warning (pop)

BOOL CALLBACK DexiOptionsDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	BOOL result = FALSE;
	switch (uMsg)
	{
	default:
		break;
	case WM_INITDIALOG:
		{
			DexiOptions* pData = (DexiOptions*)(lParam);
			SetWindowLong(hwnd, GWL_USERDATA, (LONG)(pData));
			SendDlgItemMessage(hwnd, IDC_BTACTOR, BM_SETCHECK, BST_CHECKED, 0);
			SendDlgItemMessage(hwnd, IDC_BTDWORLD, BM_SETCHECK, BST_UNCHECKED, 0);
			SendDlgItemMessage(hwnd, IDC_BTEXPORTANIM, BM_SETCHECK, BST_CHECKED, 0);
			SendDlgItemMessage(hwnd, IDC_BTANIMONLY, BM_SETCHECK, BST_UNCHECKED, 0);
			SendDlgItemMessage(hwnd, IDC_BTOVERWRITE, BM_SETCHECK, BST_UNCHECKED, 0);
			RECT WindowRect;
			HWND ParentWindow = GetParent(hwnd);
			GetWindowRect(ParentWindow, &WindowRect);
			POINT newpos;
			newpos.x = (WindowRect.right - WindowRect.left)/2;
			newpos.y = (WindowRect.bottom - WindowRect.top)/2;
			GetWindowRect(hwnd, &WindowRect);
			newpos.x -= (WindowRect.right - WindowRect.left)/2;
			newpos.y -= (WindowRect.bottom - WindowRect.top)/2;
			MoveWindow(hwnd, newpos.x, newpos.y, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, FALSE);
			SetFocus(GetDlgItem(hwnd, IDC_FILENAME));
			result = TRUE;
		}
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		default:
			break;
		case IDC_BTDWORLD:
			SendDlgItemMessage(hwnd, IDC_BTACTOR, BM_SETCHECK, BST_UNCHECKED, 0);
			break;
		case IDC_BTACTOR:
			SendDlgItemMessage(hwnd, IDC_BTDWORLD, BM_SETCHECK, BST_UNCHECKED, 0);
			SetFocus(GetDlgItem(hwnd, IDC_ANIMNAME));
			break;
		case IDC_BTEXPORTANIM:
			SetFocus(GetDlgItem(hwnd, IDC_ANIMNAME));
			break;
		case IDC_BTANIMONLY:
			SetFocus(GetDlgItem(hwnd, IDC_ANIMNAME));
			break;
		case IDC_BTOVERWRITE:
			SetFocus(GetDlgItem(hwnd, IDC_ANIMNAME));
			break;
		case IDC_BTBROWSE:
			{
				OPENFILENAME ofn;
				memset (&ofn, 0, sizeof (OPENFILENAME));
				
				char szFile[MAX_FILENAME_LEN];
				char szFileTitle[MAX_FILENAME_LEN];
				char szDefExt[32] = "dexi";
				char szFilter[128] = "Destiny3D DEXI Files (*.dexi)\0*.dexi\0All Files (*.*)\0*.*\0\0";
				szFile[0] = '\0';
				szFileTitle[0] = '\0';
				
				ofn.lStructSize = sizeof (OPENFILENAME);
				ofn.lpstrDefExt = szDefExt;
				ofn.lpstrFilter = szFilter;
				ofn.lpstrFile = szFile;
				ofn.nMaxFile = MAX_FILENAME_LEN;
				ofn.lpstrFileTitle = szFileTitle;
				ofn.nMaxFileTitle = MAX_FILENAME_LEN;
				ofn.Flags = OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
				ofn.lpstrTitle = "Export MilkShape 3D ASCII";
			
				if (::GetSaveFileName (&ofn))
				{
					SendDlgItemMessage(hwnd, IDC_FILENAME, WM_SETTEXT, 0, (LPARAM)(szFile));
				}
				if (BST_CHECKED == SendDlgItemMessage(hwnd, IDC_BTACTOR, BM_GETCHECK, 0, 0) &&
					BST_CHECKED == SendDlgItemMessage(hwnd, IDC_BTEXPORTANIM, BM_GETCHECK, 0, 0))
					SetFocus(GetDlgItem(hwnd, IDC_ANIMNAME));
			}
			break;
		case IDOK:
			{
				bool isValid = true;
				DexiOptions* pData;
				LONG LResult;
				pData = (DexiOptions*)GetWindowLong(hwnd, GWL_USERDATA);
				// filename
				SendDlgItemMessage(hwnd, IDC_FILENAME, WM_GETTEXT, MAX_FILENAME_LEN, (LPARAM)(pData->FileName));
				// validate filename
				if (pData->FileName[0] == '\0')
				{
					SetFocus(GetDlgItem(hwnd, IDC_FILENAME));
					isValid = false;
				}
				// animation name
				SendDlgItemMessage(hwnd, IDC_ANIMNAME, WM_GETTEXT, MAX_FILENAME_LEN, (LPARAM)(pData->AnimName));
				// export as actor
				if (BST_CHECKED == (LResult = SendDlgItemMessage(hwnd, IDC_BTACTOR, BM_GETCHECK, 0, 0)))
				{
					pData->Target = DexiOptions::Target_Actor;
					// export animation
					if (BST_CHECKED == (LResult = SendDlgItemMessage(hwnd, IDC_BTEXPORTANIM, BM_GETCHECK, 0, 0)))
					{
						pData->ExportAnimation = true;
						// validate animation name
						if (isValid && pData->AnimName[0] == '\0')
						{
							SetFocus(GetDlgItem(hwnd, IDC_ANIMNAME));
							isValid = false;
						}
						// animation only
						if (BST_CHECKED == (LResult = SendDlgItemMessage(hwnd, IDC_BTANIMONLY, BM_GETCHECK, 0, 0)))
						{
							pData->AnimationOnly = true;
							// overwrite existing animation
							if (BST_CHECKED == (LResult = SendDlgItemMessage(hwnd, IDC_BTOVERWRITE, BM_GETCHECK, 0, 0)))
								pData->OverwriteAnim = true;
							else
								pData->OverwriteAnim = false;
						}
						else
							pData->AnimationOnly = false;
					}
					else
						pData->ExportAnimation = false;
				}
				// export as dworld
				else if (BST_CHECKED == (LResult = SendDlgItemMessage(hwnd, IDC_BTDWORLD, BM_GETCHECK, 0, 0)))
					pData->Target = DexiOptions::Target_DWorld;
				else
					isValid = false;

				if (isValid)
					EndDialog(hwnd, 1);
			}
			break;
		case IDCANCEL:
			EndDialog(hwnd, -1);
			break;
		}
		break;
	}
	return result;
}

bool DoDexiOptionDialog(HINSTANCE instance, DexiOptions* optionsData)
{
	if (!optionsData)
		return false;

	HWND ParentWindow = GetActiveWindow();
	
	optionsData->FileName[0] = '\0';
	optionsData->AnimName[0] = '\0';
	optionsData->Target = DexiOptions::Target_DWorld;
	optionsData->ExportAnimation = false;
	optionsData->AnimationOnly = false;
	optionsData->OverwriteAnim = false;
	
	int RetVal = DialogBoxParam(instance, MAKEINTRESOURCE(IDD_DEXIOPTDLG),
								ParentWindow, DexiOptionsDlgProc, (LPARAM)(optionsData));
	if (RetVal < 0)
		return false;
	
	return true;
}

