struct PlayerListEntryData
{
	var playerObject : weak< GameObject >;
	var playerListEntry : weak< inkWidget >;
}

import class gameuiPlayerListGameController extends inkHUDGameController
{
	private var m_playerEntries : array< PlayerListEntryData >;
	private editable var m_container : inkCompoundRef;

	protected event OnPlayerAttach( playerPuppet : GameObject )
	{
		AddPlayerToList( playerPuppet );
	}

	protected event OnRemotePlayerAttach( playerPuppet : GameObject )
	{
		AddPlayerToList( playerPuppet );
	}

	protected event OnPlayerDetach( playerPuppet : GameObject )
	{
		RemovePlayerFromList( playerPuppet );
	}

	protected event OnRemotePlayerDetach( playerPuppet : GameObject )
	{
		RemovePlayerFromList( playerPuppet );
	}

	private function AddPlayerToList( playerPuppet : GameObject )
	{
		var playerListEntry : weak< inkWidget >;
		var controller : weak< PlayerListEntryLogicController >;
		var playerListEntryData : PlayerListEntryData;
		playerListEntry = SpawnFromLocal( inkCompoundRef.Get( m_container ), 'player_list_entry' );
		controller = ( ( PlayerListEntryLogicController )( playerListEntry.GetController() ) );
		controller.SetEntryData( playerPuppet );
		controller.SetEntryColorAndIcon( playerPuppet );
		playerListEntryData.playerObject = playerPuppet;
		playerListEntryData.playerListEntry = playerListEntry;
		m_playerEntries.PushBack( playerListEntryData );
	}

	private function RemovePlayerFromList( playerPuppet : GameObject )
	{
		var i : Int32;
		for( i = 0; i < m_playerEntries.Size(); i += 1 )
		{
			if( m_playerEntries[ i ].playerObject == playerPuppet )
			{
				inkCompoundRef.RemoveChild( m_container, m_playerEntries[ i ].playerListEntry );
				m_playerEntries.EraseFast( i );
				break;
			}
		}
	}

}

class PlayerListEntryLogicController extends inkLogicController
{
	private editable var m_playerNameLabel : inkWidgetRef;
	private editable var m_playerClassIcon : inkImageRef;

	private function GetPlayerClassName( playerPuppet : GameObject ) : CName
	{
		var className : CName;
		var characterRecordID : TweakDBID;
		characterRecordID = ( ( gamePuppetBase )( playerPuppet ) ).GetRecordID();
		className = TweakDBInterface.GetCharacterRecord( characterRecordID ).CpoClassName();
		return className;
	}

	public function SetEntryData( playerPuppet : GameObject )
	{
		var textLabel : weak< inkText >;
		var playerNickname : String;
		var mpPlayerMgr : mpPlayerManager;
		mpPlayerMgr = ( ( mpPlayerManager )( GameInstance.GetPlayerManagerSystem( playerPuppet.GetGame() ) ) );
		if( mpPlayerMgr )
		{
			playerNickname = mpPlayerMgr.GetPlayerNicknameByGameObject( playerPuppet );
		}
		else
		{
			playerNickname = "Local";
		}
		textLabel = ( ( inkText )( inkWidgetRef.Get( m_playerNameLabel ) ) );
		textLabel.SetText( playerNickname );
	}

	public function SetEntryColorAndIcon( playerPuppet : GameObject )
	{
		var textLabel : weak< inkText >;
		var classIcon : weak< inkImage >;
		var className : CName;
		className = GetPlayerClassName( playerPuppet );
		textLabel = ( ( inkText )( inkWidgetRef.Get( m_playerNameLabel ) ) );
		textLabel.SetState( className );
		classIcon = ( ( inkImage )( inkImageRef.Get( m_playerClassIcon ) ) );
		classIcon.SetState( className );
		classIcon.SetTexturePart( className );
	}

}

