class VehicleUIactivateEvent extends Event
{
	var m_activate : Bool;
}

class vehicleInteriorUIGameController extends inkHUDGameController
{
	private var m_vehicleBlackboard : weak< IBlackboard >;
	private var m_vehicleBBStateConectionId : CallbackHandle;
	private var m_vehicleBBReadyConectionId : CallbackHandle;
	private var m_vehicleBBUIActivId : CallbackHandle;
	private var m_speedBBConnectionId : CallbackHandle;
	private var m_gearBBConnectionId : CallbackHandle;
	private var m_rpmValueBBConnectionId : CallbackHandle;
	private var m_rpmMaxBBConnectionId : CallbackHandle;
	private var m_autopilotOnId : CallbackHandle;
	private var m_rootWidget : weak< inkCanvas >;
	private editable var m_speedTextWidget : inkTextRef;
	private editable var m_gearTextWidget : inkTextRef;
	private editable var m_rpmValueWidget : inkTextRef;
	private editable var m_rpmGaugeForegroundWidget : inkRectangleRef;
	private editable var m_autopilotTextWidget : inkTextRef;
	private var m_activeChunks : Int32;
	private editable var m_chunksNumber : Int32;
	private editable var m_dynamicRpmPath : CName;
	private editable var m_rpmPerChunk : Int32;
	private editable var m_hasRevMax : Bool;
	private var m_rpmGaugeMaxSize : Vector2;
	private var m_rpmMaxValue : Float;
	private var m_isInAutoPilot : Bool;
	private var m_isVehicleReady : Bool;
	private var m_HudRedLineAnimation : inkAnimProxy;

	protected event OnInitialize()
	{
		var vehicle : weak< VehicleObject >;
		vehicle = ( ( VehicleObject )( GetOwnerEntity() ) );
		m_rootWidget = ( ( inkCanvas )( GetRootWidget() ) );
		m_rootWidget.SetVisible( false );
		m_rpmGaugeMaxSize = inkRectangleRef.GetSize( m_rpmGaugeForegroundWidget );
		m_vehicleBlackboard = vehicle.GetBlackboard();
		if( IsUIactive() )
		{
			ActivateUI();
		}
		if( m_vehicleBlackboard )
		{
			m_vehicleBBUIActivId = m_vehicleBlackboard.RegisterListenerBool( GetAllBlackboardDefs().Vehicle.IsUIActive, this, 'OnActivateUI' );
		}
	}

