class RoachRaceGameController extends MinigameController
{
	private editable var m_gameMenu : inkWidgetRef;
	private editable var m_scoreboardMenu : inkWidgetRef;
	private var m_isCutsceneInProgress : Bool;

	protected event OnInitialize()
	{
		m_isCutsceneInProgress = false;
		OpenMenu();
	}

	protected override function OnGameFinishLogic( gameFinishEvent : GameFinishEvent )
	{
		var gameState : MinigameState;
		var scoreSystem : SideScrollerMiniGameScoreSystem;
		var scoreRequest : SendScoreRequest;
		var lastMaxRecord : Int32;
		gameState = gameFinishEvent.gameState;
		scoreSystem = ( ( SideScrollerMiniGameScoreSystem )( GameInstance.GetScriptableSystemsContainer( GetPlayerControlledObject().GetGame() ).Get( 'SideScrollerMiniGameScoreSystem' ) ) );
		scoreRequest = new SendScoreRequest;
		scoreRequest.m_score = gameState.currentScore;
		scoreRequest.m_gameName = "RoachRace";
		scoreSystem.QueueRequest( scoreRequest );
		lastMaxRecord = scoreSystem.GetMaxScore( "RoachRace" );
		OpenScoreboard( ( ( lastMaxRecord > gameState.currentScore ) ? ( lastMaxRecord ) : ( gameState.currentScore ) ) );
	}

	private function OpenMenu()
	{
		var animation : inkAnimProxy;
		m_isCutsceneInProgress = true;
		animation = PlayLibraryAnimation( 'MenuIntro' );
		animation.RegisterToCallback( inkanimEventType.OnFinish, this, 'FinishCutscene' );
		SetEnableComponent( m_gameMenu, true );
		SetEnableComponent( gameplayCanvas, false );
		SetEnableComponent( m_scoreboardMenu, false );
		UnregisterFromCallback( 'OnRelease', this, 'OnOpenMenuClick' );
		RegisterToCallback( 'OnRelease', this, 'OnStartGameClick' );
	}

	private function OpenScoreboard( playerScore : Int32 )
	{
		var scoreboardLogic : ScoreboardLogicController;
		scoreboardLogic = ( ( ScoreboardLogicController )( inkWidgetRef.GetController( m_scoreboardMenu ) ) );
		scoreboardLogic.CleanGrid();
		SetEnableComponent( m_scoreboardMenu, true );
		SetEnableComponent( m_gameMenu, false );
		SetEnableComponent( gameplayCanvas, false );
		RegisterToCallback( 'OnRelease', this, 'OnOpenMenuClick' );
		scoreboardLogic.FillGrid( playerScore );
	}

	private function OpenGameplay()
	{
		SetEnableComponent( gameplayCanvas, true );
		SetEnableComponent( m_gameMenu, false );
		SetEnableComponent( m_scoreboardMenu, false );
		UnregisterFromCallback( 'OnRelease', this, 'OnStartGameClick' );
	}

	private function SetEnableComponent( component : inkWidgetRef, isEnabled : Bool )
	{
		inkWidgetRef.SetVisible( component, isEnabled );
		inkWidgetRef.SetInteractive( component, isEnabled );
	}

	public function OnOpenMenuClick( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) && !( m_isCutsceneInProgress ) )
		{
			OpenMenu();
		}
	}

	public function OnStartGameClick( e : inkPointerEvent )
	{
		var outroAnimation : inkAnimProxy;
		if( e.IsAction( 'click' ) && !( m_isCutsceneInProgress ) )
		{
			m_isCutsceneInProgress = true;
			outroAnimation = PlayLibraryAnimation( 'MenuOutro' );
			outroAnimation.RegisterToCallback( inkanimEventType.OnFinish, this, 'FinishCutscene' );
			outroAnimation.RegisterToCallback( inkanimEventType.OnFinish, this, 'GameStart' );
		}
	}

	public function FinishCutscene( e : inkAnimProxy )
	{
		m_isCutsceneInProgress = false;
	}

	public function GameStart( e : inkAnimProxy )
	{
		OpenGameplay();
		StartGame();
	}

}

