class CpoCharacterButtonItemController extends inkButtonDpadSupportedController
{
	private var m_characterRecordId : TweakDBID;

	public function SetButtonDetails( const text : ref< String >, characterRecordId : TweakDBID )
	{
		var currListText : weak< inkText >;
		m_rootWidget = ( ( inkCanvas )( GetRootWidget() ) );
		currListText = ( ( inkText )( m_rootWidget.GetWidget( 'textLabel' ) ) );
		currListText.SetText( text );
		m_characterRecordId = characterRecordId;
	}

	public function GetCharacterRecordId() : TweakDBID
	{
		return m_characterRecordId;
	}

}

class CpoCharacterSelectionWidgetGameController extends inkGameController
{
	editable var m_defaultCharacterTexturePart : String;
	editable var m_soloCharacterTexturePart : String;
	private var m_horizontalPanelsList : array< weak< inkHorizontalPanel > >;
	private var m_amount : Int32;
	default m_amount = 5;

	protected event OnInitialize()
	{
		var horizontalList1 : weak< inkHorizontalPanel >;
		var horizontalList2 : weak< inkHorizontalPanel >;
		var horizontalList3 : weak< inkHorizontalPanel >;
		var verticalPanel : weak< inkVerticalPanel >;
		verticalPanel = ( ( inkVerticalPanel )( GetWidget( 'SubCanvas\VerticalPanel' ) ) );
		horizontalList1 = ( ( inkHorizontalPanel )( SpawnFromLocal( verticalPanel, 'CpoCharactersList' ) ) );
		horizontalList2 = ( ( inkHorizontalPanel )( SpawnFromLocal( verticalPanel, 'CpoCharactersList' ) ) );
		horizontalList3 = ( ( inkHorizontalPanel )( SpawnFromLocal( verticalPanel, 'CpoCharactersList' ) ) );
		CreateCharacterButton( horizontalList1, "Default", T"Character.Cpo_Default_Player" );
		CreateCharacterButton( horizontalList1, "Solo", T"Character.Cpo_Solo" );
		CreateCharacterButton( horizontalList1, "Assassin", T"Character.Cpo_Assassin" );
		CreateCharacterButton( horizontalList2, "Techie", T"Character.Cpo_Techie" );
		CreateCharacterButton( horizontalList2, "Netrunner", T"Character.Cpo_Netrunner" );
		CreateCharacterButton( horizontalList2, "Solo[M]", T"Character.Cpo_Muppet_Solo" );
		CreateCharacterButton( horizontalList3, "Assassin[M]", T"Character.Cpo_Muppet_Assassin" );
		CreateCharacterButton( horizontalList3, "Techie[M]", T"Character.Cpo_Muppet_Techie" );
		CreateCharacterButton( horizontalList3, "Netrunner[M]", T"Character.Cpo_Muppet_Netrunner" );
		SetCursorOverWidget( horizontalList1.GetWidget( 0 ) );
		SetVisibilityInBlackboard( true );
	}

	protected event OnUninitialize()
	{
		SetVisibilityInBlackboard( false );
	}

	private function CreateCharacterButton( parent : weak< inkHorizontalPanel >, const argText : ref< String >, characterRecordId : TweakDBID )
	{
		var currButton : weak< inkCanvas >;
		var currLogic : weak< CpoCharacterButtonItemController >;
		currButton = ( ( inkCanvas )( SpawnFromLocal( parent, 'CpoCharacterButton' ) ) );
		currButton.RegisterToCallback( 'OnRelease', this, 'OnSelectCharacter' );
		currButton.RegisterToCallback( 'OnEnter', this, 'OnSelectCharacterEnter' );
		currButton.RegisterToCallback( 'OnLeave', this, 'OnSelectCharacterLeave' );
		currLogic = ( ( CpoCharacterButtonItemController )( currButton.GetController() ) );
		currLogic.SetButtonDetails( argText, characterRecordId );
	}

	public function OnSelectCharacter( e : inkPointerEvent )
	{
		var button : weak< inkCanvas >;
		var controller : weak< CpoCharacterButtonItemController >;
		var bb : IBlackboard;
		if( e.IsAction( 'click' ) )
		{
			button = ( ( inkCanvas )( e.GetCurrentTarget() ) );
			controller = ( ( CpoCharacterButtonItemController )( button.GetController() ) );
			bb = GameInstance.GetBlackboardSystem( ( ( GameObject )( GetOwnerEntity() ) ).GetGame() ).Get( GetAllBlackboardDefs().UI_CpoCharacterSelection );
			bb.SetVariant( GetAllBlackboardDefs().UI_CpoCharacterSelection.CharacterRecordId, controller.GetCharacterRecordId() );
			bb.SignalVariant( GetAllBlackboardDefs().UI_CpoCharacterSelection.CharacterRecordId );
		}
	}

	public function OnSelectCharacterEnter( e : inkPointerEvent )
	{
		var button : weak< inkWidget >;
		var controller : weak< CpoCharacterButtonItemController >;
		button = ( ( inkWidget )( e.GetCurrentTarget() ) );
		controller = ( ( CpoCharacterButtonItemController )( button.GetController() ) );
		FillTooltip( controller.GetCharacterRecordId() );
		ShowTooltip( true );
	}

	public function OnSelectCharacterLeave( e : inkPointerEvent )
	{
		ShowTooltip( false );
	}

	private function FillTooltip( characterRecordId : TweakDBID )
	{
		var tooltipWidgetText : weak< inkText >;
		var tooltipWidgetText2 : weak< inkText >;
		var character : weak< Character_Record >;
		tooltipWidgetText = ( ( inkText )( GetWidget( 'SubCanvas/tooltip/tooltipText' ) ) );
		tooltipWidgetText2 = ( ( inkText )( GetWidget( 'SubCanvas/tooltip/tooltipText2' ) ) );
		character = TweakDBInterface.GetCharacterRecord( characterRecordId );
		tooltipWidgetText.SetLocalizedTextScript( character.DisplayName() );
		tooltipWidgetText2.SetLocalizedTextScript( character.DisplayDescription() );
	}

	private function ShowTooltip( visible : Bool )
	{
		var tooltipWidget : weak< inkCanvas >;
		tooltipWidget = ( ( inkCanvas )( GetWidget( 'SubCanvas\tooltip' ) ) );
		tooltipWidget.SetVisible( visible );
	}

	private function SetVisibilityInBlackboard( isVisible : Bool )
	{
		var bb : IBlackboard;
		bb = GameInstance.GetBlackboardSystem( ( ( GameObject )( GetOwnerEntity() ) ).GetGame() ).Get( GetAllBlackboardDefs().UI_CpoCharacterSelection );
		bb.SetBool( GetAllBlackboardDefs().UI_CpoCharacterSelection.SelectionMenuVisible, isVisible );
		bb.SignalBool( GetAllBlackboardDefs().UI_CpoCharacterSelection.SelectionMenuVisible );
	}

}

