
///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_PSP_SCEUtilityWrapper.cpp
// Author : Andy Tate
// Date   : 4-Oct-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header...
#include "PSPEmul/SCEUtility/Asura_PSP_SCEUtilityWrapper.h"

// Asura independent header-files...
#include "Rendering/Asura_Render.h"

// Asura platform header-files...
#include "PSPEmul/SCEUtility/Asura_PSP_SCEUtility_LoadListHandler.h"
#include "PSPEmul/SCEUtility/Asura_PSP_SCEUtility_OSKHandler.h"
#include "PSPEmul/SCEUtility/Asura_PSP_SCEUtility_SaveListHandler.h"

// Game independent header-files...

// Game platform header-files...


////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtility_Handler*				Asura_PSP_SCEUtilityWrapper::s_pxCurrentHandler			= NULL;

Asura_PSP_SCEUtilityWrapper::CommandState	Asura_PSP_SCEUtilityWrapper::s_eCommandState			= Asura_PSP_SCEUtilityWrapper::COMMAND_INACTIVE;

bool										Asura_PSP_SCEUtilityWrapper::s_bAbleToPerformAutosave	= false;
bool										Asura_PSP_SCEUtilityWrapper::s_bCircleButtonIsEnter		= false;
bool										Asura_PSP_SCEUtilityWrapper::s_bMemoryStickPreset		= true;

Asura_UnicodeChar							Asura_PSP_SCEUtilityWrapper::s_wszNickname [uASURA_PSP_NICKNAME_MAX_CHARS];


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::Initialise
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the initialisation of SCE Utility wrapper.
//
// Parameters   : The title of the game
//
// Return value : None.
////////////////////////////////////////////////////////////////////////////////

