class VendorConfirmationPopupData extends inkGameNotificationData
{
	var itemData : InventoryItemData;
	var inventoryItem : weak< UIInventoryItem >;
	var quantity : Int32;
	var type : VendorConfirmationPopupType;
	var price : Int32;
}

enum VendorConfirmationPopupType
{
	Default = 0,
	ExpensiveItem = 1,
	EquippedItem = 2,
	StashEquippedItem = 3,
	BuyAndEquipCyberware = 4,
	BuyNotEquipableCyberware = 5,
	SellCyberware = 6,
	DisassembeIconic = 7,
}

class VendorConfirmationPopupCloseData extends inkGameNotificationData
{
	var confirm : Bool;
	var itemData : InventoryItemData;
	var inventoryItem : weak< UIInventoryItem >;
	var quantity : Int32;
	var type : VendorConfirmationPopupType;
}

class VendorConfirmationPopup extends inkGameController
{
	private editable var m_itemNameText : inkTextRef;
	private editable var m_buttonHintsRoot : inkWidgetRef;
	private editable var m_itemDisplayRef : inkWidgetRef;
	private editable var m_rairtyBar : inkWidgetRef;
	private editable var m_eqippedItemContainer : inkWidgetRef;
	private editable var m_itemPriceContainer : inkWidgetRef;
	private editable var m_itemPriceText : inkTextRef;
	private editable var m_root : inkWidgetRef;
	private editable var m_background : inkWidgetRef;
	private var m_closeData : VendorConfirmationPopupCloseData;
	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 : VendorConfirmationPopupData;
	private var m_itemDisplayController : weak< InventoryItemDisplayController >;
	private var m_displayContextData : ItemDisplayContextData;
	private editable var m_libraryPath : inkWidgetLibraryReference;

	protected event OnInitialize()
	{
		var headerText : String;
		var quantity : Int32;
		RegisterToGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
		m_data = ( ( VendorConfirmationPopupData )( GetRootWidget().GetUserData( 'VendorConfirmationPopupData' ) ) );
		m_itemDisplayController = ( ( InventoryItemDisplayController )( inkWidgetRef.GetController( m_itemDisplayRef ) ) );
		if( m_data.inventoryItem )
		{
			quantity = m_data.quantity;
			if( quantity <= 0 )
			{
				quantity = m_data.inventoryItem.GetQuantity();
			}
			m_displayContextData = ItemDisplayContextData.Make();
			m_itemDisplayController.Setup( m_data.inventoryItem, m_displayContextData, false, false, false, quantity );
		}
		else
		{
			m_itemDisplayController.Setup( m_data.itemData );
		}
		switch( m_data.type )
		{
			case VendorConfirmationPopupType.BuyAndEquipCyberware:
				headerText = "UI-PopupNotification-BuyAndEquipeCyberware";
			break;
			case VendorConfirmationPopupType.BuyNotEquipableCyberware:
				headerText = "UI-PopupNotification-BuyNotEqupableCyberware";
			break;
			case VendorConfirmationPopupType.DisassembeIconic:
				headerText = "UI-PopupNotification-dissembling_iconic";
			break;
			case VendorConfirmationPopupType.StashEquippedItem:
				headerText = "UI-PopupNotification-TransferEquippedItem";
			break;
			default:
				headerText = "UI-PopupNotification-confirm_sell";
		}
		inkTextRef.SetText( m_itemNameText, headerText );
		inkWidgetRef.RegisterToCallback( m_buttonOk, 'OnRelease', this, 'OnOkClick' );
		inkWidgetRef.RegisterToCallback( m_buttonCancel, 'OnRelease', this, 'OnCancelClick' );
		inkWidgetRef.SetVisible( m_root, true );
		inkWidgetRef.SetVisible( m_background, true );
		if( m_data.inventoryItem )
		{
			inkWidgetRef.SetState( m_rairtyBar, m_data.inventoryItem.GetQualityName() );
		}
		else if( !( InventoryItemData.IsEmpty( m_data.itemData ) ) )
		{
			inkWidgetRef.SetState( m_rairtyBar, InventoryItemData.GetQuality( m_data.itemData ) );
		}
		else
		{
			inkWidgetRef.SetState( m_rairtyBar, 'Common' );
		}
		inkWidgetRef.SetVisible( m_eqippedItemContainer, m_data.type == VendorConfirmationPopupType.EquippedItem || m_data.type == VendorConfirmationPopupType.StashEquippedItem );
		if( ( m_data.price > 0 ) && m_data.type != VendorConfirmationPopupType.StashEquippedItem )
		{
			inkWidgetRef.SetVisible( m_itemPriceContainer, true );
			inkTextRef.SetText( m_itemPriceText, IntToString( m_data.price ) );
		}
		else
		{
			inkWidgetRef.SetVisible( m_itemPriceContainer, false );
		}
		PlayLibraryAnimation( 'vendor_popup_confirmation_intro' );
	}

	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( 'one_click_confirm' ) )
		{
			Close( true );
		}
		else if( evt.IsAction( 'cancel' ) )
		{
			Close( false );
		}
	}

	protected event OnOkClick( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'click' ) )
		{
			Close( true );
		}
	}

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

	private function Close( success : Bool )
	{
		var closeAnimProxy : inkAnimProxy;
		m_closeData = new VendorConfirmationPopupCloseData;
		m_closeData.confirm = success;
		m_closeData.itemData = m_data.itemData;
		m_closeData.inventoryItem = m_data.inventoryItem;
		m_closeData.quantity = m_data.quantity;
		m_closeData.type = m_data.type;
		closeAnimProxy = PlayLibraryAnimation( 'vendr_popup_confirmation_outro' );
		closeAnimProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnCloseAnimationFinished' );
	}

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

}

