class ExplosiveTriggerDeviceController extends ExplosiveDeviceController
{

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

}

class ExplosiveTriggerDeviceControllerPS extends ExplosiveDeviceControllerPS
{
	default m_deviceName = "LocKey#42163";
	default m_tweakDBRecord = T"Devices.ExplosiveDevice";
	default m_tweakDBDescriptionRecord = T"device_descriptions.ExplosiveDevice";
	[ category = "Trigger conditions" ]
	private persistent var m_playerSafePass : Bool;
	default m_playerSafePass = false;
	private persistent var m_triggerExploded : Bool;

	protected event OnInstantiated()
	{
		super.OnInstantiated();
		if( !( IsStringValid( m_deviceName ) ) )
		{
			m_deviceName = "LocKey#42163";
		}
	}

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

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

	public const function CanPlayerSafePass() : Bool
	{
		return m_playerSafePass;
	}

	public const function IsDisarmed() : Bool
	{
		return m_disarmed;
	}

	public function IsTriggerExploded() : Bool
	{
		return m_triggerExploded;
	}

	public function SetTriggerExplodedState( state : Bool )
	{
		m_triggerExploded = state;
	}

	public override function ActionToggleON() : ToggleON
	{
		var action : ToggleON;
		action = new ToggleON;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOnClearance();
		action.SetUp( this );
		action.SetProperties( m_deviceState );
		action.AddDeviceName( m_deviceName );
		if( IsON() )
		{
			action.CreateInteraction( , "Disarm" );
		}
		else
		{
			action.CreateInteraction( , "Arm" );
		}
		action.CreateActionWidgetPackage();
		return action;
	}

	protected function ActionSpiderbotDisarmExplosiveDevice() : SpiderbotDisarmExplosiveDevice
	{
		var action : SpiderbotDisarmExplosiveDevice;
		action = new SpiderbotDisarmExplosiveDevice;
		action.clearanceLevel = DefaultActionsParametersHolder.GetSpiderbotClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.CreateInteraction();
		m_disarmed = true;
		return action;
	}

