class keyboardHintGameController extends inkHUDGameController
{
	private editable var m_TopElementName : CName;
	default m_TopElementName = 'KeyboardHintItem_Top';
	private editable var m_BottomElementName : CName;
	default m_BottomElementName = 'KeyboardHintItem_Bot';
	private editable var m_Layout : inkBasePanelRef;
	private var m_UIItems : array< weak< KeyboardHintItemController > >;
	private var m_Player : weak< PlayerPuppet >;
	private var m_QuickSlotsManager : weak< QuickSlotsManager >;
	private var m_UiQuickItemsBlackboard : weak< IBlackboard >;
	private var m_KeyboardCommandBBID : CallbackHandle;

	protected event OnInitialize()
	{
		var i, limit : Int32;
		m_Player = ( ( PlayerPuppet )( GetOwnerEntity() ) );
		m_QuickSlotsManager = m_Player.GetQuickSlotsManager();
		for( i = 0, limit = QuickSlotsManager.GetMaxKeyboardItems(); i < limit; i += 1 )
		{
			AddKeyboardItem( i );
		}
		m_UiQuickItemsBlackboard = GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_QuickSlotsData );
		if( m_UiQuickItemsBlackboard )
		{
			m_KeyboardCommandBBID = m_UiQuickItemsBlackboard.RegisterListenerVariant( GetAllBlackboardDefs().UI_QuickSlotsData.KeyboardCommand, this, 'OnKeyboardCommand' );
		}
	}

	protected event OnUninitialize()
	{
		if( m_UiQuickItemsBlackboard )
		{
			m_UiQuickItemsBlackboard.UnregisterListenerVariant( GetAllBlackboardDefs().UI_QuickSlotsData.KeyboardCommand, m_KeyboardCommandBBID );
		}
		m_UiQuickItemsBlackboard = NULL;
	}

	private function AddKeyboardItem( index : Int32 )
	{
		var itemName : CName;
		var controller : weak< KeyboardHintItemController >;
		if( ( index % 2 ) == 0 )
		{
			itemName = m_TopElementName;
		}
		else
		{
			itemName = m_BottomElementName;
		}
		controller = ( ( KeyboardHintItemController )( SpawnFromLocal( inkBasePanelRef.Get( m_Layout ), itemName ).GetController() ) );
		if( controller )
		{
			controller.Setup( index + 1 );
			m_UIItems.PushBack( controller );
		}
	}

	private function OnKeyboardCommand( value : Variant )
	{
		var quickSlotInformation : QuickSlotUIStructure;
		quickSlotInformation = ( ( QuickSlotUIStructure )value );
		AnimateKeyboardIcons( quickSlotInformation.ItemIndex, quickSlotInformation.OperationResult );
	}

	private function AnimateKeyboardIcons( choosenItemIndex : Int32, success : Bool )
	{
		var i, offset, currentIndex : Int32;
		var currentItem : QuickSlotCommand;
		var isEnabled : Bool;
		if( choosenItemIndex > 3 )
		{
			offset = 4;
		}
		else
		{
			offset = 0;
		}
		for( i = 0; i < 4; i += 1 )
		{
			currentIndex = i + offset;
			if( m_QuickSlotsManager.IsKeyboardActionAvaliable( currentIndex ) )
			{
				currentItem = m_QuickSlotsManager.GetKeyboardCommandAtSlot( currentIndex );
				isEnabled = success;
				m_UIItems[ currentIndex ].SetState( isEnabled, currentIndex == choosenItemIndex );
				m_UIItems[ currentIndex ].SetIcon( currentItem.AtlasPath, currentItem.IconName );
				m_UIItems[ currentIndex ].Animate( isEnabled );
			}
		}
	}

}

