//////////////////////////////////////////////////////////////////////////////
//
// Asura_Platform_Leaderboards.cpp
//
// Copyright (c) 2008 Rebellion. All rights reserved.
//
// Description:
//
//     PS3 support for leaderboards interface.
//
//////////////////////////////////////////////////////////////////////////////

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

#include "Core/Asura.h"

#ifdef ASURA_SUPPORT_LEADERBOARDS

// Asura independent
#include "Debugging/Asura_Assert.h"
#include "Network/Online/Asura_Leaderboards.h"

// Asura PS3
#include "Network/Online/Asura_PS3_NP_Basic.h"
#include "Network/Online/Asura_PS3_NP_TUS.h"
#include "Network/Online/Asura_PS3_Stats.h"

void Asura_Leaderboard_System::Platform_Initialise()
{
	Asura_PS3_NP_Score::Initialise();
	Asura_PS3_NP_TUS::Initialise();
}

void Asura_Leaderboard_System::Platform_Shutdown()
{
	Asura_PS3_NP_TUS::Shutdown();
	Asura_PS3_NP_Score::Shutdown();
}

void Asura_Leaderboard::Platform_Initialise()
{
	m_pPlatformData = new Asura_PS3_StatsReader();
}

void Asura_Leaderboard::Platform_Shutdown()
{
	Asura_PS3_StatsReader* pxStatsReader = reinterpret_cast
		<Asura_PS3_StatsReader*>(m_pPlatformData);
	delete pxStatsReader;
}

bool Asura_Leaderboard::Platform_FetchData()
{
	Asura_PS3_StatsReader* pxStatsReader = reinterpret_cast
		<Asura_PS3_StatsReader*>(m_pPlatformData);

	Asura_Assert(pxStatsReader, "no stats reader!");

	const u_int uLeaderboard = m_uLeaderboardIndex;
	const u_int uLeaderboardID = Asura_PS3_Stats::Project_GetBoardID(m_uLeaderboardIndex);
	const u_int uNumColumns = Asura_Leaderboard_System::Project_GetNumLeaderboardColumns(uLeaderboard);

	bool bRet;
	if (m_bFriendsOnly)
	{
		// buffers must remain on the stack
		bRet = pxStatsReader->FetchStatsForNpIds(uLeaderboardID, 
			Asura_PS3_NP_Basic::GetFriendsListIncludingSelf(), 
			Asura_PS3_NP_Basic::GetFriendsListSizeIncludingSelf());
	}
	else
	{
		bRet = pxStatsReader->FetchStats(uLeaderboardID, m_uFirstRow, uASURA_LEADERBOARD_MAX_ROWS_IN_MEMORY);
	}
	return bRet;
}

bool Asura_Leaderboard::Platform_Update()
{
	Asura_PS3_StatsReader* pxStatsReader = reinterpret_cast
		<Asura_PS3_StatsReader*>(m_pPlatformData);

	Asura_Assert(pxStatsReader, "no stats reader!");

	if (pxStatsReader->Update())
	{
		if (pxStatsReader->HasErrorOccurred())
		{
			m_bError = true;
			return true;
		}

		// copy the stats into the independent arrays
		m_uTotalEntries = pxStatsReader->GetTotalEntries();
		const u_int uNumRows = pxStatsReader->GetNumValidRows();
		const u_int uNumCols = Asura_Leaderboard_System::Project_GetNumLeaderboardColumns(m_uLeaderboardIndex);
		m_uNumRows = uNumRows;

		const SceNpScoreRankData* pxStatsRowData = pxStatsReader->GetStatsDataArray();
		const SceNpScoreGameInfo* pxStatsRowExtraCols = pxStatsReader->GetExtraColumnDataArray();
		for (u_int uRow = 0; uRow < uNumRows; ++uRow)
		{
			SetASCIINameAndRank(uRow, pxStatsRowData->npId.handle.data, pxStatsRowData->rank);
			SetData(uRow, 0, static_cast<float>(pxStatsRowData->scoreValue));
			// columns 1 to n are the unrated columns
			Asura_PS3_Stats_ExtraColumns xColumn;
			xColumn.m_xGameInfo = *pxStatsRowExtraCols;
			for (u_int uCol = 1; uCol < uNumCols; ++uCol)
			{
				if (Asura_Leaderboard_System::Project_GetDataFormat(m_uLeaderboardIndex, uCol) 
					== ASURA_LB_DATAFORMAT_FLOAT)
				{
					SetData(uRow, uCol, static_cast<float>(xColumn.m_xColumns[uCol-1].m_fData));
				}
				else
				{
					SetData(uRow, uCol, static_cast<float>(xColumn.m_xColumns[uCol-1].m_uData));
				}
			}

			++pxStatsRowData;
			++pxStatsRowExtraCols;
		}
		return true;
	}
	return false;
}

#endif // #ifdef ASURA_SUPPORT_LEADERBOARDS
