class VendingTerminal extends InteractiveDevice
{
	default m_controllerTypeName = 'VendingTerminalController';
	var m_position : Vector4;
	protected var m_canMeshComponent : MeshComponent;
	protected const instanceeditable var m_vendingBlacklist : array< EVendorMode >;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
		EntityRequestComponentsInterface.RequestComponent( ri, 'ui', 'worlduiWidgetComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'CanMesh', 'MeshComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'Trigger', 'gameStaticTriggerAreaComponent', false );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_canMeshComponent = ( ( MeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'CanMesh' ) ) );
		m_uiComponent = ( ( worlduiWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ui' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( VendingTerminalController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		GetDevicePS().Prepare( this );
		if( IsUIdirty() && m_isInsideLogicArea )
		{
			RefreshUI();
		}
	}

	protected event OnDetach()
	{
		super.OnDetach();
	}

	public const override function GetBlackboardDef() : VendingMachineDeviceBlackboardDef
	{
		return GetDevicePS().GetBlackboardDef();
	}

	protected override function CreateBlackboard()
	{
		m_blackboard = IBlackboard.Create( GetAllBlackboardDefs().VendingMachineDeviceBlackboard );
	}

	protected const override function GetController() : VendingTerminalController
	{
		return ( ( VendingTerminalController )( m_controller ) );
	}

	public const override function GetDevicePS() : VendingTerminalControllerPS
	{
		return GetController().GetPS();
	}

	protected override function PushPersistentData()
	{
		super.PushPersistentData();
	}

	protected event OnAreaEnter( evt : AreaEnteredEvent )
	{
		RefreshUI();
	}

	protected event OnAreaExit( evt : AreaExitedEvent )
	{
		GetDevicePS().GetVendorDataManager().ClearCart();
		RefreshUI();
	}

	protected event OnCraftItemForTarget( evt : CraftItemForTarget )
	{
		RefreshUI();
		PlaySoundEvent( this, 'dev_vending_machine_processing' );
	}

	protected event OnBuyItemFromVendor( evt : BuyItemFromVendor )
	{
		var buyRequest : BuyRequest;
		var buyRequestData : TransactionRequestData;
		buyRequestData.itemID = evt.itemID;
		buyRequest = new BuyRequest;
		buyRequest.owner = this;
		buyRequest.items.PushBack( buyRequestData );
		MarketSystem.GetInstance( GetGame() ).QueueRequest( buyRequest );
		RefreshUI();
		PlaySoundEvent( this, 'dev_vending_machine_processing' );
	}

	protected event OnSellItemToVendor( evt : SellItemToVendor )
	{
		var sellRequest : SellRequest;
		var sellRequestData : TransactionRequestData;
		sellRequestData.itemID = evt.itemID;
		sellRequest = new SellRequest;
		sellRequest.owner = this;
		sellRequest.items.PushBack( sellRequestData );
		MarketSystem.GetInstance( GetGame() ).QueueRequest( sellRequest );
		RefreshUI();
		PlaySoundEvent( this, 'dev_vending_machine_processing' );
	}

	protected event OnDispenceItemFromVendor( evt : DispenceItemFromVendor )
	{
		RefreshUI();
		DelayVendingMachineEvent( GetDevicePS().GetVendorDataManager().GetTimeToCompletePurchase(), evt.GetItemID() );
		PlaySoundEvent( this, 'dev_vending_machine_processing' );
	}

	protected event OnVendingMachineFinishedEvent( evt : VendingMachineFinishedEvent )
	{
		m_position = Matrix.GetTranslation( m_canMeshComponent.GetLocalToWorld() );
		GameInstance.GetLootManager( GetGame() ).SpawnItemDrop( this, evt.itemID, m_position );
		PlaySoundEvent( this, 'dev_vending_machine_can_falls' );
		GetDevicePS().SetIsReady( true );
		RefreshUI();
	}

	protected function SendDataToUIBlackboard( const TopText : ref< String >, const BottomText : ref< String > )
	{
		GetBlackboard().FireCallbacks();
	}

	protected function DelayVendingMachineEvent( time : Float, itemID : ItemID )
	{
		var evt : VendingMachineFinishedEvent;
		evt = new VendingMachineFinishedEvent;
		evt.itemID = itemID;
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, evt, time );
	}

	private override function InitializeScreenDefinition()
	{
		if( !( TDBID.IsValid( m_screenDefinition.screenDefinition ) ) )
		{
			m_screenDefinition.screenDefinition = T"DevicesUIDefinitions.Terminal_4x3";
		}
		if( !( TDBID.IsValid( m_screenDefinition.style ) ) )
		{
			m_screenDefinition.style = T"DevicesUIStyles.None";
		}
	}

}

