//////////////////////////////////////////////////////////////////////////////
//
// Asura_Platform_GUIMenuInput.cpp
//
// Description: this handles the platform specific menu control system	for
// the PC-CLI platform
//
// Copyright (c) 2009 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

#ifdef ASURA_USE_GUI_MENU_SYSTEM

#include "Core/Asura_Timers.h"
//#include "Input/Asura_PlatformInput.h"
#include "Input/Asura_ControlMap.h"
#include "GUIMenu/Asura_GUIMenu_Message.h"
#include "GUIMenu/Asura_GUIMenu_System.h"
#include "GUIMenu/Asura_GUIMenu_Widget_Base.h"
#ifdef ASURA_USE_GUI_OSK
#include "GUIMenu/Asura_GUIMenu_OnScreenKeyboard.h"
#endif
#include "Rendering/Asura_Render.h"
#include "Rendering/Asura_Viewport.h"

#define ASURA_GUIMENU_MAX_CONTROLLERS	1
// --------------------------------------------------------------------------------------- 

void Asura_GUIMenu_System::Platform_InitInputMappings( void)
{
#ifdef ASURA_TOOLS

#define REGISTER_CONTROL_ENTRY( _mapTo, _buttonID, _pressType, _desc ) \
	RegisterControlEntry( 0, _mapTo, _buttonID, _pressType, _desc)

#else

#define REGISTER_CONTROL_ENTRY( _mapTo, _buttonID, _pressType, _UNUSED ) \
	RegisterControlEntry( 0, _mapTo, _buttonID, _pressType)

#endif // ASURA_TOOLS

	if(s_pxMenuControlMaps == NULL)
	{
		s_pxMenuControlMaps = new Asura_ControlMap[ASURA_GUIMENU_MAX_CONTROLLERS];

		s_pxMenuControlMaps[0].CreateMappings( ASURA_GUIMENU_CONTROL_MAX);

		// -- Keyboard controls --

		// COPY/PASTE These entries should be gotten from the PC version of this file. -------------------------------------------------------------------------------------------------------------------
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_UP, ASURA_KEYID_UP, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_DOWN, ASURA_KEYID_DOWN, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_LEFT, ASURA_KEYID_LEFT, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_RIGHT, ASURA_KEYID_RIGHT, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);

		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ACCEPT,		ASURA_KEYID_ENTER,			Asura_ControlMapping::ON_DEBOUNCED_PRESS, "Accept - X");
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT1, 	ASURA_KEYID_DEL,			Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - Sq");				// SQUARE
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT2, 	ASURA_KEYID_HOME,			Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - O / Tri(psp)");		// TRIANGLE
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT3, 	ASURA_KEYID_NUMPADDEL,		Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - R3 / NA(psp)");		// R3
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT4, 	ASURA_KEYID_TAB,			Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - Select");			// SELECT
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT5, 	ASURA_KEYID_NUMPADENTER,	Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - Start");			// START
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT6, 	ASURA_KEYID_NUMPAD4, 		Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - L1");				// L1
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT7, 	ASURA_KEYID_NUMPAD6, 		Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - R1");				// R1
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT8, 	ASURA_KEYID_NUMPAD7, 		Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - L2 / NA(psp)");		// L2
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT9, 	ASURA_KEYID_NUMPAD9, 		Asura_ControlMapping::ON_DEBOUNCED_PRESS,	"Alt Accept - R2 / NA(psp)");		// R2

		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_UP_HELD, ASURA_KEYID_UP, Asura_ControlMapping::ON_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_DOWN_HELD, ASURA_KEYID_DOWN, Asura_ControlMapping::ON_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_LEFT_HELD, ASURA_KEYID_LEFT, Asura_ControlMapping::ON_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_RIGHT_HELD, ASURA_KEYID_RIGHT, Asura_ControlMapping::ON_PRESS, NULL);

		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ACCEPT_HELD,		ASURA_KEYID_ENTER,			Asura_ControlMapping::ON_PRESS, "Held Accept - X");
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT1_HELD, ASURA_KEYID_DEL,			Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - Sq");				// SQUARE
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT2_HELD, ASURA_KEYID_HOME,			Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - O / Tri(psp)");	// TRIANGLE
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT3_HELD, ASURA_KEYID_NUMPADDEL,		Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - R3 / NA(psp)");	// R3
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT4_HELD, ASURA_KEYID_TAB,			Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - Select");			// SELECT
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT5_HELD, ASURA_KEYID_NUMPADENTER,	Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - Start");			// START
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT6_HELD, ASURA_KEYID_NUMPAD4, 		Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - L1");				// L1
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT7_HELD, ASURA_KEYID_NUMPAD6, 		Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - R1");				// R1
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT8_HELD, ASURA_KEYID_NUMPAD7, 		Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - L2 / NA(psp)");	// L2
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_ALT_ACCEPT9_HELD, ASURA_KEYID_NUMPAD9, 		Asura_ControlMapping::ON_PRESS,	"Held Alt Accept - R2 / NA(psp)");	// R2

		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_CANCEL, ASURA_KEYID_BACKSPACE, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_CANCEL_HELD, ASURA_KEYID_BACKSPACE, Asura_ControlMapping::ON_PRESS, NULL);

		// Use the Escape key for canceling menus. Falls in line with how other games work
		// and Eidos PC Game Standards.
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_CANCEL, ASURA_KEYID_ESCAPE, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_CANCEL_HELD, ASURA_KEYID_ESCAPE, Asura_ControlMapping::ON_PRESS, NULL);

		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_CLICK, ASURA_KEYID_MOUSEBUTTON1, Asura_ControlMapping::ON_DEBOUNCED_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_CLICK_HELD, ASURA_KEYID_MOUSEBUTTON1, Asura_ControlMapping::ON_PRESS, NULL);

		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_UP, ASURA_KEYID_MOUSEWHEELUP, Asura_ControlMapping::ON_PRESS, NULL);
		REGISTER_CONTROL_ENTRY( ASURA_GUIMENU_CONTROL_DOWN, ASURA_KEYID_MOUSEWHEELDOWN, Asura_ControlMapping::ON_PRESS, NULL);
		//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	}
}

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

