importonly struct gameLightSettings
{
	import var strength : Float;
	import var intensity : Float;
	import var radius : Float;
	import var color : Color;
	import var innerAngle : Float;
	import var outerAngle : Float;
}

import class gameLightComponent extends LightComponent
{
	public import function ToggleLight( on : Bool, optional loop : Bool );
	public import function SetParameters( settings : gameLightSettings, optional inTime : Float, optional interpolationCurve : CName, optional loop : Bool );
	public import function SetStrength( strength : Float, optional inTime : Float );
	public import function SetColor( color : Color, optional inTime : Float );
	public import function SetRadius( radius : Float, optional inTime : Float );
	public import function SetAngles( innerAngle : Float, outerAngle : Float, optional inTime : Float );
	public import function SetIntensity( intensity : Float, optional inTime : Float );
	public import function IsOn() : Bool;
	public import function Destroy( optional forceDestroy : Bool, optional skipVFX : Bool );
	public import function IsDestroyed() : Bool;
	public import function IsDestructible() : Bool;
	public import function SetDestructible( isDestructible : Bool );
	public import function GetOnStrength() : Bool;
	public import function GetDefaultSettings() : gameLightSettings;
	public import function GetTurnOnTime() : Float;
	public import function GetTurnOnCurve() : CName;
	public import function GetTurnOffTime() : Float;
	public import function GetTurnOffCurve() : CName;
	public import function GetCurrentSettings() : gameLightSettings;

	protected event OnToggleLight( evt : ToggleLightEvent )
	{
		ToggleLight( evt.toggle, evt.loop );
	}

	protected event OnToggleLightByName( evt : ToggleLightByNameEvent )
	{
		var fullComponentName : String;
		fullComponentName = NameToString( GetName() );
		if( StrContains( fullComponentName, NameToString( evt.componentName ) ) )
		{
			ToggleLight( evt.toggle );
		}
	}

	protected event OnChangeLight( evt : ChangeLightEvent )
	{
		var lightSettings : gameLightSettings;
		if( IsEnabled() )
		{
			lightSettings = GetCurrentSettings();
			lightSettings.strength = evt.settings.strength;
			lightSettings.color = evt.settings.color;
			SetParameters( lightSettings, evt.time, evt.curve, evt.loop );
		}
	}

	protected event OnChangeLightByName( evt : ChangeLightByNameEvent )
	{
		var fullComponentName : String;
		var lightSettings : gameLightSettings;
		fullComponentName = NameToString( GetName() );
		if( IsEnabled() && StrContains( fullComponentName, NameToString( evt.componentName ) ) )
		{
			lightSettings = GetCurrentSettings();
			lightSettings.strength = evt.settings.strength;
			lightSettings.color = evt.settings.color;
			SetParameters( lightSettings, evt.time, evt.curve, evt.loop );
		}
	}

	private function SetupLightSettings( const inputData : ref< EditableGameLightSettings >, out outputData : gameLightSettings )
	{
		if( inputData.modifyStrength )
		{
			outputData.strength = inputData.strength;
		}
		if( inputData.modifyIntensity )
		{
			outputData.intensity = inputData.intensity;
		}
		if( inputData.modifyRadius )
		{
			outputData.radius = inputData.radius;
		}
		if( inputData.modifyColor )
		{
			outputData.color = inputData.color;
		}
		if( inputData.modifyInnerAngle )
		{
			outputData.innerAngle = inputData.innerAngle;
		}
		if( inputData.modifyOuterAngle )
		{
			outputData.outerAngle = inputData.outerAngle;
		}
	}

	protected event OnAdvanceChangeLight( evt : AdvanceChangeLightEvent )
	{
		var lightSettings : gameLightSettings;
		var fullComponentName : String;
		if( IsEnabled() )
		{
			if( IsNameValid( evt.settings.componentName ) )
			{
				fullComponentName = NameToString( GetName() );
				if( !( StrContains( fullComponentName, NameToString( evt.settings.componentName ) ) ) )
				{
					return false;
				}
			}
			lightSettings = GetCurrentSettings();
			SetupLightSettings( evt.settings, lightSettings );
			SetParameters( lightSettings, evt.time, evt.curve, evt.loop );
		}
	}

	protected event OnChangeCurveEvent( evt : ChangeCurveEvent )
	{
		var lightSettings : gameLightSettings;
		if( IsEnabled() )
		{
			lightSettings = GetCurrentSettings();
			SetColor( Color( 0, 0, 0, 0 ) );
			SetStrength( 0.0 );
			SetParameters( lightSettings, evt.time, evt.curve, evt.loop );
		}
	}

	public static function ChangeLightSettingByRefs( const lightRefs : ref< array< gameLightComponent > >, setting : ScriptLightSettings, optional inTime : Float, optional interpolationCurve : CName, optional loop : Bool )
	{
		var lightSettings : gameLightSettings;
		var i : Int32;
		for( i = 0; i < lightRefs.Size(); i += 1 )
		{
			if( !( lightRefs[ i ] ) )
			{
				continue;
			}
			lightSettings = lightRefs[ i ].GetCurrentSettings();
			lightSettings.strength = setting.strength;
			lightSettings.color = setting.color;
			lightRefs[ i ].SetParameters( lightSettings, inTime, interpolationCurve, loop );
		}
	}

	public static function ChangeAllLightsSettings( owner : weak< GameObject >, settings : ScriptLightSettings, optional time : Float, optional curve : CName, optional loop : Bool )
	{
		var evt : ChangeLightEvent;
		evt = new ChangeLightEvent;
		evt.settings = settings;
		evt.time = time;
		evt.curve = curve;
		evt.loop = loop;
		owner.QueueEvent( evt );
	}

}

class ChangeCurveEvent extends Event
{
	var time : Float;
	var curve : CName;
	var loop : Bool;
}

class ChangeLightEvent extends Event
{
	var settings : ScriptLightSettings;
	var time : Float;
	var curve : CName;
	var loop : Bool;
}

class ChangeLightByNameEvent extends ChangeLightEvent
{
	var componentName : CName;
}

struct ScriptLightSettings
{
	var strength : Float;
	var color : Color;
}

class AdvanceChangeLightEvent extends Event
{
	var settings : EditableGameLightSettings;
	var time : Float;
	var curve : CName;
	var loop : Bool;
}

struct EditableGameLightSettings
{
	instanceeditable var componentName : CName;
	[ category = "strength" ][ rangeMin = "0.f" ][ rangeMax = "1.f" ]
	instanceeditable var strength : Float;
	[ category = "strength" ]
	instanceeditable var modifyStrength : Bool;
	[ category = "intensity" ]
	instanceeditable var intensity : Float;
	[ category = "intensity" ]
	instanceeditable var modifyIntensity : Bool;
	[ category = "radius" ]
	instanceeditable var radius : Float;
	[ category = "radius" ]
	instanceeditable var modifyRadius : Bool;
	[ category = "color" ]
	instanceeditable var color : Color;
	[ category = "color" ]
	instanceeditable var modifyColor : Bool;
	[ category = "angle" ][ rangeMin = "0.f" ][ rangeMax = "180.0f" ]
	instanceeditable var innerAngle : Float;
	[ category = "angle" ]
	instanceeditable var modifyInnerAngle : Bool;
	[ category = "angle" ][ rangeMin = "0.f" ][ rangeMax = "180.0f" ]
	instanceeditable var outerAngle : Float;
	[ category = "angle" ]
	instanceeditable var modifyOuterAngle : Bool;
}

