class VendingMachineInkGameController extends DeviceInkGameControllerBase
{
	[ category = "Widget Refs" ]
	protected editable var m_ActionsPanel : inkHorizontalPanelRef;
	[ category = "Widget Refs" ]
	protected editable var m_ActionsPanel2 : inkHorizontalPanelRef;
	[ category = "Widget Refs" ]
	protected editable var m_priceText : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_noMoneyPanel : inkCompoundRef;
	[ category = "Widget Refs" ]
	protected editable var m_soldOutPanel : inkCompoundRef;
	protected var m_state : PaymentStatus;
	protected var m_soldOut : Bool;
	private var m_onUpdateStatusListener : CallbackHandle;
	private var m_onGlitchingStateChangedListener : CallbackHandle;
	private var m_onSoldOutListener : CallbackHandle;

	protected event OnInitialize()
	{
		super.OnInitialize();
		inkCompoundRef.SetVisible( m_noMoneyPanel, false );
		inkCompoundRef.SetVisible( m_soldOutPanel, false );
	}

	protected override function RegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.RegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			m_onUpdateStatusListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().ActionStatus, this, 'OnUpdateStatus' );
			m_onGlitchingStateChangedListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, this, 'OnGlitchingStateChanged' );
			m_onSoldOutListener = blackboard.RegisterListenerBool( GetOwner().GetBlackboardDef().SoldOut, this, 'OnSoldOut' );
		}
	}

	protected override function UnRegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.UnRegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().ActionStatus, m_onUpdateStatusListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, m_onGlitchingStateChangedListener );
			blackboard.UnregisterListenerBool( GetOwner().GetBlackboardDef().SoldOut, m_onSoldOutListener );
		}
	}

	public override function UpdateActionWidgets( const widgetsData : ref< array< SActionWidgetPackage > > )
	{
		var i : Int32;
		var widget : inkWidget;
		var action : DispenceItemFromVendor;
		HideActionWidgets();
		if( !( m_soldOut ) )
		{
			inkHorizontalPanelRef.SetVisible( m_ActionsPanel, true );
			inkHorizontalPanelRef.SetVisible( m_ActionsPanel2, true );
			inkCompoundRef.SetVisible( m_noMoneyPanel, false );
			inkCompoundRef.SetVisible( m_soldOutPanel, false );
			for( i = 0; i < widgetsData.Size(); i += 1 )
			{
				widget = GetActionWidget( widgetsData[ i ] );
				if( widget == NULL )
				{
					if( i <= 2 )
					{
						CreateActionWidgetAsync( inkHorizontalPanelRef.Get( m_ActionsPanel ), widgetsData[ i ] );
					}
					else
					{
						CreateActionWidgetAsync( inkHorizontalPanelRef.Get( m_ActionsPanel2 ), widgetsData[ i ] );
					}
				}
				else
				{
					InitializeActionWidget( widget, widgetsData[ i ] );
				}
			}
			action = ( ( DispenceItemFromVendor )( widgetsData[ 0 ].action ) );
			UpdatePrice( action.GetPrice() );
		}
	}

	protected event OnSoldOut( value : Bool )
	{
		m_soldOut = value;
		if( m_soldOut )
		{
			inkCompoundRef.SetVisible( m_soldOutPanel, true );
			inkHorizontalPanelRef.SetVisible( m_ActionsPanel, false );
			inkHorizontalPanelRef.SetVisible( m_ActionsPanel2, false );
			TriggerAnimationByName( 'sold_out', EInkAnimationPlaybackOption.PLAY );
		}
		else
		{
			inkCompoundRef.SetVisible( m_soldOutPanel, false );
			inkHorizontalPanelRef.SetVisible( m_ActionsPanel, true );
			inkHorizontalPanelRef.SetVisible( m_ActionsPanel2, true );
		}
	}

	protected event OnUpdateStatus( value : Variant ) {}

	protected override function ExecuteDeviceActions( controller : DeviceActionWidgetControllerBase )
	{
		var i : Int32;
		var actions : array< weak< DeviceAction > >;
		var executor : weak< GameObject >;
		var buyAction : DispenceItemFromVendor;
		if( controller != NULL )
		{
			if( controller.CanExecuteAction() )
			{
				actions = controller.GetActions();
			}
		}
		executor = GetPlayer( GetOwner().GetGame() );
		for( i = 0; i < actions.Size(); i += 1 )
		{
			buyAction = ( ( DispenceItemFromVendor )( actions[ i ] ) );
			if( buyAction && !( buyAction.CanPay( executor ) ) )
			{
				inkCompoundRef.SetVisible( m_noMoneyPanel, true );
				PlayLibraryAnimation( 'no_money' );
			}
			else
			{
				ExecuteAction( actions[ i ], executor );
				controller.FinalizeActionExecution( executor, actions[ i ] );
			}
		}
	}

	protected event OnButtonHoverOver( e : inkPointerEvent )
	{
		var button : ImageActionButtonLogicController;
		button = ( ( ImageActionButtonLogicController )( e.GetCurrentTarget().GetController() ) );
		if( button )
		{
			UpdatePrice( ( ( Int32 )( button.GetPrice() ) ) );
		}
	}

	private function UpdatePrice( price : Int32 )
	{
		var textParams : inkTextParams;
		textParams = new inkTextParams;
		textParams.AddNumber( "COST", price );
		textParams.AddLocalizedString( "ED", "LocKey#884" );
		inkTextRef.SetLocalizedTextString( m_priceText, "LocKey#45350", textParams );
	}

	protected override function GetOwner() : VendingMachine
	{
		return ( ( VendingMachine )( GetOwnerEntity() ) );
	}

	protected override function Refresh( state : EDeviceStatus )
	{
		SetupWidgets();
		switch( state )
		{
			case EDeviceStatus.ON:
				TurnOn();
			break;
			case EDeviceStatus.OFF:
				TurnOff();
			break;
			case EDeviceStatus.UNPOWERED:
				TurnOff();
			break;
			case EDeviceStatus.DISABLED:
				TurnOff();
			break;
			default:
				break;
		}
		super.Refresh( state );
	}

	protected virtual function TurnOn()
	{
		m_rootWidget.SetVisible( true );
		RequestActionWidgetsUpdate();
	}

	protected virtual function TurnOff()
	{
		m_rootWidget.SetVisible( false );
	}

}

