class AlarmLight extends BasicDistractionDevice
{
	default m_controllerTypeName = 'AlarmLightController';
	protected var m_isGlitching : Bool;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_controller = ( ( AlarmLightController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		UpdateLights();
	}

	protected event OnSecurityAlarmBreachResponse( evt : SecurityAlarmBreachResponse )
	{
		UpdateLights();
	}

	protected event OnQuestForceSecuritySystemSafe( evt : QuestForceSecuritySystemSafe )
	{
		UpdateLights();
	}

	protected event OnQuestForceSecuritySystemArmed( evt : QuestForceSecuritySystemArmed )
	{
		UpdateLights();
	}

	protected event OnSecuritySystemOutput( evt : SecuritySystemOutput )
	{
		UpdateLights();
	}

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		if( GetDevicePS().GetAlarmState() != ESecuritySystemState.COMBAT )
		{
			m_isGlitching = true;
			TurnOnLights();
			StartBlinking();
		}
	}

	protected override function StopGlitching()
	{
		m_isGlitching = false;
		StopBlinking();
		UpdateLights();
	}

	protected function UpdateLights()
	{
		if( !( m_isGlitching ) )
		{
			if( GetDevicePS().GetAlarmState() == ESecuritySystemState.COMBAT )
			{
				TurnOnLights();
				SendStim();
			}
			else
			{
				TurnOffLights();
				StopStim();
			}
		}
	}

	private function TurnOnLights()
	{
		var evt : ToggleLightEvent;
		evt = new ToggleLightEvent;
		evt.toggle = true;
		QueueEvent( evt );
	}

	private function TurnOffLights()
	{
		var evt : ToggleLightEvent;
		evt = new ToggleLightEvent;
		evt.toggle = false;
		QueueEvent( evt );
	}

	protected function StartBlinking()
	{
		var evt : ChangeCurveEvent;
		evt = new ChangeCurveEvent;
		evt.time = 3.0;
		evt.curve = 'BrokenLamp3';
		evt.loop = true;
		QueueEvent( evt );
	}

	protected function StopBlinking()
	{
		var evt : ChangeCurveEvent;
		evt = new ChangeCurveEvent;
		evt.time = 3.0;
		evt.curve = 'alarm';
		evt.loop = true;
		QueueEvent( evt );
	}

	protected function SendStim()
	{
		var broadcaster : StimBroadcasterComponent;
		broadcaster = GetStimBroadcasterComponent();
		if( broadcaster )
		{
			broadcaster.SetSingleActiveStimuli( this, gamedataStimType.SilentAlarm, , 10.0 );
		}
	}

	protected function StopStim()
	{
		var broadcaster : StimBroadcasterComponent;
		broadcaster = GetStimBroadcasterComponent();
		if( broadcaster )
		{
			broadcaster.RemoveActiveStimuliByName( this, gamedataStimType.SilentAlarm );
		}
	}

	protected override function CutPower()
	{
		TurnOffLights();
		StopStim();
	}

	protected override function TurnOnDevice()
	{
		UpdateLights();
	}

	protected override function TurnOffDevice()
	{
		TurnOffLights();
		StopStim();
	}

	private const override function GetController() : AlarmLightController
	{
		return ( ( AlarmLightController )( m_controller ) );
	}

	public const override function GetDevicePS() : AlarmLightControllerPS
	{
		return GetController().GetPS();
	}

	public const override function DeterminGameplayRole() : EGameplayRole
	{
		return EGameplayRole.Distract;
	}

}

