class VendorSellJunkPopup 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 editable var m_headerText : inkTextRef;
	private editable var m_sellItemsFullQuantity : inkTextRef;
	private editable var m_sellItemsLimitedQuantity : inkTextRef;
	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_closeAnimProxy : inkAnimProxy;
	private var m_data : VendorSellJunkPopupData;
	private editable var m_libraryPath : inkWidgetLibraryReference;

	protected event OnInitialize()
	{
		var fullQuantityParams, limitedQuantityParams : inkTextParams;
		RegisterToGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
		m_data = ( ( VendorSellJunkPopupData )( GetRootWidget().GetUserData( 'VendorSellJunkPopupData' ) ) );
		inkWidgetRef.RegisterToCallback( m_buttonOk, 'OnButtonClick', this, 'OnOkClick' );
		inkWidgetRef.RegisterToCallback( m_buttonCancel, 'OnButtonClick', this, 'OnCancelClick' );
		inkWidgetRef.SetVisible( m_root, true );
		inkWidgetRef.SetVisible( m_background, true );
		if( m_data.actionType == VendorSellJunkActionType.Sell )
		{
			inkTextRef.SetText( m_itemNameText, "UI-PopupNotification-confirm_sell" );
			inkTextRef.SetText( m_headerText, "LocKey#53391" );
			fullQuantityParams = new inkTextParams;
			limitedQuantityParams = new inkTextParams;
			fullQuantityParams.AddNumber( "quantity", m_data.itemsQuantity );
			fullQuantityParams.AddNumber( "value", RoundF( m_data.totalPrice ) );
			limitedQuantityParams.AddNumber( "quantity", m_data.limitedItemsQuantity );
			limitedQuantityParams.AddNumber( "value", m_data.limitedTotalPrice );
			inkTextRef.SetTextParameters( m_sellItemsFullQuantity, fullQuantityParams );
			inkTextRef.SetTextParameters( m_sellItemsLimitedQuantity, limitedQuantityParams );
			inkTextRef.SetVisible( m_sellItemsLimitedQuantity, m_data.itemsQuantity != m_data.limitedItemsQuantity );
		}
		else if( m_data.actionType == VendorSellJunkActionType.Disassemble )
		{
			inkTextRef.SetText( m_headerText, "LocKey#95165" );
			fullQuantityParams = new inkTextParams;
			fullQuantityParams.AddNumber( "quantity", m_data.itemsQuantity );
			inkTextRef.SetText( m_sellItemsFullQuantity, "UI-PopupNotification-DisassembleJunkInfo" );
			inkTextRef.SetTextParameters( m_sellItemsFullQuantity, fullQuantityParams );
			inkTextRef.SetVisible( m_sellItemsLimitedQuantity, false );
		}
		PlayLibraryAnimation( 'vendor_popup_sell_junk_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( 'proceed' ) )
		{
			Close( true );
		}
		else 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 : VendorSellJunkPopupCloseData;

	private function Close( success : Bool )
	{
		m_closeData = new VendorSellJunkPopupCloseData;
		m_closeData.confirm = success;
		m_closeData.items = m_data.items;
		m_closeData.limitedItems = m_data.limitedItems;
		if( m_closeAnimProxy.IsPlaying() )
		{
			m_closeAnimProxy.Stop();
		}
		m_closeAnimProxy = PlayLibraryAnimation( 'vendor_popup_sell_junk_outro' );
		if( m_closeAnimProxy.IsValid() )
		{
			m_closeAnimProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnCloseAnimationFinished' );
		}
		else
		{
			OnCloseAnimationFinished( m_closeAnimProxy );
		}
	}

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

}

class VendorSellJunkPopupData extends inkGameNotificationData
{
	var items : array< weak< gameItemData > >;
	var limitedItems : array< VendorJunkSellItem >;
	var itemsQuantity : Int32;
	var limitedItemsQuantity : Int32;
	var totalPrice : Float;
	var limitedTotalPrice : Int32;
	var actionType : VendorSellJunkActionType;
}

enum VendorSellJunkActionType
{
	Sell = 0,
	Disassemble = 1,
}

class VendorSellJunkPopupCloseData extends inkGameNotificationData
{
	var confirm : Bool;
	var items : array< weak< gameItemData > >;
	var limitedItems : array< VendorJunkSellItem >;
}

