enum PaymentStatus
{
	DEFAULT = 0,
	IN_PROGRESS = 1,
	NO_MONEY = 2,
}

struct VendingMachineSetup
{
	instanceeditable var m_timeToCompletePurchase : Float;
	default m_timeToCompletePurchase = 0.1f;
}

struct VendingMachineSFX
{
	[ customEditor = "AudioEvent" ]
	instanceeditable var m_glitchingStart : CName;
	default m_glitchingStart = 'amb_int_custom_megabuilding_01_adverts_interactive_nicola_01_select_q110';
	[ customEditor = "AudioEvent" ]
	instanceeditable var m_glitchingStop : CName;
	default m_glitchingStop = 'amb_int_custom_megabuilding_01_adverts_interactive_nicola_01_select_q110_stop';
}

class VendingMachineController extends ScriptableDeviceComponent
{

	public const override function GetPS() : VendingMachineControllerPS
	{
		return ( ( VendingMachineControllerPS )( GetBasePS() ) );
	}

}

class VendingMachineControllerPS extends ScriptableDeviceComponentPS
{
	default m_tweakDBRecord = T"Devices.VendingMachine";
	default m_tweakDBDescriptionRecord = T"device_descriptions.VendingMachine";
	default m_deviceName = "LocKey#176";
	private instanceeditable persistent var m_vendingMachineSetup : VendingMachineSetup;
	private instanceeditable var m_vendingMachineSFX : VendingMachineSFX;
	[ rangeMin = "0.f" ][ rangeMax = "1.f" ]
	protected instanceeditable var m_soldOutProbability : Float;
	default m_soldOutProbability = 0.05f;
	protected var m_isReady : Bool;
	default m_isReady = true;
	protected var m_isSoldOut : Bool;
	protected var m_hackCount : Int32;
	default m_hackCount = 2;
	private var m_shopStock : array< SItemStack >;
	protected var m_shopStockInit : Bool;

	protected override function LogicReady()
	{
		super.LogicReady();
		m_shopStockInit = false;
	}

	public virtual function GetTimeToCompletePurchase() : Float
	{
		return m_vendingMachineSetup.m_timeToCompletePurchase;
	}

	public virtual function GetGlitchStartSFX() : CName
	{
		return m_vendingMachineSFX.m_glitchingStart;
	}

	public virtual function GetGlitchStopSFX() : CName
	{
		return m_vendingMachineSFX.m_glitchingStop;
	}

	protected function GetShopStock() : array< SItemStack >
	{
		if( !( m_shopStockInit ) )
		{
			m_shopStock = MarketSystem.GetVendorItemsForSale( ( ( GameObject )( GetOwnerEntityWeak() ) ), false );
			m_shopStockInit = true;
		}
		return m_shopStock;
	}

	public virtual function GetHackedItemCount() : Int32
	{
		return 10;
	}

	public function IsSoldOut() : Bool
	{
		return m_isSoldOut;
	}

	protected function ActionDispenceItemFromVendor( item : ItemID, price : Int32 ) : DispenceItemFromVendor
	{
		var action : DispenceItemFromVendor;
		var buttonTextureName : CName;
		buttonTextureName = TweakDBInterface.GetConsumableItemRecord( ItemID.GetTDBID( item ) ).AtlasIcon();
		action = new DispenceItemFromVendor;
		action.clearanceLevel = 1;
		action.SetUp( this );
		action.SetProperties( item, price, buttonTextureName );
		action.AddDeviceName( m_deviceName );
		action.CreateActionWidgetPackage();
		action.SetDurationValue( GetTimeToCompletePurchase() );
		return action;
	}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		if( !( super.GetActions( actions, context ) ) )
		{
			m_isReady = false;
			return false;
		}
		else
		{
			PushShopStockActions( actions, context );
		}
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

	protected virtual function PushShopStockActions( actions : ref< array< DeviceAction > >, const context : ref< GetActionsContext > )
	{
		var i : Int32;
		var price : Int32;
		var shopStock : array< SItemStack >;
		if( DispenceItemFromVendor.IsDefaultConditionMet( this, context ) && m_isReady )
		{
			shopStock = GetShopStock();
			for( i = 0; i < shopStock.Size(); i += 1 )
			{
				price = MarketSystem.GetBuyPrice( ( ( GameObject )( GetOwnerEntityWeak() ) ), shopStock[ i ].itemID );
				actions.PushBack( ActionDispenceItemFromVendor( shopStock[ i ].itemID, price ) );
			}
		}
	}

	protected const override function CanCreateAnyQuickHackActions() : Bool
	{
		return true;
	}

	protected override function GetQuickHackActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var currentAction : ScriptableDeviceAction;
		currentAction = ActionGlitchScreen( T"DeviceAction.GlitchScreenSuicide", T"QuickHack.GrenadeHackBase" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		outActions.PushBack( currentAction );
		currentAction = ActionGlitchScreen( T"DeviceAction.GlitchScreenBlind", T"QuickHack.BlindHack" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		outActions.PushBack( currentAction );
		currentAction = ActionGlitchScreen( T"DeviceAction.GlitchScreenGrenade", T"QuickHack.GrenadeHackBase" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		outActions.PushBack( currentAction );
		if( !( GlitchScreen.IsDefaultConditionMet( this, context ) ) )
		{
			SetActionsInactiveAll( outActions, "LocKey#7003" );
		}
		currentAction = ActionQuickHackDistraction();
		currentAction.SetObjectActionID( T"DeviceAction.MalfunctionClassHack" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		if( !( QuickHackDistraction.IsDefaultConditionMet( this, context ) ) || ( m_hackCount < 1 ) )
		{
			currentAction.SetInactiveWithReason( false, "LocKey#7003" );
		}
		outActions.PushBack( currentAction );
		if( IsGlitching() || IsDistracting() )
		{
			SetActionsInactiveAll( outActions, "LocKey#7004" );
		}
		if( !( IsON() ) )
		{
			SetActionsInactiveAll( outActions, "LocKey#7005" );
		}
		FinalizeGetQuickHackActions( outActions, context );
	}

	public override function GetQuestActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		super.GetQuestActions( outActions, context );
	}

	public export virtual function OnDispenceItemFromVendor( evt : DispenceItemFromVendor ) : EntityNotificationType
	{
		if( evt.IsStarted() )
		{
			m_isReady = false;
			ExecutePSActionWithDelay( evt, this, evt.GetDurationValue() );
		}
		else
		{
			if( RandF() <= m_soldOutProbability )
			{
				m_isSoldOut = true;
			}
			else
			{
				m_isReady = true;
			}
		}
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnQuickHackDistraction( evt : QuickHackDistraction ) : EntityNotificationType
	{
		if( evt.IsStarted() )
		{
			m_isReady = false;
			m_hackCount -= 1;
			if( m_hackCount == 0 )
			{
				m_isSoldOut = true;
			}
		}
		return super.OnQuickHackDistraction( evt );
	}

	public function SetIsReady( value : Bool )
	{
		m_isReady = value;
	}

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

}

