import class QuadRacerLogicController extends MinigameLogicController
{
	private editable var m_timeLeftText : inkTextRef;
	private editable var m_scoreText : inkTextRef;
	private editable var m_speedText : inkTextRef;
	private editable var m_notificationText : inkTextRef;
	private editable var m_notificationAnimationName : CName;
	private editable var m_speedCoeficient : Float;
	private var m_currentNotificationAnimation : inkAnimProxy;
	private var m_lastTime : Int32;

	protected override function OnInitializeGameLogic()
	{
		if( inkTextRef.IsValid( m_notificationText ) )
		{
			inkTextRef.SetVisible( m_notificationText, false );
		}
	}

	protected override function OnGameStateUpdateLogic( gameStateUpdateEvent : MiniGameStateUpdateEvent )
	{
		var secondsLeft : Int32;
		var gameState : QuadRacerGameState;
		gameState = ( ( QuadRacerGameState )( gameStateUpdateEvent.gameState ) );
		secondsLeft = ( ( Int32 )( gameState.timeLeft ) );
		if( inkTextRef.IsValid( m_timeLeftText ) )
		{
			inkTextRef.SetText( m_timeLeftText, IntToString( secondsLeft ) );
		}
		if( inkTextRef.IsValid( m_scoreText ) )
		{
			inkTextRef.SetText( m_scoreText, IntToString( gameState.currentScore ) );
		}
		if( inkTextRef.IsValid( m_speedText ) )
		{
			inkTextRef.SetText( m_speedText, IntToString( ( ( Int32 )( gameState.speed / m_speedCoeficient ) ) ) + " MpH" );
		}
		if( secondsLeft <= 0 )
		{
			PlayNotificationAnimation( "Out of time!" );
			FinishGameLogic();
		}
		else if( gameState.lapsPassed > 0 )
		{
			PlayNotificationAnimation( "Finish!" );
			FinishGameLogic();
		}
		else
		{
			if( inkTextRef.IsValid( m_notificationText ) )
			{
				if( ( secondsLeft <= 5 ) && ( m_lastTime > secondsLeft ) )
				{
					PlayNotificationAnimation( IntToString( secondsLeft ) );
				}
				if( gameState.hasPassedCheckpoint )
				{
					PlayNotificationAnimation( "Checkpoint" );
					gameState.hasPassedCheckpoint = false;
				}
			}
			m_lastTime = secondsLeft;
		}
	}

	private function PlayNotificationAnimation( const text : ref< String > )
	{
		StopCurrentNotificationAnimation();
		m_currentNotificationAnimation = PlayLibraryAnimationOnTargets( m_notificationAnimationName, SelectWidgets( inkTextRef.Get( m_notificationText ) ) );
		inkTextRef.SetText( m_notificationText, text );
	}

	private function StopCurrentNotificationAnimation()
	{
		if( m_currentNotificationAnimation.IsValid() )
		{
			m_currentNotificationAnimation.Stop();
		}
	}

}

