class SpiderbotActivateActivator extends ActionBool
{

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

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

	public static function IsAvailable( device : ScriptableDeviceComponentPS ) : Bool
	{
		if( !( AIActionHelper.CheckFlatheadStatPoolRequirements( device.GetGameInstance(), "DeviceAction" ) ) )
		{
			return false;
		}
		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 ActivatorController extends MasterController
{

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

}

class ActivatorControllerPS extends MasterControllerPS
{
	default m_deviceName = "LocKey#42164";
	default m_tweakDBRecord = T"Devices.Activator";
	default m_tweakDBDescriptionRecord = T"device_descriptions.Activator";
	default m_revealDevicesGrid = true;
	default m_drawGridLink = false;
	[ category = "AvailableInteractions" ]
	private instanceeditable var m_hasSpiderbotInteraction : Bool;
	default m_hasSpiderbotInteraction = false;
	[ category = "AvailableInteractions" ]
	private instanceeditable var m_spiderbotInteractionLocationOverride : NodeRef;
	[ category = "AvailableInteractions" ]
	private instanceeditable var m_hasSimpleInteraction : Bool;
	default m_hasSimpleInteraction = true;
	[ category = "InteractionNamesSetup" ][ customEditor = "TweakDBGroupInheritance;Interactions.InteractionChoice;Interactions.MountChoice" ]
	private instanceeditable var m_alternativeInteractionName : TweakDBID;
	[ category = "InteractionNamesSetup" ][ customEditor = "TweakDBGroupInheritance;Interactions.InteractionChoice;Interactions.MountChoice" ]
	private instanceeditable var m_alternativeSpiderbotInteractionName : TweakDBID;
	[ category = "InteractionNamesSetup" ][ customEditor = "TweakDBGroupInheritance;Interactions.InteractionChoice;Interactions.MountChoice" ]
	private instanceeditable var m_alternativeQuickHackName : TweakDBID;
	private instanceeditable inlined var m_activatorSkillChecks : GenericContainer;
	private var m_alternativeInteractionString : String;
	default m_alternativeInteractionString = "ToggleActivate";

	protected override function GetSkillCheckContainerForSetup() : BaseSkillCheckContainer
	{
		return m_activatorSkillChecks;
	}

	protected override function GameAttached() {}

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( outActions, context );
		if( m_hasSimpleInteraction )
		{
			outActions.PushBack( ActionToggleActivation( m_alternativeInteractionName ) );
		}
		SetActionIllegality( outActions, m_illegalActions.regularActions );
		return true;
	}

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

	protected override function GetQuickHackActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var currentAction : ScriptableDeviceAction;
		currentAction = ActionToggleActivation( m_alternativeQuickHackName );
		currentAction.SetObjectActionID( T"DeviceAction.ToggleStateClassHack" );
		actions.PushBack( currentAction );
		FinalizeGetQuickHackActions( actions, context );
	}

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

	protected override function GetSpiderbotActions( actions : ref< array< DeviceAction > >, const context : ref< GetActionsContext > )
	{
		if( ( m_hasSpiderbotInteraction && IsPowered() ) && GameInstance.GetStatsSystem( GetGameInstance() ).GetStatBoolValue( GameInstance.GetPlayerSystem( GetGameInstance() ).GetLocalPlayerMainGameObject().GetEntityID(), gamedataStatType.HasSpiderBotControl ) )
		{
			if( AIActionHelper.CheckFlatheadStatPoolRequirements( GetGameInstance(), "DeviceAction" ) )
			{
				actions.PushBack( ActionSpiderbotActivateActivator( m_alternativeSpiderbotInteractionName ) );
			}
		}
	}

	public override function GetQuestActionByName( actionName : CName ) : DeviceAction
	{
		var action : DeviceAction;
		action = super.GetQuestActionByName( actionName );
		if( action == NULL )
		{
			switch( actionName )
			{
				case 'ForceActivate':
					action = ActionQuestForceActivate();
				break;
			}
		}
		return action;
	}

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

	public function GetSpiderbotInteractionLocationOverride() : NodeRef
	{
		return m_spiderbotInteractionLocationOverride;
	}

