import class BaseVehicleHUDGameController extends inkHUDGameController
{
	protected import const var mounted : Bool;

	protected event OnVehicleMounted();
	protected event OnVehicleUnmounted();
}

class vehicleDebugUIGameController extends BaseVehicleHUDGameController
{
	private var m_vehicleBlackboard : weak< IBlackboard >;
	private var m_vehicleBBStateConectionId : CallbackHandle;
	private var m_mountBBConnectionId : CallbackHandle;
	private var m_speedBBConnectionId : CallbackHandle;
	private var m_gearBBConnectionId : CallbackHandle;
	private var m_rpmValueBBConnectionId : CallbackHandle;
	private var m_rpmMaxBBConnectionId : CallbackHandle;
	private var m_radioStateBBConnectionId : CallbackHandle;
	private var m_radioNameBBConnectionId : CallbackHandle;
	private var m_radioState : Bool;
	private var m_radioName : CName;
	private var m_radioStateWidget : weak< inkText >;
	private var m_radioNameWidget : weak< inkText >;
	private var m_autopilotOnId : CallbackHandle;
	private var rootWidget : weak< inkCanvas >;
	private var speedTextWidget : weak< inkText >;
	private var gearTextWidget : weak< inkText >;
	private var rpmValueWidget : weak< inkText >;
	private var rpmGaugeForegroundWidget : weak< inkRectangle >;
	private var rpmGaugeMaxSize : Vector2;
	private var rpmMinValue : Float;
	private var rpmMaxValue : Float;
	private var rpmMaxValueInitialized : Bool;
	private var autopilotTextWidget : weak< inkText >;
	private var isInAutoPilot : Bool;
	private var useDebugUI : Bool;

	protected event OnInitialize()
	{
		rootWidget = ( ( inkCanvas )( GetRootWidget() ) );
		speedTextWidget = ( ( inkText )( GetWidget( 'main_panel/speed_panel/speed' ) ) );
		gearTextWidget = ( ( inkText )( GetWidget( 'main_panel/speed_panel/gear' ) ) );
		rpmValueWidget = ( ( inkText )( GetWidget( 'main_panel/rpm_panel/rpm_value' ) ) );
		rpmGaugeForegroundWidget = ( ( inkRectangle )( GetWidget( 'main_panel/rpm_panel/rpmGauge/rpmGaugeForeground' ) ) );
		autopilotTextWidget = ( ( inkText )( GetWidget( 'main_panel/speed_panel/auto_pilot' ) ) );
		m_radioStateWidget = ( ( inkText )( GetWidget( 'RADIO/RADIOSTATE' ) ) );
		m_radioNameWidget = ( ( inkText )( GetWidget( 'RADIO/RADIONAME' ) ) );
		rootWidget.SetVisible( false );
		rpmGaugeMaxSize = rpmGaugeForegroundWidget.GetSize();
		useDebugUI = TDB.GetBool( T"vehicles.showDebugUi", false );
		RegisterDebugCommand( 'OnActivateTest' );
	}

	protected event OnActivateTest( value : Bool ) {}

