class InitiateTrafficLightChange extends ActionBool
{

	public function SetProperties()
	{
		actionName = 'InitiateTrafficLightChange';
		prop = DeviceActionPropertyFunctions.SetUpProperty_Bool( actionName, true, actionName, actionName );
	}

}

class TrafficIntersectionManagerController extends MasterController
{

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

}

class TrafficIntersectionManagerControllerPS extends MasterControllerPS
{
	default m_deviceName = "Traffic Intersection Manager";
	default m_tweakDBRecord = T"Devices.TrafficIntersectionManager";
	default m_tweakDBDescriptionRecord = T"device_descriptions.TrafficIntersectionManager";
	private persistent var m_trafficLightStatus : worldTrafficLightColor;

	private function ActionInitiateTrafficLightChange() : InitiateTrafficLightChange
	{
		var action : InitiateTrafficLightChange;
		action = new InitiateTrafficLightChange;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOnClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.CreateActionWidgetPackage();
		return action;
	}

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

	public function OnInitiateTrafficLightChange( evt : InitiateTrafficLightChange ) : EntityNotificationType
	{
		HandleLightChangeRequest();
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

	public function HandleLightChangeRequest()
	{
		InitiateChangeLightsSequenceForEntireIntersection();
	}

	public function SetLightChangeRequest( newColor : worldTrafficLightColor )
	{
		SetLightsSequenceForEntireIntersection( newColor );
	}

	private function InitiateChangeLightsSequenceForEntireIntersection()
	{
		var devices : array< DeviceComponentPS >;
		var toggleLightAction : ToggleLight;
		var i : Int32;
		if( !( IsON() ) )
		{
			return;
		}
		ToggleLights();
		GameInstance.GetDeviceSystem( GetGameInstance() ).GetChildren( PersistentID.ExtractEntityID( GetID() ), devices );
		for( i = 0; i < devices.Size(); i += 1 )
		{
			toggleLightAction = ( ( ToggleLight )( ( ( ScriptableDeviceComponentPS )( devices[ i ] ) ).GetActionByName( 'ToggleLight' ) ) );
			if( toggleLightAction )
			{
				GetPersistencySystem().QueuePSDeviceEvent( toggleLightAction );
			}
		}
	}

	private function SetLightsSequenceForEntireIntersection( newColor : worldTrafficLightColor )
	{
		var devices : array< DeviceComponentPS >;
		var action : DeviceAction;
		var i : Int32;
		if( !( IsON() ) )
		{
			return;
		}
		m_trafficLightStatus = newColor;
		devices = GetImmediateSlaves();
		for( i = 0; i < devices.Size(); i += 1 )
		{
			if( newColor == worldTrafficLightColor.RED )
			{
				action = ( ( ScriptableDeviceComponentPS )( devices[ i ] ) ).GetActionByName( 'TrafficLightRed' );
			}
			else
			{
				action = ( ( ScriptableDeviceComponentPS )( devices[ i ] ) ).GetActionByName( 'TrafficLightGreen' );
			}
			if( action )
			{
				GetPersistencySystem().QueuePSDeviceEvent( action );
			}
		}
	}

	public const function GetDesiredTrafficLightState() : worldTrafficLightColor
	{
		return m_trafficLightStatus;
	}

	private function ToggleLights()
	{
		m_trafficLightStatus = ( ( m_trafficLightStatus == worldTrafficLightColor.RED ) ? ( worldTrafficLightColor.GREEN ) : ( worldTrafficLightColor.RED ) );
	}

}

