class DestructibleMasterLight extends DestructibleMasterDevice
{
	default m_controllerTypeName = 'DestructibleMasterLightController';
	private var m_lightComponents : array< gameLightComponent >;
	private const instanceeditable var m_lightDefinitions : array< LightPreset >;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		var i : Int32;
		for( i = 0; i < m_lightDefinitions.Size(); i += 1 )
		{
			if( IsNameValid( m_lightDefinitions[ i ].lightSourcesName ) )
			{
				EntityRequestComponentsInterface.RequestComponent( ri, m_lightDefinitions[ i ].lightSourcesName, 'gameLightComponent', true );
			}
		}
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		var i : Int32;
		for( i = 0; i < m_lightDefinitions.Size(); i += 1 )
		{
			m_lightComponents.PushBack( ( ( gameLightComponent )( EntityResolveComponentsInterface.GetComponent( ri, m_lightDefinitions[ i ].lightSourcesName ) ) ) );
			ApplyPreset( m_lightComponents[ i ], m_lightDefinitions[ i ].preset );
		}
		super.OnTakeControl( ri );
		m_controller = ( ( DestructibleMasterLightController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected event OnPhysicalDestructionEvent( evt : PhysicalDestructionEvent )
	{
		super.OnPhysicalDestructionEvent( evt );
		GetDevicePS().ForceDisableDevice();
	}

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

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

	protected const override function ShouldRegisterToHUD() : Bool
	{
		if( m_forceRegisterInHudManager )
		{
			return true;
		}
		return false;
	}

	private function ApplyPreset( light : gameLightComponent, preset : TweakDBID )
	{
		var envLightRecord : EnvLight_Record;
		var presetColor : Color;
		var lightSettings : gameLightSettings;
		envLightRecord = TDB.GetEnvLightRecord( preset );
		light.SetTemperature( envLightRecord.Temperature() );
		lightSettings = light.GetDefaultSettings();
		lightSettings.intensity = envLightRecord.Intensity();
		lightSettings.radius = envLightRecord.Radius();
		CreateColorFromIntArray( envLightRecord.Color(), presetColor );
		lightSettings.color = presetColor;
		light.SetParameters( lightSettings );
	}

	private function CreateColorFromIntArray( const ints : ref< array< Int32 > >, out color : Color ) : Bool
	{
		if( ints.Size() != 3 )
		{
			if( !( IsFinal() ) )
			{
			}
			color = Color( 255, 0, 0, 255 );
			return false;
		}
		else
		{
			color.Red = ( ( Uint8 )( ints[ 0 ] ) );
			color.Green = ( ( Uint8 )( ints[ 1 ] ) );
			color.Blue = ( ( Uint8 )( ints[ 2 ] ) );
			color.Alpha = ( ( Uint8 )( 255 ) );
			return true;
		}
	}

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

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

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

	private function TurnOnLights()
	{
		var evt : ToggleLightEvent;
		if( GetDevicePS().GetDurabilityState() == EDeviceDurabilityState.BROKEN )
		{
			return;
		}
		if( GetDevicePS().GetDurabilityType() == EDeviceDurabilityType.INDESTRUCTIBLE )
		{
			GameObjectEffectHelper.StartEffectEvent( this, 'light_on_destr' );
		}
		evt = new ToggleLightEvent;
		evt.toggle = true;
		QueueEvent( evt );
	}

	private function TurnOffLights()
	{
		var evt : ToggleLightEvent;
		evt = new ToggleLightEvent;
		if( GetDevicePS().GetDurabilityType() == EDeviceDurabilityType.INDESTRUCTIBLE )
		{
			GameObjectEffectHelper.StopEffectEvent( this, 'light_on_destr' );
		}
		evt.toggle = false;
		QueueEvent( evt );
	}

	protected event OnHitEvent( hit : gameHitEvent )
	{
		if( AttackData.IsRangedOrDirect( hit.attackData.GetAttackType() ) || AttackData.IsExplosion( hit.attackData.GetAttackType() ) )
		{
			ReactToHit( hit );
		}
	}

	protected override function ReactToHit( hit : gameHitEvent )
	{
		if( GetDevicePS().GetDurabilityType() == EDeviceDurabilityType.INDESTRUCTIBLE )
		{
			GameObjectEffectHelper.StartEffectEvent( this, 'light_on_destr' );
		}
		if( GetDevicePS().GetDurabilityType() == EDeviceDurabilityType.DESTRUCTIBLE )
		{
			GetDevicePS().SetDurabilityState( EDeviceDurabilityState.BROKEN );
			TurnOffDevice();
		}
	}

	protected event OnEMPHitEvent( evt : EMPHitEvent )
	{
		var empEnded : EMPEnded;
		if( IsActive() )
		{
			GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.Start, 'emp_hit' );
			ExecuteAction( GetDevicePS().ActionSetDeviceUnpowered() );
			empEnded = new EMPEnded;
			GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, empEnded, evt.lifetime );
		}
	}

	protected event OnEMPEnded( evt : EMPEnded )
	{
		GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.BreakLoop, 'emp_hit' );
		ExecuteAction( GetDevicePS().ActionSetDevicePowered() );
	}

}

