class ElectricLightController extends ScriptableDeviceComponent
{

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

}

class ElectricLightControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#42165";
	default m_tweakDBRecord = T"Devices.ElectricLight";
	default m_tweakDBDescriptionRecord = T"device_descriptions.ElectricLight";
	private persistent var m_isConnectedToCLS : Bool;
	private var m_wasCLSInitTriggered : Bool;

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

	protected override function GameAttached() {}

	protected override function LogicReady()
	{
		super.LogicReady();
		m_isAttachedToGame = true;
	}

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

	public override function EvaluateDeviceState()
	{
		var fuse : FuseControllerPS;
		if( IsDisabled() )
		{
			return;
		}
		if( m_isConnectedToCLS )
		{
			fuse = GetCLSFuse();
			if( fuse )
			{
				SetDeviceState( fuse.GetDeviceState() );
			}
		}
		else if( m_wasCLSInitTriggered || !( InitializeCLS( true ) ) )
		{
			super.EvaluateDeviceState();
		}
	}

	public const override function IsConnectedToCLS() : Bool
	{
		if( m_isConnectedToCLS )
		{
			return true;
		}
		else
		{
			return super.IsConnectedToCLS();
		}
	}

	private function InitializeCLS( optional setStateInstant : Bool ) : Bool
	{
		var fuse : FuseControllerPS;
		var evt : InitializeCLSEvent;
		fuse = GetCLSFuse();
		if( fuse )
		{
			if( fuse.IsUnpowered() )
			{
				if( setStateInstant )
				{
					SetDeviceState( EDeviceStatus.UNPOWERED );
				}
				else
				{
					UpdateStateOnCLS( EDeviceStatus.UNPOWERED );
				}
			}
			else
			{
				if( fuse.IsCLSInitialized() )
				{
					if( setStateInstant )
					{
						SetDeviceState( fuse.GetDeviceState() );
					}
					else
					{
						UpdateStateOnCLS( fuse.GetDeviceState() );
					}
				}
				else
				{
					evt = new InitializeCLSEvent;
					GetPersistencySystem().QueuePSEvent( fuse.GetID(), fuse.GetClassName(), evt );
					if( setStateInstant )
					{
						SetDeviceState( fuse.GetDeviceStateByCLS() );
					}
				}
			}
			m_isConnectedToCLS = true;
		}
		m_wasCLSInitTriggered = true;
		return m_isConnectedToCLS;
	}

	private function GetCLSFuse() : FuseControllerPS
	{
		var i : Int32;
		var masters : array< DeviceComponentPS >;
		var fuse : FuseControllerPS;
		GetParents( masters );
		for( i = 0; i < masters.Size(); i += 1 )
		{
			fuse = ( ( FuseControllerPS )( masters[ i ] ) );
			if( fuse )
			{
				if( fuse.IsConnectedToCLS() )
				{
					return fuse;
				}
			}
		}
		return NULL;
	}

	private function UpdateStateOnCLS( state : EDeviceStatus )
	{
		if( state == EDeviceStatus.ON )
		{
			if( IsUnpowered() )
			{
				ExecutePSAction( ActionSetDevicePowered(), this );
			}
			ExecutePSAction( ActionSetDeviceON(), this );
		}
		else if( state == EDeviceStatus.OFF )
		{
			ExecutePSAction( ActionSetDeviceOFF(), this );
		}
		else if( state == EDeviceStatus.UNPOWERED )
		{
			ExecutePSAction( ActionSetDeviceUnpowered(), this );
		}
	}

}

