struct vehicleVisualCustomizationPersistentData
{
	persistent var ID : TweakDBID;
	persistent var visualCustomizationData : vehicleVisualModdingDefinition;
}

class StoreVisualCustomizationDataForIDEvent extends Event
{
	var ID : TweakDBID;
	var definition : vehicleVisualModdingDefinition;
}

class vehicleVisualCustomizationComponent extends GameComponent
{

	public function RetriveVisualCustomizationDefinitionForID( ID : TweakDBID ) : vehicleVisualModdingDefinition
	{
		var definition : vehicleVisualModdingDefinition;
		GetMyPS().GetDataForVehicleWithID( ID, definition );
		return definition;
	}

	protected event OnStoreVisualCustomizationDataForIDEvent( evt : StoreVisualCustomizationDataForIDEvent )
	{
		GetMyPS().StoreCustomizationData( evt.ID, evt.definition );
	}

	private const function GetOwner() : GameObject
	{
		return ( ( GameObject )( GetEntity() ) );
	}

	private const function GetMyPS() : vehicleVisualCustomizationComponentPS
	{
		return ( ( vehicleVisualCustomizationComponentPS )( GetPS() ) );
	}

	public function VehicleVisualCustomizationColorParamsToColor( angle : Float, onlyHue : Bool, optional saturation : Float, optional brightness : Float ) : Color
	{
		var hue : Float;
		var sector : Int32;
		var sectorFraction : Float;
		var p, q, t : Float;
		var pScaled, qScaled, tScaled, vScaled : Int32;
		var color : Color;
		if( onlyHue )
		{
			saturation = 1.0;
			brightness = 1.0;
		}
		angle = ( ( angle / ( 2.0 * Pi() ) ) * 6.0 ) * 60.0;
		hue = angle / 360.0;
		sector = FloorF( hue * 6.0 ) % 6;
		sectorFraction = ( hue * 6.0 ) - ( ( Float )( FloorF( hue * 6.0 ) ) );
		p = brightness * ( 1.0 - saturation );
		q = brightness * ( 1.0 - ( sectorFraction * saturation ) );
		t = brightness * ( 1.0 - ( ( 1.0 - sectorFraction ) * saturation ) );
		pScaled = ( ( Int32 )( p * 255.0 ) );
		qScaled = ( ( Int32 )( q * 255.0 ) );
		tScaled = ( ( Int32 )( t * 255.0 ) );
		vScaled = ( ( Int32 )( brightness * 255.0 ) );
		switch( sector )
		{
			case 0:
				color = Color( ( ( Uint8 )( vScaled ) ), ( ( Uint8 )( tScaled ) ), ( ( Uint8 )( pScaled ) ), ( ( Uint8 )( 255.0 ) ) );
			break;
			case 1:
				color = Color( ( ( Uint8 )( qScaled ) ), ( ( Uint8 )( vScaled ) ), ( ( Uint8 )( pScaled ) ), ( ( Uint8 )( 255.0 ) ) );
			break;
			case 2:
				color = Color( ( ( Uint8 )( pScaled ) ), ( ( Uint8 )( vScaled ) ), ( ( Uint8 )( tScaled ) ), ( ( Uint8 )( 255.0 ) ) );
			break;
			case 3:
				color = Color( ( ( Uint8 )( pScaled ) ), ( ( Uint8 )( qScaled ) ), ( ( Uint8 )( vScaled ) ), ( ( Uint8 )( 255.0 ) ) );
			break;
			case 4:
				color = Color( ( ( Uint8 )( tScaled ) ), ( ( Uint8 )( pScaled ) ), ( ( Uint8 )( vScaled ) ), ( ( Uint8 )( 255.0 ) ) );
			break;
			default:
				color = Color( ( ( Uint8 )( vScaled ) ), ( ( Uint8 )( pScaled ) ), ( ( Uint8 )( qScaled ) ), ( ( Uint8 )( 255.0 ) ) );
			break;
		}
		if( color == Color( 0, 0, 0, 255 ) )
		{
			color = Color( 5, 5, 5, 255 );
		}
		return color;
	}

	public function GetAreColorsUnchanged( newSet : vehicleVisualModdingDefinition, previousSet : vehicleVisualModdingDefinition ) : Bool
	{
		var colorACompare : Bool;
		var colorBCompare : Bool;
		colorACompare = VehicleVisualCustomizationColorParamsToColor( newSet.primaryColorH, false, newSet.primaryColorS, newSet.primaryColorB ) == VehicleVisualCustomizationColorParamsToColor( previousSet.primaryColorH, false, previousSet.primaryColorS, previousSet.primaryColorB );
		colorBCompare = VehicleVisualCustomizationColorParamsToColor( newSet.secondaryColorH, false, newSet.secondaryColorS, newSet.secondaryColorB ) == VehicleVisualCustomizationColorParamsToColor( previousSet.secondaryColorH, false, previousSet.secondaryColorS, previousSet.secondaryColorB );
		return colorACompare && colorBCompare;
	}

}

class vehicleVisualCustomizationComponentPS extends GameComponentPS
{
	private persistent var m_storedVisualCustomizationData : array< vehicleVisualCustomizationPersistentData >;

	public function StoreCustomizationData( ID : TweakDBID, data : vehicleVisualModdingDefinition )
	{
		var dataPackage : vehicleVisualCustomizationPersistentData;
		var i : Int32;
		var checkedID : TweakDBID;
		while( i < m_storedVisualCustomizationData.Size() )
		{
			checkedID = m_storedVisualCustomizationData[ i ].ID;
			if( checkedID == ID )
			{
				m_storedVisualCustomizationData[ i ].visualCustomizationData = data;
				return;
			}
			i += 1;
		}
		dataPackage.ID = ID;
		dataPackage.visualCustomizationData = data;
		m_storedVisualCustomizationData.PushBack( dataPackage );
	}

	public function GetDataForVehicleWithID( ID : TweakDBID, out data : vehicleVisualModdingDefinition ) : Bool
	{
		var i : Int32;
		var checkedID : TweakDBID;
		if( m_storedVisualCustomizationData.Size() == 0 )
		{
			return false;
		}
		while( i < m_storedVisualCustomizationData.Size() )
		{
			checkedID = m_storedVisualCustomizationData[ i ].ID;
			if( checkedID == ID )
			{
				data = m_storedVisualCustomizationData[ i ].visualCustomizationData;
				return true;
			}
			i += 1;
		}
		return false;
	}

}

