class OverloadDevice extends ActionBool
{
	protected var m_killDelay : Float;
	default m_killDelay = 1.0f;

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

	public function GetKillDelay() : Float
	{
		return m_killDelay;
	}

	public function SetKillDelay( delay : Float )
	{
		m_killDelay = delay;
	}

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

}

class SendSpiderbotToOverloadDevice extends ActionBool
{

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

}

class SendSpiderbotToTogglePower extends ActionBool
{

	public function SetProperties( status : EDeviceStatus )
	{
		var unpowered : Bool;
		actionName = 'TogglePower';
		if( status == EDeviceStatus.UNPOWERED )
		{
			unpowered = true;
		}
		prop = DeviceActionPropertyFunctions.SetUpProperty_Bool( 'TogglePower', unpowered, 'LocKey#258', 'LocKey#257' );
	}

	public override function GetTweakDBChoiceRecord() : String
	{
		if( !( ( ( Bool )prop.first ) ) )
		{
			return "Unpower";
		}
		else
		{
			return "Power";
		}
	}

}

class FuseBoxController extends MasterController
{

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

}

class FuseBoxControllerPS extends MasterControllerPS
{
	default m_deviceName = "LocKey#2013";
	default m_tweakDBRecord = T"Devices.FuseBox";
	default m_tweakDBDescriptionRecord = T"device_descriptions.FuseBox";
	private instanceeditable inlined var m_fuseBoxSkillChecks : EngineeringContainer;
	[ tooltip = "Generator has spiderbot action to overload given device" ]
	private editable var m_isGenerator : Bool;
	private persistent var m_isOverloaded : Bool;

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

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

	protected override function GameAttached() {}

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

	public const function IsGenerator() : Bool
	{
		return m_isGenerator;
	}

	public const override function GetExpectedSlaveState() : EDeviceStatus
	{
		if( !( IsON() ) )
		{
			return EDeviceStatus.UNPOWERED;
		}
		return EDeviceStatus.INVALID;
	}

	protected const override function GetClearance() : Clearance
	{
		return Clearance.CreateClearance( 5, 5 );
	}

	public override function GetWidgetTypeName() : CName
	{
		return 'FuseBoxWidget';
	}

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