	public function ActivateConnectedDevices()
	{
		var i : Int32;
		var activateAction : ActivateDevice;
		var devices : array< DeviceComponentPS >;
		devices = GetImmediateSlaves();
		m_hasSpiderbotInteraction = false;
		for( i = 0; i < devices.Size(); i += 1 )
		{
			if( ( ( VentilationAreaControllerPS )( devices[ i ] ) ) )
			{
				ExtractActionFromSlave( devices[ i ], 'ActivateDevice', ( ( DeviceAction )( activateAction ) ) );
			}
			else if( ( ( AOEAreaControllerPS )( devices[ i ] ) ) )
			{
				ExtractActionFromSlave( devices[ i ], 'ActivateDevice', ( ( DeviceAction )( activateAction ) ) );
			}
			else
			{
				activateAction = ActionActivateDevice();
			}
			if( activateAction )
			{
				ExecutePSAction( activateAction, devices[ i ] );
			}
		}
	}

	protected override function ActionEngineering( const context : ref< GetActionsContext > ) : ActionEngineering
	{
		var action : ActionEngineering;
		var additionalActions : array< DeviceAction >;
		action = super.ActionEngineering( context );
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetAvailableOnUnpowered();
		action.CreateInteraction( context.processInitiatorObject, additionalActions );
		return action;
	}

	protected override function ActionDemolition( const context : ref< GetActionsContext > ) : ActionDemolition
	{
		var action : ActionDemolition;
		var additionalActions : array< DeviceAction >;
		additionalActions.PushBack( ActionToggleActivation() );
		action = super.ActionDemolition( context );
		action.SetAvailableOnUnpowered();
		action.CreateInteraction( context.processInitiatorObject, additionalActions );
		return action;
	}

	public override function OnActionDemolition( evt : ActionDemolition ) : EntityNotificationType
	{
		if( !( evt.WasPassed() ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		super.OnActionDemolition( evt );
		if( evt.IsCompleted() )
		{
		}
		return EntityNotificationType.DoNotNotifyEntity;
	}

	public override function OnActionEngineering( evt : ActionEngineering ) : EntityNotificationType
	{
		if( !( evt.WasPassed() ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		super.OnActionEngineering( evt );
		if( evt.IsCompleted() )
		{
			DisableDevice();
			ActivateConnectedDevices();
			return EntityNotificationType.SendThisEventToEntity;
		}
		return EntityNotificationType.DoNotNotifyEntity;
	}

	protected override function ResolveActionHackingCompleted( evt : ActionHacking )
	{
		super.ResolveActionHackingCompleted( evt );
		if( evt.GetAttributeCheckType() != EDeviceChallengeSkill.Invalid )
		{
			DisableDevice();
			ActivateConnectedDevices();
		}
	}

	protected function ActionToggleActivation( interactionTDBID : TweakDBID ) : ToggleActivation
	{
		var action : ToggleActivation;
		action = new ToggleActivation;
		action.SetUp( this );
		action.SetProperties( m_deviceState );
		action.AddDeviceName( m_deviceName );
		action.CreateInteraction( , m_alternativeInteractionString, interactionTDBID );
		return action;
	}

	protected function ActionSpiderbotActivateActivator( interactionTDBID : TweakDBID ) : SpiderbotActivateActivator
	{
		var action : SpiderbotActivateActivator;
		action = new SpiderbotActivateActivator;
		action.SetUp( this );
		action.AddDeviceName( m_deviceName );
		action.CreateInteraction( , , interactionTDBID );
		return action;
	}

	public function OnSpiderbotActivateActivator( evt : SpiderbotActivateActivator ) : EntityNotificationType
	{
		m_isSpiderbotInteractionOrdered = true;
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnDisassembleDevice( evt : DisassembleDevice ) : EntityNotificationType
	{
		DisableDevice();
		return super.OnDisassembleDevice( evt );
	}

	public override function OnToggleActivation( evt : ToggleActivation ) : EntityNotificationType
	{
		UseNotifier( evt );
		if( IsEnabled() )
		{
			DisableDevice();
			ActivateConnectedDevices();
			m_hasSpiderbotInteraction = false;
			return EntityNotificationType.SendThisEventToEntity;
		}
		else
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
	}

	public export override function OnQuestForceActivate( evt : QuestForceActivate ) : EntityNotificationType
	{
		UseNotifier( evt );
		if( IsEnabled() )
		{
			DisableDevice();
			ActivateConnectedDevices();
			m_hasSpiderbotInteraction = false;
			return EntityNotificationType.SendThisEventToEntity;
		}
		else
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
	}

}

