class BackpackEquipSlotChooserPopup extends inkGameController
{
	private editable var m_titleText : inkTextRef;
	private editable var m_buttonHintsRoot : inkWidgetRef;
	private editable var m_rairtyBar : inkWidgetRef;
	private editable var m_root : inkWidgetRef;
	private editable var m_background : inkWidgetRef;
	private editable var m_weaponSlotsContainer : inkCompoundRef;
	private editable var m_tooltipsManagerRef : inkWidgetRef;
	private var m_buttonHintsController : weak< ButtonHints >;
	private var m_gameData : gameItemData;
	protected editable var m_buttonOk : inkWidgetRef;
	protected editable var m_buttonCancel : inkWidgetRef;
	private var m_data : BackpackEquipSlotChooserData;
	private var m_selectedSlotIndex : Int32;
	private var m_tooltipsManager : weak< gameuiTooltipsManager >;
	private var m_comparisonResolver : InventoryItemPreferredComparisonResolver;
	private editable var m_libraryPath : inkWidgetLibraryReference;

	protected event OnInitialize()
	{
		m_tooltipsManager = ( ( gameuiTooltipsManager )( inkWidgetRef.GetControllerByType( m_tooltipsManagerRef, 'gameuiTooltipsManager' ) ) );
		m_tooltipsManager.Setup( ETooltipsStyle.Menus );
		RegisterToGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
		m_data = ( ( BackpackEquipSlotChooserData )( GetRootWidget().GetUserData( 'BackpackEquipSlotChooserData' ) ) );
		inkTextRef.SetText( m_titleText, "UI-PopupNotification-ChooseWeaponSlotTitle" );
		inkWidgetRef.RegisterToCallback( m_buttonOk, 'OnButtonClick', this, 'OnOkClick' );
		inkWidgetRef.RegisterToCallback( m_buttonCancel, 'OnButtonClick', this, 'OnCancelClick' );
		inkWidgetRef.SetVisible( m_root, true );
		inkWidgetRef.SetVisible( m_background, true );
		m_selectedSlotIndex = -1;
		m_comparisonResolver = InventoryItemPreferredComparisonResolver.Make( m_data.inventoryScriptableSystem );
		SpawnWeaponSlots( m_data.inventoryScriptableSystem );
		PlayLibraryAnimation( 'backpack_equip_chooser_intro' );
	}

	private function SpawnWeaponSlots( inventoryScriptableSystem : UIInventoryScriptableSystem )
	{
		var i : Int32;
		var controller : InventoryItemDisplayController;
		inkCompoundRef.RemoveAllChildren( m_weaponSlotsContainer );
		for( i = 0; i < 3; i += 1 )
		{
			controller = ( ( InventoryItemDisplayController )( ItemDisplayUtils.SpawnCommonSlotController( this, m_weaponSlotsContainer, 'weaponDisplay' ) ) );
			controller.RegisterToCallback( 'OnHoverOver', this, 'OnSlotHoverOver' );
			controller.RegisterToCallback( 'OnHoverOut', this, 'OnSlotHoverOut' );
			controller.RegisterToCallback( 'OnRelease', this, 'OnSlotClick' );
			controller.Bind( inventoryScriptableSystem, gamedataEquipmentArea.Weapon, i, ItemDisplayContext.GearPanel );
		}
	}

	protected event OnSlotHoverOut( e : inkPointerEvent )
	{
		m_tooltipsManager.HideTooltips();
	}

	protected event OnSlotHoverOver( e : inkPointerEvent )
	{
		var controller : InventoryItemDisplayController;
		var inspectedItem : weak< UIInventoryItem >;
		var widget : weak< inkWidget >;
		var itemTooltipData : UIInventoryItemTooltipWrapper;
		m_tooltipsManager.HideTooltips();
		widget = e.GetCurrentTarget();
		controller = ( ( InventoryItemDisplayController )( widget.GetController() ) );
		if( controller )
		{
			inspectedItem = controller.GetUIInventoryItem();
		}
		if( inspectedItem && controller )
		{
			itemTooltipData = new UIInventoryItemTooltipWrapper;
			itemTooltipData.m_data = inspectedItem;
			m_tooltipsManager.ShowTooltipAtWidget( 'itemTooltip', e.GetCurrentTarget(), itemTooltipData, gameuiETooltipPlacement.RightTop );
		}
	}

	protected event OnSlotClick( e : inkPointerEvent )
	{
		var controller : InventoryItemDisplayController;
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( InventoryItemDisplayController )( e.GetCurrentTarget().GetController() ) );
			if( controller )
			{
				m_selectedSlotIndex = controller.GetSlotIndex();
				Close( true );
			}
		}
	}

	protected event OnUninitialize()
	{
		UnregisterFromGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
	}

	private function SetButtonHints()
	{
		AddButtonHints( 'UI_Apply', "UI-ResourceExports-Confirm" );
		AddButtonHints( 'UI_Cancel', "UI-ResourceExports-Cancel" );
	}

	private function AddButtonHints( actionName : CName, const label : ref< String > )
	{
		var buttonHint : LabelInputDisplayController;
		buttonHint = ( ( LabelInputDisplayController )( SpawnFromExternal( inkWidgetRef.Get( m_buttonHintsRoot ), inkWidgetLibraryResource.GetPath( m_libraryPath.widgetLibrary ), m_libraryPath.widgetItem ).GetController() ) );
		buttonHint.SetInputActionLabel( actionName, label );
	}

	protected event OnHandlePressInput( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'cancel' ) )
		{
			Close( false );
		}
	}

	protected event OnOkClick( controller : weak< inkButtonController > )
	{
		Close( true );
	}

	protected event OnCancelClick( controller : weak< inkButtonController > )
	{
		Close( false );
	}


	private var m_closeData : BackpackEquipSlotChooserCloseData;

	private function Close( success : Bool )
	{
		var closeAnimProxy : inkAnimProxy;
		m_closeData = new BackpackEquipSlotChooserCloseData;
		m_closeData.confirm = success;
		m_closeData.slotIndex = m_selectedSlotIndex;
		m_closeData.itemData = m_data.item;
		closeAnimProxy = PlayLibraryAnimation( 'backpack_equip_chooser_outro' );
		closeAnimProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnCloseAnimationFinished' );
	}

	protected event OnCloseAnimationFinished( proxy : inkAnimProxy )
	{
		m_data.token.TriggerCallback( m_closeData );
	}

}

class BackpackEquipSlotChooserData extends inkGameNotificationData
{
	var item : weak< UIInventoryItem >;
	var inventoryScriptableSystem : UIInventoryScriptableSystem;
}

class BackpackEquipSlotChooserCloseData extends inkGameNotificationData
{
	var confirm : Bool;
	var itemData : weak< UIInventoryItem >;
	var slotIndex : Int32;
}