	public override function OnToggleON( evt : ToggleON ) : EntityNotificationType
	{
		super.OnToggleON( evt );
		RefreshSlaves_Event();
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected override function OnSetDeviceOFF( evt : SetDeviceOFF ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		if( IsDisabled() )
		{
			return SendActionFailedEvent( evt, evt.GetRequesterID(), "Unpowered or Disabled" );
		}
		SetDeviceState( EDeviceStatus.OFF );
		RefreshSlaves_Event();
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected override function OnSetDeviceON( evt : SetDeviceON ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		if( IsDisabled() )
		{
			return SendActionFailedEvent( evt, evt.GetRequesterID(), "Unpowered or Disabled" );
		}
		SetDeviceState( EDeviceStatus.ON );
		RefreshSlaves_Event();
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	private function RefreshSlaves( devices : array< DeviceComponentPS > )
	{
		var i : Int32;
		var action : ScriptableDeviceAction;
		var device : ScriptableDeviceComponentPS;
		devices = GetImmediateSlaves();
		for( i = 0; i < devices.Size(); i = i + 1 )
		{
			if( devices[ i ] == this )
			{
				continue;
			}
			device = ( ( ScriptableDeviceComponentPS )( devices[ i ] ) );
			if( device )
			{
				if( IsON() )
				{
					action = ( ( ScriptableDeviceAction )( device.ActionSetDevicePowered() ) );
				}
				else
				{
					action = ( ( ScriptableDeviceAction )( device.ActionSetDeviceUnpowered() ) );
				}
			}
			if( action )
			{
				ExecutePSAction( action, device );
			}
		}
	}

	protected export override function OnRefreshSlavesEvent( evt : RefreshSlavesEvent ) : EntityNotificationType
	{
		RefreshSlaves( evt.devices );
		return EntityNotificationType.DoNotNotifyEntity;
	}

	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 );
		action.CreateActionWidgetPackage();
		action.CreateInteraction();
		return action;
	}

	protected virtual function ActionSendSpiderbotToTogglePower() : SendSpiderbotToTogglePower
	{
		var action : SendSpiderbotToTogglePower;
		action = new SendSpiderbotToTogglePower;
		action.clearanceLevel = DefaultActionsParametersHolder.GetTogglePowerClearance();
		action.SetUp( this );
		action.SetProperties( m_deviceState );
		action.AddDeviceName( m_deviceName );
		action.CreateActionWidgetPackage();
		action.CreateInteraction();
		return action;
	}

	protected function OnSendSpiderbotToTogglePower( evt : SendSpiderbotToTogglePower ) : EntityNotificationType
	{
		SendSpiderbotToPerformAction( ActionToggleON(), evt.GetExecutor() );
		UseNotifier( evt );
		return EntityNotificationType.DoNotNotifyEntity;
	}

	protected function ActionSendSpiderbotToOverloadDevice() : SendSpiderbotToOverloadDevice
	{
		var action : SendSpiderbotToOverloadDevice;
		action = new SendSpiderbotToOverloadDevice;
		action.clearanceLevel = DefaultActionsParametersHolder.GetInteractiveClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( GetDeviceName() );
		action.CreateInteraction();
		return action;
	}

	protected function OnSendSpiderbotToOverloadDevice( evt : SendSpiderbotToOverloadDevice ) : EntityNotificationType
	{
		SendSpiderbotToPerformAction( ActionOverloadDevice(), evt.GetExecutor() );
		UseNotifier( evt );
		return EntityNotificationType.DoNotNotifyEntity;
	}

	protected override function ActionOverloadDevice() : OverloadDevice
	{
		var action : OverloadDevice;
		action = new OverloadDevice;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOnClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.SetObjectActionID( T"DeviceAction.OverloadClassHack" );
		action.CreateInteraction();
		action.SetDurationValue( GetDistractionDuration( action ) );
		return action;
	}

	protected override function OnOverloadDevice( evt : OverloadDevice ) : EntityNotificationType
	{
		if( evt.IsStarted() )
		{
			m_isOverloaded = true;
			evt.SetCanTriggerStim( true );
			ExecutePSActionWithDelay( evt, this, evt.GetDurationValue() );
		}
		else
		{
			evt.SetCanTriggerStim( false );
			ExecutePSAction( ActionToggleActivation(), this );
		}
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function IsOverloaded() : Bool
	{
		return m_isOverloaded;
	}

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		if( m_isGenerator )
		{
			return false;
		}
		if( !( super.GetActions( outActions, context ) ) )
		{
			return false;
		}
		if( ToggleON.IsDefaultConditionMet( this, context ) )
		{
			outActions.PushBack( ActionToggleON() );
		}
		SetActionIllegality( outActions, m_illegalActions.regularActions );
		return true;
	}

	protected override function CanCreateAnySpiderbotActions() : Bool
	{
		if( !( m_isGenerator ) || m_isOverloaded )
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	protected override function GetSpiderbotActions( actions : ref< array< DeviceAction > >, const context : ref< GetActionsContext > )
	{
		if( !( m_isGenerator ) || m_isOverloaded )
		{
			return;
		}
		actions.PushBack( ActionSendSpiderbotToOverloadDevice() );
		if( HasAnySlave() )
		{
			actions.PushBack( ActionSendSpiderbotToTogglePower() );
		}
	}

	protected const override function CanCreateAnyQuickHackActions() : Bool
	{
		if( !( m_isGenerator ) || m_isOverloaded )
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	protected override function GetQuickHackActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var currentAction : ScriptableDeviceAction;
		if( !( m_isGenerator ) || m_isOverloaded )
		{
			return;
		}
		currentAction = ActionQuickHackDistraction();
		currentAction.SetObjectActionID( T"DeviceAction.MalfunctionClassHack" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		currentAction.SetInactiveWithReason( QuickHackDistraction.IsDefaultConditionMet( this, context ), "LocKey#7003" );
		actions.PushBack( currentAction );
		currentAction = ActionOverloadDevice();
		currentAction.SetObjectActionID( T"DeviceAction.OverloadClassHack" );
		currentAction.SetInactiveWithReason( !( m_isOverloaded ), "LocKey#7013" );
		actions.PushBack( currentAction );
		if( HasAnySlave() )
		{
			currentAction = ActionQuickHackToggleON();
			currentAction.SetObjectActionID( T"DeviceAction.ToggleStateClassHack" );
			currentAction.SetInactiveWithReason( QuickHackToggleON.IsDefaultConditionMet( ( ( FuseBoxControllerPS )( this ) ), context ), "LocKey#7003" );
			actions.PushBack( currentAction );
		}
		if( IsGlitching() || IsDistracting() )
		{
			SetActionsInactiveAll( actions, "LocKey#7004" );
		}
		FinalizeGetQuickHackActions( actions, context );
	}

	protected override function GetDeviceIconTweakDBID() : TweakDBID
	{
		return T"DeviceIcons.GeneratorDeviceIcon";
	}

	protected override function GetBackgroundTextureTweakDBID() : TweakDBID
	{
		return T"DeviceIcons.GeneratorDeviceBackground";
	}

}

