struct WindowBlindersData
{
	instanceeditable persistent var m_windowBlindersState : EWindowBlindersStates;
	default m_windowBlindersState = EWindowBlindersStates.Closed;
	instanceeditable persistent var m_hasOpenInteraction : Bool;
	default m_hasOpenInteraction = true;
	instanceeditable persistent var m_hasTiltInteraction : Bool;
	default m_hasTiltInteraction = false;
	instanceeditable persistent var m_hasQuickHack : Bool;
	default m_hasQuickHack = true;
}

class ToggleTiltBlinders extends ActionBool
{

	public function SetProperties( isTilted : Bool )
	{
		actionName = 'ActionTiltBlinders';
		prop = DeviceActionPropertyFunctions.SetUpProperty_Bool( 'ActionTiltBlinders', isTilted, 'LocKey#271', 'LocKey#272' );
	}

	public override function GetTweakDBChoiceRecord() : String
	{
		if( TDBID.IsValid( m_objectActionID ) )
		{
			return super.GetTweakDBChoiceRecord();
		}
		else
		{
			if( ( ( Bool )prop.first ) )
			{
				return "TiltBlindersClose";
			}
			else
			{
				return "TiltBlindersOpen";
			}
		}
	}

}

class WindowBlindersController extends ScriptableDeviceComponent
{

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

}

enum EWindowBlindersStates
{
	NonInteractive = 0,
	Open = 1,
	Closed = 2,
	Tilted = 3,
}

class WindowBlindersControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#104";
	default m_tweakDBRecord = T"Devices.WindowBlinders";
	default m_tweakDBDescriptionRecord = T"device_descriptions.WindowBlinders";
	private instanceeditable inlined var m_windowBlindersSkillChecks : EngDemoContainer;
	protected instanceeditable persistent var m_windowBlindersData : WindowBlindersData;
	protected var m_cachedState : EWindowBlindersStates;
	protected var m_alarmRaised : Bool;

	protected event OnInstantiated()
	{
		super.OnInstantiated();
		if( !( IsStringValid( m_deviceName ) ) )
		{
			m_deviceName = "Gameplay-Devices-DisplayNames-WindowBlinders";
		}
	}

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

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

	protected override function GameAttached()
	{
		TryInitializeSkillChecks();
		if( IsSkillCheckActive() )
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Closed;
		}
	}

	public override function GetDeviceIconPath() : String
	{
		return "base/gameplay/gui/brushes/devices/icon_door.widgetbrush";
	}

	public quest const function IsOpen() : Bool
	{
		if( m_windowBlindersData.m_windowBlindersState == EWindowBlindersStates.Open )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public quest const function IsTilted() : Bool
	{
		if( m_windowBlindersData.m_windowBlindersState == EWindowBlindersStates.Tilted )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public quest const function IsClosed() : Bool
	{
		if( m_windowBlindersData.m_windowBlindersState == EWindowBlindersStates.Closed )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public quest const function IsNonInteractive() : Bool
	{
		if( m_windowBlindersData.m_windowBlindersState == EWindowBlindersStates.NonInteractive )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	protected override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		if( IsNonInteractive() )
		{
			return false;
		}
		if( super.GetActions( actions, context ) )
		{
			if( m_windowBlindersData.m_hasOpenInteraction )
			{
				actions.PushBack( ActionToggleOpen() );
			}
			if( !( IsOpen() ) && m_windowBlindersData.m_hasTiltInteraction )
			{
				actions.PushBack( ActionToggleTiltBlinders() );
			}
		}
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

	protected const override function CanCreateAnyQuickHackActions() : Bool
	{
		if( IsNonInteractive() || !( m_windowBlindersData.m_hasQuickHack ) )
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	protected override function GetQuickHackActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var currentAction : ScriptableDeviceAction;
		if( IsNonInteractive() || !( m_windowBlindersData.m_hasQuickHack ) )
		{
			return;
		}
		currentAction = ActionQuickHackToggleOpen();
		currentAction.SetObjectActionID( T"DeviceAction.ToggleStateClassHack" );
		outActions.PushBack( currentAction );
		if( m_windowBlindersData.m_hasTiltInteraction )
		{
			currentAction = ActionToggleTiltBlinders();
			currentAction.SetObjectActionID( T"DeviceAction.MalfunctionClassHack" );
			currentAction.SetInactiveWithReason( !( IsOpen() ), "LocKey#7012" );
			outActions.PushBack( currentAction );
		}
		FinalizeGetQuickHackActions( outActions, context );
	}

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

	protected virtual function ActionQuickHackToggleOpen() : QuickHackToggleOpen
	{
		var action : QuickHackToggleOpen;
		action = new QuickHackToggleOpen;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetUp( this );
		action.SetProperties( IsOpen() );
		action.AddDeviceName( GetDeviceName() );
		action.CreateInteraction();
		return action;
	}

	public function OnQuickHackToggleOpen( evt : QuickHackToggleOpen ) : EntityNotificationType
	{
		ExecutePSAction( ActionToggleOpen() );
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function ActionToggleOpen() : ToggleOpen
	{
		var action : ToggleOpen;
		action = new ToggleOpen;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetUp( this );
		action.SetProperties( IsOpen() );
		action.AddDeviceName( GetDeviceName() );
		action.CreateInteraction();
		action.CreateActionWidgetPackage();
		return action;
	}

	public function OnToggleOpen( evt : ToggleOpen ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		if( IsOpen() )
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Closed;
		}
		else
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Open;
		}
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function ActionToggleTiltBlinders() : ToggleTiltBlinders
	{
		var action : ToggleTiltBlinders;
		action = new ToggleTiltBlinders;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetUp( this );
		action.SetProperties( IsTilted() );
		action.AddDeviceName( GetDeviceName() );
		action.CreateInteraction();
		action.CreateActionWidgetPackage();
		return action;
	}

	public function OnToggleTiltBlinders( evt : ToggleTiltBlinders ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		if( !( IsTilted() ) )
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Tilted;
		}
		else
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Closed;
		}
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnActionEngineering( evt : ActionEngineering ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		super.OnActionEngineering( evt );
		if( IsOpen() )
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Closed;
		}
		else
		{
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Open;
		}
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnActionDemolition( evt : ActionDemolition ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		super.OnActionDemolition( evt );
		m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Open;
		ForceDisableDevice();
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnSecuritySystemOutput( evt : SecuritySystemOutput ) : EntityNotificationType
	{
		super.OnSecuritySystemOutput( evt );
		if( evt.GetCachedSecurityState() == ESecuritySystemState.COMBAT )
		{
			m_alarmRaised = true;
			m_cachedState = m_windowBlindersData.m_windowBlindersState;
			if( IsOpen() )
			{
				ExecutePSAction( ActionToggleOpen(), this );
			}
			else if( IsTilted() )
			{
				ExecutePSAction( ActionToggleTiltBlinders(), this );
			}
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.NonInteractive;
		}
		else if( evt.GetCachedSecurityState() == ESecuritySystemState.SAFE && m_alarmRaised )
		{
			m_alarmRaised = false;
			m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Closed;
			if( m_cachedState == EWindowBlindersStates.Tilted )
			{
				ExecutePSAction( ActionToggleTiltBlinders(), this );
			}
			else if( m_cachedState == EWindowBlindersStates.Open )
			{
				ExecutePSAction( ActionToggleOpen(), this );
			}
		}
		return EntityNotificationType.DoNotNotifyEntity;
	}

	private function ActionQuestForceOpen() : QuestForceOpen
	{
		var action : QuestForceOpen;
		action = new QuestForceOpen;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public function OnQuestForceOpen( evt : QuestForceOpen ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Open;
		Notify( notifier, evt );
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

	private function ActionQuestForceClose() : QuestForceClose
	{
		var action : QuestForceClose;
		action = new QuestForceClose;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties( 1.0 );
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public function OnQuestForceClose( evt : QuestForceClose ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		m_windowBlindersData.m_windowBlindersState = EWindowBlindersStates.Closed;
		Notify( notifier, evt );
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

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

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

}

