class CraftingPopupController extends inkGameController
{
	private editable var m_tooltipContainer : inkWidgetRef;
	private editable var m_craftIcon : inkImageRef;
	private editable var m_itemName : inkTextRef;
	private editable var m_itemTopName : inkTextRef;
	private editable var m_itemQuality : inkTextRef;
	private editable var m_headerText : inkTextRef;
	private editable var m_closeButton : inkWidgetRef;
	private editable var m_buttonHintsRoot : inkWidgetRef;
	private editable var m_libraryPath : inkWidgetLibraryReference;
	private var m_itemTooltip : weak< AGenericTooltipController >;
	private var m_closeButtonController : weak< inkButtonController >;
	private var m_data : CraftingPopupData;

	protected event OnInitialize()
	{
		m_closeButtonController = ( ( inkButtonController )( inkWidgetRef.GetController( m_closeButton ) ) );
		m_closeButtonController.RegisterToCallback( 'OnButtonClick', this, 'OnOkClick' );
		RegisterToGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
		m_data = ( ( CraftingPopupData )( GetRootWidget().GetUserData( 'CraftingPopupData' ) ) );
		SetPopupData( m_data.itemTooltipData, m_data.craftingCommand );
		AddButtonHint();
	}

	private function AddButtonHint()
	{
		var buttonHint : LabelInputDisplayController;
		buttonHint = ( ( LabelInputDisplayController )( SpawnFromExternal( inkWidgetRef.Get( m_buttonHintsRoot ), inkWidgetLibraryResource.GetPath( m_libraryPath.widgetLibrary ), m_libraryPath.widgetItem ).GetController() ) );
		buttonHint.SetInputActionLabel( 'UI_Cancel', "UI-ResourceExports-Confirm" );
	}

	protected event OnHandlePressInput( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'cancel' ) )
		{
			m_data.token.TriggerCallback( NULL );
		}
	}

	private function SetPopupData( tooltipsData : InventoryTooltipData, command : CraftingCommands )
	{
		var stateName : CName;
		var stateNameLoc : String;
		var previewEvent : CraftingItemPreviewEvent;
		var rarityItemType : RarityItemType;
		previewEvent = new CraftingItemPreviewEvent;
		if( m_itemTooltip == NULL )
		{
			m_itemTooltip = ( ( AGenericTooltipController )( SpawnFromExternal( inkWidgetRef.Get( m_tooltipContainer ), R"base\gameplay\gui\common\tooltip\tooltipslibrary_4k.inkwidget", 'itemTooltip' ).GetController() ) );
		}
		previewEvent.itemID = tooltipsData.itemID;
		QueueEvent( previewEvent );
		stateName = InventoryItemData.GetQuality( tooltipsData.inventoryItemData );
		rarityItemType = UIItemsHelper.ItemTypeToRarity( InventoryItemData.GetItemType( tooltipsData.inventoryItemData ), InventoryItemData.GetGameItemData( tooltipsData.inventoryItemData ) );
		stateNameLoc = UIItemsHelper.QualityToDefaultString( UIItemsHelper.QualityNameToEnum( stateName ), rarityItemType );
		inkTextRef.SetText( m_itemName, tooltipsData.itemName );
		inkTextRef.SetState( m_itemName, stateName );
		inkTextRef.SetText( m_itemQuality, stateNameLoc );
		inkTextRef.SetText( m_itemTopName, tooltipsData.itemName );
		m_itemTooltip.SetData( tooltipsData );
		switch( command )
		{
			case CraftingCommands.CraftingFinished:
				inkTextRef.SetText( m_headerText, "UI-Crafting-CraftingFinishedNotification" );
			inkImageRef.SetTexturePart( m_craftIcon, 'ico_cafting_crafting' );
			break;
			case CraftingCommands.UpgradingFinished:
				inkTextRef.SetText( m_headerText, "UI-Crafting-UpgradingFinishedNotification" );
			inkImageRef.SetTexturePart( m_craftIcon, 'ico_cafting_upgrading' );
			break;
		}
	}

	protected event OnOkClick( controller : weak< inkButtonController > )
	{
		m_data.token.TriggerCallback( NULL );
	}

	protected event OnUninitialize()
	{
		m_closeButtonController.UnregisterFromCallback( 'OnButtonClick', this, 'OnOkClick' );
		UnregisterFromGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
	}

}

class CraftingPopupData extends inkGameNotificationData
{
	var itemTooltipData : InventoryTooltipData;
	var craftingCommand : CraftingCommands;
}

