class PayActionWidgetController extends DeviceActionWidgetControllerBase
{
	[ category = "Widget Refs" ]
	protected editable var m_priceContainer : inkWidgetRef;
	[ category = "Widget Refs" ]
	protected editable var m_moneyStatusContainer : inkWidgetRef;
	[ category = "Widget Refs" ]
	protected editable var m_processingStatusContainer : inkWidgetRef;
	[ category = "Animations" ]
	protected editable var m_moneyStatusAnimName : CName;
	default m_moneyStatusAnimName = 'no_money';
	[ category = "Animations" ]
	protected editable var m_processingAnimName : CName;
	default m_processingAnimName = 'pay';
	private var m_isProcessingPayment : Bool;

	public override function Initialize( gameController : DeviceInkGameControllerBase, const widgetData : ref< SActionWidgetPackage > )
	{
		var action : Pay;
		var textParams : inkTextParams;
		super.Initialize( gameController, widgetData );
		action = ( ( Pay )( widgetData.action ) );
		inkWidgetRef.SetVisible( m_moneyStatusContainer, false );
		inkWidgetRef.SetVisible( m_processingStatusContainer, false );
		if( action )
		{
			textParams = new inkTextParams;
			textParams.AddNumber( "COST", action.GetCost() );
			textParams.AddLocalizedString( "ED", "LocKey#884" );
			textParams.AddString( "STATUS", "" );
			inkTextRef.SetTextParameters( m_displayNameWidget, textParams );
		}
	}

	public override function FinalizeActionExecution( executor : GameObject, action : DeviceAction )
	{
		var contextAction : Pay;
		contextAction = ( ( Pay )( action ) );
		if( contextAction )
		{
			ProcessPayment( contextAction, executor );
		}
	}

	protected function ProcessPayment( action : Pay, executor : GameObject )
	{
		if( action && !( m_isProcessingPayment ) )
		{
			m_isProcessingPayment = true;
			m_targetWidget.SetInteractive( false );
			if( action.CanPayCost( executor ) )
			{
				PlayLibraryAnimation( m_processingAnimName ).RegisterToCallback( inkanimEventType.OnFinish, this, 'OnPaymentProcessed' );
			}
			else
			{
				PlayLibraryAnimation( m_moneyStatusAnimName ).RegisterToCallback( inkanimEventType.OnFinish, this, 'OnNoMoneyShowed' );
			}
		}
	}

	protected event OnPaymentProcessed( e : inkAnimProxy )
	{
		e.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnPaymentProcessed' );
		m_targetWidget.SetInteractive( true );
		m_isProcessingPayment = false;
	}

	protected event OnNoMoneyShowed( e : inkAnimProxy )
	{
		e.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnNoMoneyShowed' );
		m_targetWidget.SetInteractive( true );
		m_isProcessingPayment = false;
	}

	public const override function CanExecuteAction() : Bool
	{
		return super.CanExecuteAction() && !( m_isProcessingPayment );
	}

}