	protected function ActionSpiderbotDisarmExplosiveDevicePerformed() : SpiderbotDisarmExplosiveDevicePerformed
	{
		var action : SpiderbotDisarmExplosiveDevicePerformed;
		action = new SpiderbotDisarmExplosiveDevicePerformed;
		action.clearanceLevel = DefaultActionsParametersHolder.GetSpiderbotClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	protected override function ActionEngineering( const context : ref< GetActionsContext > ) : ActionEngineering
	{
		var action : ActionEngineering;
		var displayName : String;
		action = super.ActionEngineering( context );
		displayName = "Disarm";
		action.ResetCaption();
		action.CreateInteraction( context.processInitiatorObject, , displayName );
		return action;
	}

	public override function OnActionEngineering( evt : ActionEngineering ) : EntityNotificationType
	{
		if( !( evt.WasPassed() ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		super.OnActionEngineering( evt );
		if( evt.IsCompleted() )
		{
			if( IsON() )
			{
				Disarm( evt );
				RPGManager.GiveReward( evt.GetExecutor().GetGame(), T"RPGActionRewards.ExtractPartsSecurityTurret" );
				return EntityNotificationType.SendThisEventToEntity;
			}
		}
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected override function Disarm( action : ScriptableDeviceAction )
	{
		var actionToSend : ScriptableDeviceAction;
		actionToSend = ( ( ScriptableDeviceAction )( ActionToggleON() ) );
		actionToSend.RegisterAsRequester( PersistentID.ExtractEntityID( GetID() ) );
		actionToSend.SetExecutor( action.GetExecutor() );
		GetPersistencySystem().QueuePSDeviceEvent( actionToSend );
		SetBlockSecurityWakeUp( true );
		m_disarmed = true;
	}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		if( !( super.GetActions( actions, context ) ) )
		{
			return false;
		}
		if( TogglePower.IsDefaultConditionMet( this, context ) && context.requestType == gamedeviceRequestType.External )
		{
			actions.PushBack( ActionTogglePower() );
		}
		if( ToggleON.IsDefaultConditionMet( this, context ) && ( context.requestType != gamedeviceRequestType.Direct && context.requestType != gamedeviceRequestType.Remote ) )
		{
			actions.PushBack( ActionToggleON() );
		}
		actions.PushBack( ActionForceDetonate() );
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

	public override function GetQuestActionByName( actionName : CName ) : DeviceAction
	{
		var action : DeviceAction;
		action = super.GetQuestActionByName( actionName );
		if( action == NULL )
		{
			switch( actionName )
			{
				case 'ForceSafeForPlayer':
					action = ActionQuestSetPlayerSafePass( true );
				break;
				case 'DisableSafeForPlayer':
					action = ActionQuestSetPlayerSafePass( false );
				break;
			}
		}
		return action;
	}

	public override function GetQuestActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		super.GetQuestActions( actions, context );
		actions.PushBack( ActionQuestSetPlayerSafePass( true ) );
		actions.PushBack( ActionQuestSetPlayerSafePass( false ) );
	}

	protected function ActionQuestSetPlayerSafePass( value : Bool ) : QuestSetPlayerSafePass
	{
		var action : QuestSetPlayerSafePass;
		action = new QuestSetPlayerSafePass;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties( value );
		action.AddDeviceName( m_deviceName );
		return action;
	}

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

	protected override function PushSkillCheckActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > ) : Bool
	{
		if( IsON() && !( m_playerSafePass ) )
		{
			return super.PushSkillCheckActions( outActions, context );
		}
		return false;
	}

	protected override function GetQuickHackActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var currentAction : ScriptableDeviceAction;
		currentAction = ActionQuickHackToggleON();
		currentAction.SetObjectActionID( T"DeviceAction.ToggleStateClassHack" );
		actions.PushBack( currentAction );
		currentAction = ActionSetDeviceAttitude();
		currentAction.SetObjectActionID( T"DeviceAction.OverrideAttitudeClassHack" );
		currentAction.SetInactiveWithReason( !( CanPlayerSafePass() ), "LocKey#7010" );
		actions.PushBack( currentAction );
		currentAction = ActionQuickHackExplodeExplosive();
		currentAction.SetObjectActionID( T"DeviceAction.OverloadClassHack" );
		actions.PushBack( currentAction );
		FinalizeGetQuickHackActions( actions, context );
	}

	protected override function CanCreateAnySpiderbotActions() : Bool
	{
		if( IsON() )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	protected override function GetSpiderbotActions( actions : ref< array< DeviceAction > >, const context : ref< GetActionsContext > )
	{
		actions.PushBack( ActionSpiderbotExplodeExplosiveDevice() );
		if( IsON() )
		{
			actions.PushBack( ActionSpiderbotDisarmExplosiveDevice() );
		}
	}

	protected function OnSetDeviceAttitude( evt : SetDeviceAttitude ) : EntityNotificationType
	{
		m_playerSafePass = true;
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function OnQuestSetPlayerSafePass( evt : QuestSetPlayerSafePass ) : EntityNotificationType
	{
		m_playerSafePass = ( ( Bool )evt.prop.first );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function OnSpiderbotDisarmExplosiveDevice( evt : SpiderbotDisarmExplosiveDevice ) : EntityNotificationType
	{
		SendSpiderbotToPerformAction( ActionSpiderbotDisarmExplosiveDevicePerformed(), evt.GetExecutor() );
		return EntityNotificationType.DoNotNotifyEntity;
	}

	protected function OnSpiderbotDisarmExplosiveDevicePerformed( evt : SpiderbotDisarmExplosiveDevicePerformed ) : EntityNotificationType
	{
		if( IsON() )
		{
			ExecutePSAction( ActionToggleON() );
		}
		return EntityNotificationType.DoNotNotifyEntity;
	}

}

class SpiderbotDisarmExplosiveDevice extends ActionBool
{

	public function SetProperties()
	{
		actionName = 'SpiderbotDisarmExplosiveDevice';
		prop = DeviceActionPropertyFunctions.SetUpProperty_Bool( actionName, true, 'LocKey#391', 'LocKey#391' );
	}

	public override function GetTweakDBChoiceRecord() : String
	{
		return "SpiderbotDisarmExplosiveDevice";
	}

	public constexpr static function IsAvailable( device : ScriptableDeviceComponentPS ) : Bool
	{
		return true;
	}

	public static function IsClearanceValid( clearance : Clearance ) : Bool
	{
		if( Clearance.IsInRange( clearance, DefaultActionsParametersHolder.GetInteractiveClearance() ) )
		{
			return true;
		}
		return false;
	}

	public static function IsContextValid( const context : ref< GetActionsContext > ) : Bool
	{
		if( context.requestType == gamedeviceRequestType.Remote )
		{
			return true;
		}
		return false;
	}

}

class SpiderbotDisarmExplosiveDevicePerformed extends ActionBool
{

	public function SetProperties()
	{
		actionName = 'SpiderbotDisarmExplosiveDevicePerformed';
		prop = DeviceActionPropertyFunctions.SetUpProperty_Bool( actionName, true, 'SpiderbotDisarmExplosiveDevicePerformed', 'SpiderbotDisarmExplosiveDevicePerformed' );
	}

	public constexpr static function IsAvailable( device : ScriptableDeviceComponentPS ) : Bool
	{
		return true;
	}

	public constexpr static function IsClearanceValid( clearance : Clearance ) : Bool
	{
		return true;
	}

	public constexpr static function IsContextValid( const context : ref< GetActionsContext > ) : Bool
	{
		return true;
	}

}

class QuestSetPlayerSafePass extends ActionBool
{

	public function SetProperties( value : Bool )
	{
		if( value )
		{
			actionName = 'ForceSafeForPlayer';
		}
		else
		{
			actionName = 'DisableSafeForPlayer';
		}
		prop = DeviceActionPropertyFunctions.SetUpProperty_Bool( 'QuestSetPlayerSafePass', value, 'QuestSetPlayerSafePass', 'QuestSetPlayerSafePass' );
	}

}

