class RoadBlockController extends ScriptableDeviceComponent
{

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

}

class RoadBlockControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#126";
	default m_tweakDBRecord = T"Devices.RoadBlock";
	default m_tweakDBDescriptionRecord = T"device_descriptions.RoadBlock";
	protected instanceeditable persistent var m_isBlocking : Bool;
	protected editable var m_negateAnimState : Bool;
	[ customEditor = "TweakDBGroupInheritance;Interactions.InteractionChoice;Interactions.MountChoice" ]
	protected instanceeditable var m_nameForBlocking : TweakDBID;
	[ customEditor = "TweakDBGroupInheritance;Interactions.InteractionChoice;Interactions.MountChoice" ]
	protected instanceeditable var m_nameForUnblocking : TweakDBID;

	public quest const function IsBlocking() : Bool
	{
		return m_isBlocking;
	}

	public quest const function IsNotBlocking() : Bool
	{
		return !( IsBlocking() );
	}

	private function SetIsBlockingState( isBlocking : Bool )
	{
		m_isBlocking = isBlocking;
		NotifyParents();
	}

	public const function NegateAnim() : Bool
	{
		return m_negateAnimState;
	}

	protected override function GameAttached()
	{
		if( IsBlocking() )
		{
			m_activationState = EActivationState.ACTIVATED;
		}
		else
		{
			m_activationState = EActivationState.DEACTIVATED;
		}
	}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( actions, context );
		if( ToggleBlockade.IsDefaultConditionMet( ( ( RoadBlockControllerPS )( this ) ), context ) )
		{
			if( context.requestType == gamedeviceRequestType.External )
			{
				actions.PushBack( ActionToggleBlockade() );
			}
		}
		SetActionIllegality( actions, 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 = ActionQuickHackToggleBlockade();
		currentAction.SetObjectActionID( T"DeviceAction.ToggleStateClassHack" );
		currentAction.SetInactiveWithReason( ToggleBlockade.IsDefaultConditionMet( ( ( RoadBlockControllerPS )( this ) ), context ), "LocKey#7003" );
		actions.PushBack( currentAction );
		FinalizeGetQuickHackActions( actions, context );
	}

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

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

	protected virtual function ActionToggleBlockade() : ToggleBlockade
	{
		var action : ToggleBlockade;
		action = new ToggleBlockade;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetUp( this );
		action.SetProperties( IsBlocking(), m_nameForBlocking, m_nameForUnblocking );
		action.AddDeviceName( GetDeviceName() );
		action.CreateInteraction();
		action.CreateActionWidgetPackage();
		return action;
	}

	protected virtual function ActionQuickHackToggleBlockade() : QuickHackToggleBlockade
	{
		var action : QuickHackToggleBlockade;
		action = new QuickHackToggleBlockade;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetUp( this );
		action.SetProperties( IsBlocking(), m_nameForBlocking, m_nameForUnblocking );
		action.AddDeviceName( GetDeviceName() );
		if( IsBlocking() )
		{
			action.CreateInteraction( , , m_nameForBlocking );
		}
		else
		{
			action.CreateInteraction( , , m_nameForUnblocking );
		}
		action.CreateActionWidgetPackage();
		return action;
	}

	protected function ActionQuestForceRoadBlockadeActivate() : QuestForceRoadBlockadeActivate
	{
		var action : QuestForceRoadBlockadeActivate;
		action = new QuestForceRoadBlockadeActivate;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	protected function ActionQuestForceRoadBlockadeDeactivate() : QuestForceRoadBlockadeDeactivate
	{
		var action : QuestForceRoadBlockadeDeactivate;
		action = new QuestForceRoadBlockadeDeactivate;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public function OnToggleBlockade( evt : ToggleBlockade ) : EntityNotificationType
	{
		SetIsBlockingState( !( IsBlocking() ) );
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function OnQuickHackToggleBlockadeQuickHackToggleBlockade( evt : QuickHackToggleBlockade ) : EntityNotificationType
	{
		SetIsBlockingState( !( IsBlocking() ) );
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public virtual function OnQuestForceRoadBlockadeActivate( evt : QuestForceRoadBlockadeActivate ) : EntityNotificationType
	{
		SetIsBlockingState( true );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public virtual function OnQuestForceRoadBlockadeDeactivate( evt : QuestForceRoadBlockadeDeactivate ) : EntityNotificationType
	{
		SetIsBlockingState( false );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected override function OnActivateDevice( evt : ActivateDevice ) : EntityNotificationType
	{
		if( m_activationState != EActivationState.ACTIVATED && IsON() )
		{
			super.OnActivateDevice( evt );
			SetIsBlockingState( true );
			return EntityNotificationType.SendThisEventToEntity;
		}
		else
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
	}

	protected override function OnDeactivateDevice( evt : DeactivateDevice ) : EntityNotificationType
	{
		if( m_activationState != EActivationState.DEACTIVATED && IsON() )
		{
			super.OnDeactivateDevice( evt );
			SetIsBlockingState( false );
			return EntityNotificationType.SendThisEventToEntity;
		}
		else
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
	}

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

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

}