void Asura_PSP_SCEUtilityWrapper::Initialise (const Asura_UnicodeChar* wszTitleName)
{
	// Set the nickname...
	wcscpy	(s_wszNickname, L"PLAYER");
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::Initialise'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::Shutdown
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the shutdown of SCE Utility wrapper.
//
// Return value : None.
////////////////////////////////////////////////////////////////////////////////

void Asura_PSP_SCEUtilityWrapper::Shutdown (void)
{
	// Remove any handler which may still be running...
	delete	(s_pxCurrentHandler);

	s_pxCurrentHandler	= NULL;
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::Shutdown'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::InitialiseOnScreenKeyboard
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Initialises the SCE PSP Utility on-screen keyboard handler.
//
// Parameters   : 
//
// Return value : The state of the command we attempted to initialise.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseOnScreenKeyboard (const Asura_UnicodeChar* wszMessage,
																								   const Asura_UnicodeChar* wszTextBeforeEdit,
																								   Asura_UnicodeChar* wszTextAfterEdit,
																								   const int iMaxChars,
																								   const OSKCharacterSet eCharacterSet)
{
	// Are we already handling something?
	if (NULL == s_pxCurrentHandler)
	{
		// No - create the OSK handler...
		s_pxCurrentHandler	= new Asura_PSP_SCEUtility_OSKHandler (wszMessage, wszTextBeforeEdit, iMaxChars, wszTextAfterEdit);
		s_eCommandState		= COMMAND_IN_PROGRESS;
	}

	else
	{
		// Yes - can't handle 2 jobs at once...
		Asura_Assert	(false, "We already have a SCE Utility handler.");

		s_eCommandState	= COMMAND_ERROR;
	}

	// Return the current state...
	return	 (s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::InitialiseOnScreenKeyboard'. ///



Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataAutoload (void* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize,
																								   LoadDataValidationFunction* pfnValidation)
{
	return	(COMMAND_ERROR);
}

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataAutosave (const Asura_UnicodeChar* wszSaveTitle,
																								   const Asura_UnicodeChar* wszSaveDescription,
																								   const Asura_UnicodeChar* wszNewData,
																								   void const* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize)
{
	return	(COMMAND_ERROR);
}

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataDelete (void)
{
	return	(COMMAND_ERROR);
}

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataLoadList
// Author       : Andy Tate
// Date         : 6-Oct-2005
//
// Description  : 
//
// Parameters   : 
//
// Return value : 
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataLoadList (const int iNumSaveSlots,
																								   void* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize,
																								   LoadDataValidationFunction* pfnValidation)
{
	// Are we already handling something?
	if (NULL == s_pxCurrentHandler)
	{
		// No - create the OSK handler...
		s_pxCurrentHandler	= new Asura_PSP_SCEUtility_LoadListHandler (iNumSaveSlots, pvData, uDataSize, uBufferSize, pfnValidation);
		s_eCommandState		= COMMAND_IN_PROGRESS;
	}

	else
	{
		// Yes - can't handle 2 jobs at once...
		Asura_Assert	(false, "We already have a SCE Utility handler.");

		s_eCommandState	= COMMAND_ERROR;
	}

	// Return the current state...
	return	 (s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataLoadList'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataSaveList
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Initialises the save-data using a file-list.
//
// Parameters   : 
//
// Return value : The state of the command we attempted to initialise.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataSaveList (const int iNumSaveSlots,
																								   const Asura_UnicodeChar* wszSaveTitle,
																								   const Asura_UnicodeChar* wszSaveDescription,
																								   const Asura_UnicodeChar* wszNewData,
																								   void const* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize)
{
	// Are we already handling something?
	if (NULL == s_pxCurrentHandler)
	{
		// No - create the OSK handler...
		s_pxCurrentHandler	= new Asura_PSP_SCEUtility_SaveListHandler (iNumSaveSlots, wszSaveTitle, wszSaveDescription, wszNewData, pvData, uDataSize, uBufferSize);
		s_eCommandState		= COMMAND_IN_PROGRESS;
	}

	else
	{
		// Yes - can't handle 2 jobs at once...
		Asura_Assert	(false, "We already have a SCE Utility handler.");

		s_eCommandState	= COMMAND_ERROR;
	}

	// Return the current state...
	return	 (s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataSaveList'. ///


Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataLoadSingle (const char* szUserID,
																								   void* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize,
																								   LoadDataValidationFunction* pfnValidation)
{
	return InitialiseSaveDataLoadList(1, pvData, uDataSize, uBufferSize, pfnValidation);
}

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataSaveSingle (const char* szUserID,
																								   const Asura_UnicodeChar* wszSaveTitle,
																								   const Asura_UnicodeChar* wszSaveDescription,
																								   const Asura_UnicodeChar* wszNewData,
																								   void const* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize)
{
	return InitialiseSaveDataSaveList(1, wszSaveTitle, wszSaveDescription, wszNewData, pvData, uDataSize, uBufferSize);
}

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataAutoloadSingle (const char* szUserID,
																								   void* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize,
																								   LoadDataValidationFunction* pfnValidation)
{
	return InitialiseSaveDataLoadSingle(szUserID, pvData, uDataSize, uBufferSize, pfnValidation);
}

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseSaveDataAutosaveSingle (const char* szUserID,
																								   const Asura_UnicodeChar* wszSaveTitle,
																								   const Asura_UnicodeChar* wszSaveDescription,
																								   const Asura_UnicodeChar* wszNewData,
																								   void const* pvData,
																								   const u_int uDataSize,
																								   const u_int uBufferSize)
{
	return InitialiseSaveDataSaveSingle(szUserID, wszSaveTitle, wszSaveDescription, wszNewData, pvData, uDataSize, uBufferSize);
}


Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::InitialiseMessageDialog (const u_int uErrorNumber)
{
	return	(COMMAND_ERROR);
}



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::BeginFrame
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the beginning of the current frame.
//
// Return value : The current state of the command being executed.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::BeginFrame (void)
{
	// Nothing to do here - return the current state...
	return	(s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::BeginFrame'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::Update
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the update of the PSP SCE Utility.
//
// Return value : The current state of the command being executed.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::Update (void)
{
	bool	bCancelled;

	// Do we have a handler?
	if (NULL == s_pxCurrentHandler)
	{
		// No - return to being inactive...
		s_eCommandState	= COMMAND_INACTIVE;
	}

	else if (s_pxCurrentHandler->Update(bCancelled))
	{
		// Still updating...
		s_eCommandState	= COMMAND_IN_PROGRESS;
	}

	else
	{
		// Finished updating - remove the handler...
		delete	(s_pxCurrentHandler);

		s_pxCurrentHandler	= NULL;
		s_eCommandState		= (bCancelled) ? COMMAND_CANCELLED : COMMAND_COMPLETED;
	}

	// Return the current state...
	return	(s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::Update'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::Render
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the rendering of the PSP SCE Utility.
//
// Return value : The current state of the command being executed.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::Render (void)
{
	// Do we have anything to render?
	if (NULL != s_pxCurrentHandler)
	{
		// Yes - time to do so...
		s_pxCurrentHandler->Render	();
	}

	// Return the current state...
	return	(s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::Render'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtilityWrapper::EndFrame
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the end of the current frame.
//
// Return value : The current state of the command being executed.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtilityWrapper::CommandState Asura_PSP_SCEUtilityWrapper::EndFrame (void)
{
	// Nothing to do here - return the current state...
	return	(s_eCommandState);
}

/// End of function 'Asura_PSP_SCEUtilityWrapper::EndFrame'. ///


/// End of file. ///
