class hudCarRaceController extends inkHUDGameController
{
	private editable var m_Countdown : inkCanvasRef;
	private editable var m_PositionCounter : inkCanvasRef;
	private editable var m_RacePosition : inkTextRef;
	private editable var m_RaceTime : inkTextRef;
	private editable var m_RaceCheckpoint : inkTextRef;
	private var m_maxPosition : Int32;
	private var m_maxCheckpoint : Int32;
	private var m_playerPosition : Int32;
	private var m_minute : Int32;
	private var m_activeVehicleUIBlackboard : weak< IBlackboard >;
	private var m_activeVehicle : weak< VehicleObject >;
	private var m_raceStartEngineTime : EngineTime;
	private var m_factCallbackID : Uint32;

	protected event OnInitialize() {}

	protected event OnUninitialize()
	{
		Setup( false );
	}

	protected event OnForwardVehicleRaceUIEvent( evt : ForwardVehicleRaceUIEvent )
	{
		switch( evt.mode )
		{
			case vehicleRaceUI.PreRaceSetup:
				GetRootWidget().SetVisible( false );
			inkCanvasRef.SetVisible( m_Countdown, false );
			inkCanvasRef.SetVisible( m_PositionCounter, false );
			m_maxPosition = evt.maxPosition;
			m_maxCheckpoint = evt.maxCheckpoints;
			Setup( true );
			break;
			case vehicleRaceUI.CountdownStart:
				StartCountdown();
			break;
			case vehicleRaceUI.RaceStart:
				StartRace();
			break;
			case vehicleRaceUI.RaceEnd:
				EndRace();
			break;
			case vehicleRaceUI.Disable:
				GetRootWidget().SetVisible( false );
			Setup( false );
			break;
		}
	}

	private function Setup( on : Bool )
	{
		var questSys : QuestsSystem;
		m_activeVehicle = GetMountedVehicle( GetPlayerControlledObject() );
		if( m_activeVehicle == NULL )
		{
			return;
		}
		m_activeVehicleUIBlackboard = GameInstance.GetBlackboardSystem( m_activeVehicle.GetGame() ).Get( GetAllBlackboardDefs().UI_ActiveVehicleData );
		questSys = GameInstance.GetQuestsSystem( m_activeVehicle.GetGame() );
		if( m_activeVehicleUIBlackboard )
		{
			if( on )
			{
				m_factCallbackID = questSys.RegisterEntity( 'sq024_current_race_checkpoint_fact_add', m_activeVehicle.GetEntityID() );
			}
			else
			{
				questSys.UnregisterEntity( 'sq024_current_race_checkpoint_fact_add', m_factCallbackID );
				m_factCallbackID = 0;
			}
		}
	}

	private function StartCountdown()
	{
		PlayLibraryAnimation( 'Countdown' );
		PlayLibraryAnimation( 'Position_counter' );
		SetupCounters();
		inkCanvasRef.SetVisible( m_Countdown, true );
		inkCanvasRef.SetVisible( m_PositionCounter, true );
		GetRootWidget().SetVisible( true );
	}

	private function SetupCounters()
	{
		var playerPosition : Int32;
		inkTextRef.SetText( m_RaceCheckpoint, "0/" + IntToString( m_maxCheckpoint ) );
		playerPosition = m_activeVehicleUIBlackboard.GetInt( GetAllBlackboardDefs().UI_ActiveVehicleData.PositionInRace );
		inkTextRef.SetText( m_RacePosition, ( IntToString( playerPosition ) + "/" ) + IntToString( m_maxPosition ) );
		inkTextRef.SetText( m_RaceTime, "00:00:00" );
	}

	private function StartRace()
	{
		m_raceStartEngineTime = GameInstance.GetSimTime( m_activeVehicle.GetGame() );
	}

	private function EndRace() {}

	protected event OnVehicleForwardRaceCheckpointFactEvent( evt : VehicleForwardRaceCheckpointFactEvent )
	{
		var questSys : QuestsSystem;
		var factValue : Int32;
		questSys = GameInstance.GetQuestsSystem( m_activeVehicle.GetGame() );
		factValue = questSys.GetFact( 'sq024_current_race_checkpoint_fact_add' );
		inkTextRef.SetText( m_RaceCheckpoint, ( IntToString( factValue ) + "/" ) + IntToString( m_maxCheckpoint ) );
	}

	protected event OnVehicleForwardRaceClockUpdateEvent( evt : VehicleForwardRaceClockUpdateEvent )
	{
		var params : inkTextParams;
		var currentEngineTime : EngineTime;
		var enginetimeToDisplay : EngineTime;
		var engineTimeFloat : Float;
		var minutes : Int32;
		var second : Int32;
		var millisecondFloat : Float;
		var millisecond : Int32;
		var currentPlayerPosition : Int32;
		var questSystem : QuestsSystem;
		currentEngineTime = GameInstance.GetSimTime( m_activeVehicle.GetGame() );
		enginetimeToDisplay = currentEngineTime - m_raceStartEngineTime;
		engineTimeFloat = EngineTime.ToFloat( enginetimeToDisplay );
		minutes = RoundF( engineTimeFloat / 60.0 );
		second = ( ( Int32 )( engineTimeFloat ) ) - ( minutes * 60 );
		millisecondFloat = ( engineTimeFloat - ( ( Float )( second ) ) ) - ( ( ( Float )( minutes ) ) * 60.0 );
		millisecond = ( ( Int32 )( millisecondFloat * 100.0 ) );
		params = new inkTextParams;
		params.AddNumber( "Minutes", minutes );
		params.AddNumber( "Seconds", second );
		params.AddNumber( "Milliseconds", millisecond );
		inkTextRef.SetLocalizedTextScript( m_RaceTime, "LocKey#77071", params );
		currentPlayerPosition = m_activeVehicleUIBlackboard.GetInt( GetAllBlackboardDefs().UI_ActiveVehicleData.PositionInRace );
		if( m_playerPosition != currentPlayerPosition )
		{
			questSystem = GameInstance.GetQuestsSystem( m_activeVehicle.GetGame() );
			m_playerPosition = currentPlayerPosition;
			inkTextRef.SetText( m_RacePosition, ( IntToString( m_playerPosition ) + "/" ) + IntToString( m_maxPosition ) );
			questSystem.SetFact( 'sq024_current_race_player_position', m_playerPosition );
		}
	}

}

