// ------------------------------------------------------------------------------------ //
//																						//
//	File: Asura_GUIModal_System.cpp														//
//																						//
//	Description: class for handling a modal style of menu page							//
//																						//
//	Created: Steve Keen - 9/5/07														//
//																						//
//	Copyright (C) 2007 Rebellion (Derby), All Rights Reserved.							//
// ------------------------------------------------------------------------------------ //

#include "Core/Asura.h"
#include "GUIMenu/Asura_GUIModal_System.h"

#ifdef ASURA_USE_GUI_MENU_SYSTEM
#ifdef ASURA_USE_GUI_MODAL_SYSTEM

#include "ChunkSystem/Asura_Chunk_Loaders.h"
#include "Interface/Asura_CommandConsole.h"
#include "GUIMenu/Asura_GUIMenu_System.h"
#include "Main/Asura_Project_Main.h"
#include "Rendering/Asura_Viewport.h"

// ---------------------------------------------------------------------------------------

bool					Asura_GUIModal_System::s_bInitialised = false;
bool					Asura_GUIModal_System::s_bDialogActive = false;
Asura_GUIModal_Options	Asura_GUIModal_System::s_eModalOptionType;
Asura_GUIModal_ReturnValues	Asura_GUIModal_System::s_eModalReturnValue;
Asura_GUIModal_QueryType Asura_GUIModal_System::s_eQueryType;

#ifdef ASURA_TOOLS
bool					Asura_GUIModal_System::s_bTestMode = false;
#endif // ASURA_TOOLS

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::Initialise( void)
{
	if( s_bInitialised == false)
	{
		Reset();
		ClearLocalVariables();

		LoadModalMenuPages();

		//Add console vars
		Asura_CommandConsole::AddVar( L"GUIModal.OptionType",  (u_int*)&s_eModalOptionType, 0, ASURA_GUIMODAL_OPTIONS_ENDOFLIST, Asura_CommandConsole::REMOVE_AT_SHUTDOWN  );
		Asura_CommandConsole::AddVar( L"GUIModal.MessageIndex",  (int*)&s_eQueryType, -1, ASURA_GUIMODAL_QUERYTYPE_ENDOFLIST, Asura_CommandConsole::REMOVE_AT_SHUTDOWN  );
		//Asura_CommandConsole::AddVar( L"GUIModal.ReturnValue",  (u_int*)&s_eModalReturnValue, 0, ASURA_GUIMODAL_RESULT_OK, Asura_CommandConsole::REMOVE_AT_SHUTDOWN  );

		Asura_CommandConsole::AddCmd( L"GUIModal.SetResult", ConsoleCmd_SetResult, Asura_CommandConsole::REMOVE_AT_SHUTDOWN);

#ifdef ASURA_TOOLS
		Asura_CommandConsole::AddVar( L"GUIModal.TestMode", &s_bTestMode, Asura_CommandConsole::REMOVE_AT_SHUTDOWN  );
#endif
		s_bInitialised = true;
	}
}

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::Reset( void)
{
	if( s_bInitialised == true)
	{
		s_eModalOptionType = ASURA_GUIMODAL_OPTIONS_NONE;
		s_eModalReturnValue = ASURA_GUIMODAL_RESULT_NOT_SET;
		s_eQueryType = ASURA_GUIMODAL_QUERYTYPE_NONE;

		Asura_GUIMenu_System::DestroyMenuPage( uASURA_MODAL_DIALOG_SMALL);
	}
}

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::Shutdown( void)
{
	if( s_bInitialised == true)
	{
		Asura_GUIMenu_System::DestroyMenuPage( uASURA_MODAL_DIALOG_SMALL);

		Asura_CommandConsole::RemoveVar( L"GUIModal.OptionType");
		Asura_CommandConsole::RemoveVar( L"GUIModal.MessageIndex");

#ifdef ASURA_TOOLS
		Asura_CommandConsole::RemoveVar( L"GUIModal.TestMode");
#endif
//		Asura_CommandConsole::RemoveVar( L"GUIModal.ReturnValue");

		s_bInitialised = false;
	}
}

// ---------------------------------------------------------------------------------------