void Asura_GUIMenu_System::Platform_ShutdownInputMappings( void)
{
	u_int u;

	if( s_pxMenuControlMaps)
	{
		for( u=0; u<ASURA_GUIMENU_MAX_CONTROLLERS; u++)
		{
			s_pxMenuControlMaps[u].DeleteMappings();
		}

		delete []s_pxMenuControlMaps;
		s_pxMenuControlMaps = NULL;
	}
}

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

bool Asura_GUIMenu_System::Platform_ShouldUpdateControls()
{
	return true;
}

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

bool Asura_GUIMenu_System::Platform_ShouldRenderMenuPages()
{
	return true;
}

// --------------------------------------------------------------------------------------- 
#ifdef ASURA_DEBUG_VARIABLE_SYSTEM
void Asura_GUIMenu_System::Platform_RenderSafetyBorder( void) {}
#endif // ASURA_DEBUG_VARIABLE_SYSTEM

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

u_int Asura_GUIMenu_System::Platform_GetNumControlMaps( void)
{
	return ASURA_GUIMENU_MAX_CONTROLLERS;
}

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

void Asura_GUIMenu_System::Platform_BeginTextInput( const Asura_UnicodeChar* wszMessage, 
												    Asura_UnicodeChar* wszBuffer, 
													u_int uBufferMaxChars )
{
	Asura_Assert( true, "GUIMenu text input has not been implemented on PC-CLI" );
}

void Asura_GUIMenu_System::Platform_UpdateTextInput() {}

void Asura_GUIMenu_System::Platform_BeginRender() {}

void Asura_GUIMenu_System::Platform_RenderTextInput()
{
	// just leave the menu to render it? what about cursors?
}

void Asura_GUIMenu_System::Platform_EndRender() {}

bool Asura_GUIMenu_System::Platform_IsTextInputFinished()
{
	return true;
}

bool Asura_GUIMenu_System::Platform_IsTextInputCancelled()
{
	return true;
}

void Asura_GUIMenu_System::Platform_EndTextInput() {}

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

bool Asura_GUIMenu_System::Platform_ProcessMenuBackControls( u_int uButtonsPressed, u_int uButtonsTrapped)
{
	bool bRet = false;

	// filters out any buttons presses not required, then we do an equality check
	uButtonsPressed &= (ASURA_GUIMENU_ACTION_BUTTON_CANCEL|ASURA_GUIMENU_ACTION_BUTTON_CANCEL_HELD);

	if( uButtonsPressed == (ASURA_GUIMENU_ACTION_BUTTON_CANCEL|ASURA_GUIMENU_ACTION_BUTTON_CANCEL_HELD) )
	{
		if( (uButtonsTrapped&ASURA_GUIMENU_ACTION_BUTTON_CANCEL) == 0)
		{
			bRet = true;
		}
	}

	return bRet;
}
// --------------------------------------------------------------------------------------- 

#endif // ASURA_USE_GUI_MENU_SYSTEM