	protected event OnUninitialize()
	{
		if( m_vehicleBlackboard )
		{
			m_vehicleBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().Vehicle.VehicleState, m_vehicleBBStateConectionId );
			m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.SpeedValue, m_speedBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().Vehicle.GearValue, m_gearBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMValue, m_rpmValueBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMMax, m_rpmMaxBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.IsAutopilotOn, m_autopilotOnId );
			m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.VehRadioState, m_radioStateBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerName( GetAllBlackboardDefs().Vehicle.VehRadioStationName, m_radioNameBBConnectionId );
		}
	}

	protected event OnVehicleMounted()
	{
		var playerPuppet : weak< GameObject >;
		var vehicle : weak< VehicleObject >;
		if( useDebugUI )
		{
			playerPuppet = ( ( PlayerPuppet )( GetOwnerEntity() ) );
			vehicle = GetMountedVehicle( playerPuppet );
			m_vehicleBlackboard = vehicle.GetBlackboard();
			if( m_vehicleBlackboard )
			{
				m_vehicleBBStateConectionId = m_vehicleBlackboard.RegisterListenerInt( GetAllBlackboardDefs().Vehicle.VehicleState, this, 'OnVehicleStateChanged' );
				m_speedBBConnectionId = m_vehicleBlackboard.RegisterListenerFloat( GetAllBlackboardDefs().Vehicle.SpeedValue, this, 'OnSpeedValueChanged' );
				m_gearBBConnectionId = m_vehicleBlackboard.RegisterListenerInt( GetAllBlackboardDefs().Vehicle.GearValue, this, 'OnGearValueChanged' );
				m_rpmValueBBConnectionId = m_vehicleBlackboard.RegisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMValue, this, 'OnRpmValueChanged' );
				m_rpmMaxBBConnectionId = m_vehicleBlackboard.RegisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMMax, this, 'OnRpmMaxChanged' );
				m_autopilotOnId = m_vehicleBlackboard.RegisterListenerBool( GetAllBlackboardDefs().Vehicle.IsAutopilotOn, this, 'OnAutopilotChanged' );
				m_radioStateBBConnectionId = m_vehicleBlackboard.RegisterListenerBool( GetAllBlackboardDefs().Vehicle.VehRadioState, this, 'OnRadioStateChanged' );
				m_radioNameBBConnectionId = m_vehicleBlackboard.RegisterListenerName( GetAllBlackboardDefs().Vehicle.VehRadioStationName, this, 'OnRadioNameChanged' );
			}
		}
	}

	protected event OnVehicleUnmounted()
	{
		if( m_vehicleBlackboard )
		{
			m_vehicleBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().Vehicle.VehicleState, m_vehicleBBStateConectionId );
			m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.SpeedValue, m_speedBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerInt( GetAllBlackboardDefs().Vehicle.GearValue, m_gearBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMValue, m_rpmValueBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerFloat( GetAllBlackboardDefs().Vehicle.RPMMax, m_rpmMaxBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.IsAutopilotOn, m_autopilotOnId );
			m_vehicleBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().Vehicle.VehRadioState, m_radioStateBBConnectionId );
			m_vehicleBlackboard.UnregisterListenerName( GetAllBlackboardDefs().Vehicle.VehRadioStationName, m_radioNameBBConnectionId );
		}
		rootWidget.SetVisible( false );
	}

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

	protected event OnSpeedValueChanged( speedValue : Float )
	{
		if( speedValue < 0.0 )
		{
			speedTextWidget.SetText( IntToString( RoundMath( speedValue * 3.5999999 ) * -1 ) );
		}
		else
		{
			speedTextWidget.SetText( IntToString( RoundMath( speedValue * 3.5999999 ) ) );
		}
	}

	protected event OnGearValueChanged( gearValue : Int32 )
	{
		if( gearValue == ( ( Int32 )( 0 ) ) )
		{
			gearTextWidget.SetText( "R" );
		}
		else if( gearValue == ( ( Int32 )( -1 ) ) )
		{
			gearTextWidget.SetText( "N" );
		}
		else
		{
			gearTextWidget.SetText( IntToString( gearValue ) );
		}
	}

	protected event OnRpmValueChanged( rpmValue : Float )
	{
		rpmValueWidget.SetText( IntToString( RoundMath( rpmValue ) ) );
		rpmGaugeForegroundWidget.SetSize( Vector2( ( ( Float )( ( rpmValue * ( ( Float )( rpmGaugeMaxSize.X ) ) ) ) ) / rpmMaxValue, ( ( Float )( rpmGaugeMaxSize.Y ) ) ) );
	}

	protected event OnRpmMaxChanged( rpmMax : Float )
	{
		rpmMaxValue = rpmMax + 1000.0;
	}

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

	private function RefreshUI()
	{
		if( isInAutoPilot )
		{
			autopilotTextWidget.SetVisible( isInAutoPilot );
		}
		else
		{
			autopilotTextWidget.SetVisible( isInAutoPilot );
		}
	}

	protected event OnRadioStateChanged( state : Bool )
	{
		if( state )
		{
			m_radioState = true;
		}
		else if( !( state ) )
		{
			m_radioState = false;
		}
		m_radioStateWidget.SetText( BoolToString( m_radioState ) );
	}

	protected event OnRadioNameChanged( stationName : CName )
	{
		m_radioNameWidget.SetText( NameToString( stationName ) );
	}

}