Asura_GUIModal_ReturnValues Asura_GUIModal_System::DoModalSmall( Asura_GUIModal_QueryType eQueryType, Asura_GUIModal_Options eType, Asura_GUIModal_DefaultButton eDefaultButton)
{
	if( s_bDialogActive == false)
	{
		// -- check to see if the menu page exists, if not load it here --
		// -- The project should probably load this after a reset, along with all it's other assets.
		if( Asura_GUIMenu_System::DoesMenuPageExist( uASURA_MODAL_DIALOG_SMALL) == false)
		{
			LoadModalMenuPages();
		}

		s_eModalOptionType = eType;
		Asura_Hash_ID uDefaultButtonHash = ASURA_HASH_ID_UNSET;
	
		float fScreenWidth, fScreenHeight;

		fScreenWidth = (float)Asura_Viewport::GetWidth();
		fScreenHeight = (float)Asura_Viewport::GetHeight();

		Asura_Vector_3 xMenuPos( (fScreenWidth * 0.5f)-(320.0f * 0.5f), (fScreenHeight * 0.5f)-(200.0f * 0.5f), 0.0f);

		switch( eType)
		{
			case ASURA_GUIMODAL_OPTIONS_YESNO:	// check for yes/no 
				uDefaultButtonHash = (eDefaultButton == ASURA_GUIMODAL_BUTTON_YES) ? uASURA_MODAL_DIALOG_SMALL_YESNO_YES_BUTTON : uASURA_MODAL_DIALOG_SMALL_YESNO_NO_BUTTON;
				break;

			case ASURA_GUIMODAL_OPTIONS_RETRYCANCEL: // check for retry/cancel
				uDefaultButtonHash = (eDefaultButton == ASURA_GUIMODAL_BUTTON_RETRY) ? uASURA_MODAL_DIALOG_SMALL_RETRYCANCEL_RETRY_BUTTON : uASURA_MODAL_DIALOG_SMALL_RETRYCANCEL_CANCEL_BUTTON;
				break;

			case ASURA_GUIMODAL_OPTIONS_CONTINUECANCEL: //check for continue/cancel
				uDefaultButtonHash = (eDefaultButton == ASURA_GUIMODAL_BUTTON_CONTINUE) ? uASURA_MODAL_DIALOG_SMALL_CONTINUECANCEL_CONTINUE_BUTTON : uASURA_MODAL_DIALOG_SMALL_CONTINUECANCEL_CANCEL_BUTTON;
				break;

			case ASURA_GUIMODAL_OPTIONS_CONTINUE:
				uDefaultButtonHash = uASURA_MODAL_DIALOG_SMALL_CONTINUE_BUTTON;
				break;

			case ASURA_GUIMODAL_OPTIONS_CANCEL:
				uDefaultButtonHash = uASURA_MODAL_DIALOG_SMALL_CANCEL_BUTTON;
				break;

			case ASURA_GUIMODAL_OPTIONS_OK:
				uDefaultButtonHash = uASURA_MODAL_DIALOG_SMALL_OK_BUTTON;
				break;

			case ASURA_GUIMODAL_OPTIONS_NONE:	// no buttons for this option
			default:
				break;
			
		}
		
		if( uDefaultButtonHash != ASURA_HASH_ID_UNSET)
		{
			Asura_GUIMenu_System::OpenMenuPageOnWidget( uASURA_MODAL_DIALOG_SMALL, uDefaultButtonHash, true, false);
		}
		else
		{
			Asura_GUIMenu_System::OpenMenuPage( uASURA_MODAL_DIALOG_SMALL, true, false);
		}

		Asura_GUIMenu_System::SetMenuPageDefaultPos( uASURA_MODAL_DIALOG_SMALL, xMenuPos);

		bool bLoop = false;

		s_bDialogActive = true;
		s_eModalReturnValue = ASURA_GUIMODAL_RESULT_NOT_SET;
		s_eQueryType = eQueryType;

		do
		{
			Platform_HandleUpdate();

			bLoop = Asura_Project::MainLoop();

		}while( bLoop && s_eModalReturnValue == ASURA_GUIMODAL_RESULT_NOT_SET);

		Asura_GUIMenu_System::CloseMenuPage( uASURA_MODAL_DIALOG_SMALL);

#ifdef ASURA_TOOLS
		xMenuPos.x = xMenuPos.y = xMenuPos.z = 0.0f;
		Asura_GUIMenu_System::SetMenuPageDefaultPos( uASURA_MODAL_DIALOG_SMALL, xMenuPos);
#endif



		s_bDialogActive = false;
	}
	else
	{
		Asura_Assert( s_bDialogActive == false, "Asura_GUIModal_System::DoModalSmall - The small dialog is already active.");
		return ASURA_GUIMODAL_RESULT_DIALOG_FAILURE;
	}

	return s_eModalReturnValue;
}

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::CloseModalSmall( void)
{
	if( s_bDialogActive == false)
	{
		s_eModalReturnValue = ASURA_GUIMODAL_RESULT_NONE;
	}
}

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::ClearLocalVariables( void)
{
	s_bInitialised = false;

	s_eModalOptionType = ASURA_GUIMODAL_OPTIONS_NONE;
	s_eModalReturnValue = ASURA_GUIMODAL_RESULT_NOT_SET;
	s_eQueryType = ASURA_GUIMODAL_QUERYTYPE_NONE;
}

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::LoadModalMenuPages( void)
{
	if( Asura_GUIMenu_System::DoesMenuPageExist( uASURA_MODAL_DIALOG_SMALL) == false)
	{
		Asura_Char szFilename[256];

		Asura_PrintfToBfr( szFilename, "GUIMenu\\Modal." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
		Asura_ChunkLoading_System::LoadAsuraFile( szFilename);
	}
}

// ---------------------------------------------------------------------------------------

bool Asura_GUIModal_System::ConsoleCmd_SetResult( const Asura_Char* pcCmd, bool bInterrogate, bool bHelp )
{
	if( bHelp )
	{
		Asura_CommandConsole::Printf( L"GUIModal.SetResult" );
		Asura_CommandConsole::Printf( L"    Sets the result of which modal dialog button is clicked and exits the menu." );
	}
	else if( bInterrogate )
	{
		Asura_CommandConsole::Printf( L"GUIModal.SetResult" );
		Asura_CommandConsole::Printf( L"    Sets the result of which modal dialog button is clicked and exits the menu." );
	}
	else
	{
#ifdef ASURA_TOOLS
		if( s_bTestMode)	return true;
#endif
//		u_int uClickedVal =  atoi( pcCmd);
		u_int uClickedVal = 0;// atoi( pcCmd);
		switch( uClickedVal)
		{
			case ASURA_GUIMODAL_RESULT_NONE:
			case ASURA_GUIMODAL_RESULT_YES:
			case ASURA_GUIMODAL_RESULT_NO:
			case ASURA_GUIMODAL_RESULT_CONTINUE:
			case ASURA_GUIMODAL_RESULT_CANCEL:
			case ASURA_GUIMODAL_RESULT_RETRY:
			case ASURA_GUIMODAL_RESULT_OK:
				s_eModalReturnValue = (Asura_GUIModal_ReturnValues)uClickedVal;
				break;

			default:
				s_eModalReturnValue = ASURA_GUIMODAL_RESULT_NOT_SET;
				break;
		}

	}
	return true;
}

// ---------------------------------------------------------------------------------------

void Asura_GUIModal_System::SetResult( Asura_GUIModal_ReturnValues eReturnValue)
{
	bool bSetResult = false;

#ifdef ASURA_TOOLS
	if( s_bTestMode)	return;
#endif

	if( eReturnValue == ASURA_GUIMODAL_RESULT_NONE)	bSetResult = true;

	switch( s_eModalOptionType)
	{
		case ASURA_GUIMODAL_OPTIONS_YESNO:
			if( (eReturnValue == ASURA_GUIMODAL_RESULT_YES) ||
				(eReturnValue == ASURA_GUIMODAL_RESULT_NO))
			{
				bSetResult = true;
			}
			break;

		case ASURA_GUIMODAL_OPTIONS_RETRYCANCEL:
			if( (eReturnValue == ASURA_GUIMODAL_RESULT_RETRY) ||
				(eReturnValue == ASURA_GUIMODAL_RESULT_CANCEL))
			{
				bSetResult = true;
			}
			break;

		case ASURA_GUIMODAL_OPTIONS_CONTINUECANCEL:
			if( (eReturnValue == ASURA_GUIMODAL_RESULT_CONTINUE) ||
				(eReturnValue == ASURA_GUIMODAL_RESULT_CANCEL))
			{
				bSetResult = true;
			}
			break;

		case ASURA_GUIMODAL_OPTIONS_CONTINUE:
			if( eReturnValue == ASURA_GUIMODAL_RESULT_CONTINUE)	bSetResult = true;
			break;

		case ASURA_GUIMODAL_OPTIONS_CANCEL:
			if( eReturnValue == ASURA_GUIMODAL_RESULT_CANCEL)	bSetResult = true;
			break;

		case ASURA_GUIMODAL_OPTIONS_OK:
			if( eReturnValue == ASURA_GUIMODAL_RESULT_OK)	bSetResult = true;
			break;

		default:

			break;
	}

	if( bSetResult)
	{
		s_eModalReturnValue = eReturnValue;
	}

	Asura_Assert( bSetResult == false, "Asura_GUIModal_System::SetResult - Invalid result passed for this dialog type.");

}

// ---------------------------------------------------------------------------------------
#endif //ASURA_USE_GUI_MODAL_SYSTEM
#endif // ASURA_USE_GUI_MENU_SYSTEM