	protected event OnUninitialize()
	{
		if( m_vehicleBlackboard )
		{
			m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.IsUIActive, m_vehicleBBUIActivId );
			UnregisterBlackBoardCallbacks();
		}
	}

	private function RegisterBlackBoardCallbacks()
	{
		if( m_vehicleBlackboard )
		{
			if( !( m_vehicleBBStateConectionId ) )
			{
				m_vehicleBBStateConectionId = m_vehicleBlackboard.RegisterListenerInt( GetAllBlackboardDefs().Vehicle.VehicleState, this, 'OnVehicleStateChanged' );
			}
			if( !( m_speedBBConnectionId ) )
			{
				m_speedBBConnectionId = m_vehicleBlackboard.RegisterListenerFloat( GetAllBlackboardDefs().Vehicle.SpeedValue, this, 'OnSpeedValueChanged' );
			}
			if( !( m_gearBBConnectionId ) )
			{
				m_gearBBConnectionId = m_vehicleBlackboard.RegisterListenerInt( GetAllBlackboardDefs().Vehicle.GearValue, this, 'OnGearValueChanged' );
			}
			if( !( m_rpmValueBBConnectionId ) )
			{
				m_rpmValueBBConnectionId = m_vehicleBlackboard.RegisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMValue, this, 'OnRpmValueChanged' );
			}
			if( !( m_rpmMaxBBConnectionId ) )
			{
				m_rpmMaxBBConnectionId = m_vehicleBlackboard.RegisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMMax, this, 'OnRpmMaxChanged' );
			}
			if( !( m_autopilotOnId ) )
			{
				m_autopilotOnId = m_vehicleBlackboard.RegisterListenerBool( GetAllBlackboardDefs().Vehicle.IsAutopilotOn, this, 'OnAutopilotChanged' );
			}
			if( !( m_vehicleBBReadyConectionId ) )
			{
				m_vehicleBBReadyConectionId = m_vehicleBlackboard.RegisterListenerBool( GetAllBlackboardDefs().Vehicle.VehicleReady, this, 'OnVehicleReady' );
			}
		}
	}

	private function UnregisterBlackBoardCallbacks()
	{
		if( m_vehicleBlackboard )
		{
			if( m_vehicleBBStateConectionId )
			{
				m_vehicleBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().Vehicle.VehicleState, m_vehicleBBStateConectionId );
			}
			if( m_speedBBConnectionId )
			{
				m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.SpeedValue, m_speedBBConnectionId );
			}
			if( m_gearBBConnectionId )
			{
				m_vehicleBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().Vehicle.GearValue, m_gearBBConnectionId );
			}
			if( m_rpmValueBBConnectionId )
			{
				m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMValue, m_rpmValueBBConnectionId );
			}
			if( m_rpmMaxBBConnectionId )
			{
				m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMMax, m_rpmMaxBBConnectionId );
			}
			if( m_autopilotOnId )
			{
				m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.IsAutopilotOn, m_autopilotOnId );
			}
			if( m_vehicleBBReadyConectionId )
			{
				m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.VehicleReady, m_vehicleBBReadyConectionId );
			}
		}
	}

	protected event OnActivateUI( activate : Bool )
	{
		var evt : VehicleUIactivateEvent;
		evt = new VehicleUIactivateEvent;
		if( activate )
		{
			evt.m_activate = true;
		}
		else
		{
			evt.m_activate = false;
		}
		QueueEvent( evt );
	}

	protected event OnActivateUIEvent( evt : VehicleUIactivateEvent )
	{
		if( evt.m_activate )
		{
			ActivateUI();
		}
		else
		{
			DeactivateUI();
		}
	}

	private function ActivateUI()
	{
		m_rootWidget.SetVisible( true );
		RegisterBlackBoardCallbacks();
	}

	private function DeactivateUI()
	{
		UnregisterBlackBoardCallbacks();
		m_rootWidget.SetVisible( false );
	}

	private function IsUIactive() : Bool
	{
		if( m_vehicleBlackboard && m_vehicleBlackboard.GetBool( GetAllBlackboardDefs().Vehicle.IsUIActive ) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	protected event OnVehicleReady( ready : Bool )
	{
		if( ready )
		{
			m_rootWidget.SetVisible( true );
		}
		else if( !( ready ) )
		{
			m_rootWidget.SetVisible( false );
		}
		m_isVehicleReady = ready;
	}

	protected event OnVehicleStateChanged( state : Int32 )
	{
		if( state == ( ( Int32 )( vehicleEState.On ) ) )
		{
			m_rootWidget.SetVisible( true );
		}
		if( state != ( ( Int32 )( vehicleEState.On ) ) )
		{
			m_rootWidget.SetVisible( false );
		}
	}

	protected event OnRpmMaxChanged( rpmMax : Float )
	{
		var rpm : Int32;
		var level : Float;
		var levelInt : Int32;
		m_rpmMaxValue = rpmMax + 1000.0;
		rpm = ( ( Int32 )( rpmMax ) );
		level = ( ( Float )( rpm / m_rpmPerChunk ) );
		levelInt = ( ( Int32 )( level ) );
		EvaluateRPMMeterWidget( levelInt );
	}

	protected event OnSpeedValueChanged( speedValue : Float )
	{
		if( speedValue < 0.0 )
		{
			inkTextRef.SetText( m_speedTextWidget, "0" );
		}
		else
		{
			inkTextRef.SetText( m_speedTextWidget, IntToString( RoundMath( speedValue * 2.23693991 ) ) );
		}
	}

	protected event OnGearValueChanged( gearValue : Int32 )
	{
		if( gearValue == ( ( Int32 )( 0 ) ) )
		{
			inkTextRef.SetText( m_gearTextWidget, "UI-Cyberpunk-Vehicles-Gears-Neutral" );
		}
		else
		{
			inkTextRef.SetText( m_gearTextWidget, IntToString( gearValue ) );
		}
	}

	protected event OnRpmValueChanged( rpmValue : Float )
	{
		inkTextRef.SetText( m_rpmValueWidget, IntToString( RoundMath( rpmValue ) ) );
		if( rpmValue > 7800.0 )
		{
			m_HudRedLineAnimation = PlayLibraryAnimation( 'redLine' );
		}
		drawRPMGaugeFull( rpmValue );
		inkRectangleRef.SetSize( m_rpmGaugeForegroundWidget, Vector2( ( ( Float )( ( rpmValue * ( ( Float )( m_rpmGaugeMaxSize.X ) ) ) ) ) / m_rpmMaxValue, ( ( Float )( m_rpmGaugeMaxSize.Y ) ) ) );
	}

	public function drawRPMGaugeFull( rpmValue : Float )
	{
		var rpm : Int32;
		var level : Float;
		var levelInt : Int32;
		rpm = ( ( Int32 )( rpmValue ) );
		level = ( ( Float )( rpm / m_rpmPerChunk ) );
		levelInt = ( ( Int32 )( level ) );
		EvaluateRPMMeterWidget( levelInt );
	}

	private function EvaluateRPMMeterWidget( currentAmountOfChunks : Int32 )
	{
		if( currentAmountOfChunks == m_activeChunks )
		{
			return;
		}
		RedrawRPM( currentAmountOfChunks );
	}

	private function RedrawRPM( currentAmountOfChunks : Int32 )
	{
		var increasing : Bool;
		var difference : Int32;
		var chunkToModify : String;
		var widgetToModify : weak< inkRectangle >;
		var i : Int32;
		difference = Abs( currentAmountOfChunks - m_activeChunks );
		increasing = currentAmountOfChunks > m_activeChunks;
		if( increasing )
		{
			for( i = m_activeChunks; i <= ( m_activeChunks + difference ); i += 1 )
			{
				chunkToModify = NameToString( m_dynamicRpmPath ) + IntToString( i );
				widgetToModify = ( ( inkRectangle )( GetWidget( StringToName( chunkToModify ) ) ) );
				widgetToModify.SetVisible( true );
				widgetToModify.SetOpacity( 1.0 );
			}
		}
		else
		{
			for( i = m_activeChunks; i >= ( m_activeChunks - difference ); i -= 1 )
			{
				chunkToModify = NameToString( m_dynamicRpmPath ) + IntToString( i );
				widgetToModify = ( ( inkRectangle )( GetWidget( StringToName( chunkToModify ) ) ) );
				widgetToModify.SetVisible( false );
			}
		}
		m_activeChunks = currentAmountOfChunks;
	}

	private function AddChunk()
	{
		var chunkToActivate : String;
		var widgetToShow : weak< inkRectangle >;
		var i : Int32;
		if( IsNameValid( m_dynamicRpmPath ) )
		{
			for( i = 1; i <= m_activeChunks; i += 1 )
			{
				chunkToActivate = NameToString( m_dynamicRpmPath ) + IntToString( i );
				widgetToShow = ( ( inkRectangle )( GetWidget( StringToName( chunkToActivate ) ) ) );
				widgetToShow.SetVisible( true );
				widgetToShow.SetOpacity( 1.0 );
			}
		}
	}

	private function RemoveChunk()
	{
		var chunkToRemove : String;
		var widgetToHide : weak< inkRectangle >;
		var i : Int32;
		if( IsNameValid( m_dynamicRpmPath ) )
		{
			for( i = m_chunksNumber; i > m_activeChunks; i -= 1 )
			{
				chunkToRemove = NameToString( m_dynamicRpmPath ) + IntToString( i );
				widgetToHide = ( ( inkRectangle )( GetWidget( StringToName( chunkToRemove ) ) ) );
				widgetToHide.SetVisible( false );
			}
		}
	}

	protected event OnAutopilotChanged( autopilotOn : Bool )
	{
		m_isInAutoPilot = autopilotOn;
		RefreshUI();
	}

	private function RefreshUI()
	{
		if( m_isInAutoPilot )
		{
			inkTextRef.SetVisible( m_autopilotTextWidget, m_isInAutoPilot );
		}
		else
		{
			inkTextRef.SetVisible( m_autopilotTextWidget, m_isInAutoPilot );
		}
	}

}

