struct ScoreboardPlayer
{
	editable var m_playerName : String;
	editable var m_playerScore : Int32;
}

class ScoreboardLogicController extends inkLogicController
{
	private editable var m_gridItem : CName;
	private editable var m_namesWidget : inkCompoundRef;
	private editable var m_scoresWidget : inkCompoundRef;
	private const instanceeditable var m_highScores : array< ScoreboardPlayer >;

	public function CleanGrid()
	{
		inkCompoundRef.RemoveAllChildren( m_namesWidget );
		inkCompoundRef.RemoveAllChildren( m_scoresWidget );
	}

	public function FillGrid( playerScore : Int32 )
	{
		var i : Int32;
		var j : Int32;
		var nameController : ScoreboardEntityLogicController;
		var scoreController : ScoreboardEntityLogicController;
		var scoreboardPlayers : array< ScoreboardPlayer >;
		var tempPlayer : ScoreboardPlayer;
		for( i = 0; i < m_highScores.Size(); i += 1 )
		{
			scoreboardPlayers.PushBack( m_highScores[ i ] );
		}
		scoreboardPlayers.PushBack( ScoreboardPlayer( "V.", playerScore ) );
		for( i = 0; i < ( scoreboardPlayers.Size() - 1 ); i += 1 )
		{
			for( j = i + 1; j < scoreboardPlayers.Size(); j += 1 )
			{
				if( scoreboardPlayers[ i ].m_playerScore < scoreboardPlayers[ j ].m_playerScore )
				{
					tempPlayer = scoreboardPlayers[ i ];
					scoreboardPlayers[ i ] = scoreboardPlayers[ j ];
					scoreboardPlayers[ j ] = tempPlayer;
				}
			}
		}
		for( i = 0; i < scoreboardPlayers.Size(); i += 1 )
		{
			nameController = ( ( ScoreboardEntityLogicController )( SpawnFromLocal( inkCompoundRef.Get( m_namesWidget ), m_gridItem ).GetController() ) );
			scoreController = ( ( ScoreboardEntityLogicController )( SpawnFromLocal( inkCompoundRef.Get( m_scoresWidget ), m_gridItem ).GetController() ) );
			nameController.SetText( scoreboardPlayers[ i ].m_playerName );
			scoreController.SetText( IntToString( scoreboardPlayers[ i ].m_playerScore ) );
		}
	}

}

