importonly final struct BinkResource
{
	public import static function IsValid( self : BinkResource ) : Bool;
	public import static function GetPath( self : BinkResource ) : ResRef;
}

import class gameuiCreditsController extends inkGameController
{
	import var shouldShowRewardPrompt : Bool;
	import var isInFinalBoardsMode : Bool;
	import var exitNotificationDisplayTime : Float;
	import var isPreVideoFinished : Bool;
	import var isEp1CreditsImplementation : Bool;
	import var exitTooltipContainer : inkCompoundRef;
}

class CreditsData extends inkUserData
{
	var isFinalBoards : Bool;
	editable var showRewardPrompt : Bool;
}

class CreditsGameController extends gameuiCreditsController
{
	private editable var m_videoContainer : inkCompoundRef;
	private editable var m_sceneTexture : inkImageRef;
	private editable var m_backgroundVideo : inkVideoRef;
	private editable var m_binkVideo : inkVideoRef;
	private const editable var m_binkVideos : array< BinkResource >;
	private editable var m_fastForward : inkTextRef;
	private editable var m_timerUntilFadeEp1 : Float;
	private editable var m_musicVideoEp1 : inkVideoRef;
	private editable var m_creditsAnimEp1 : inkCompoundRef;
	private var m_currentBinkVideo : Int32;
	private var m_videoSummary : VideoWidgetSummary;
	private var m_isDataSet : Bool;
	private var m_accumulatedTime : Float;
	private var m_isCounting : Bool;

	protected event OnInitialize()
	{
		InitializeCredits();
	}

	protected event OnUpdate( timeDelta : Float )
	{
		if( ( isInFinalBoardsMode && isEp1CreditsImplementation ) && m_isCounting )
		{
			m_accumulatedTime += timeDelta;
			if( m_accumulatedTime > m_timerUntilFadeEp1 )
			{
				m_isCounting = false;
				FinishVideo();
			}
		}
	}

	protected event OnUninitialize()
	{
		inkVideoRef.Stop( m_backgroundVideo );
		inkVideoRef.Stop( m_binkVideo );
		inkVideoRef.UnregisterFromCallback( m_musicVideoEp1, 'OnVideoFinished', this, 'OnVideoFinished' );
	}

	protected event OnSetUserData( data : IScriptable )
	{
		var creditsData : CreditsData;
		creditsData = ( ( CreditsData )( data ) );
		if( creditsData.isFinalBoards )
		{
			inkImageRef.SetVisible( m_sceneTexture, true );
			inkVideoRef.SetVisible( m_binkVideo, false );
		}
		else
		{
			inkImageRef.SetVisible( m_sceneTexture, false );
			inkVideoRef.SetVisible( m_binkVideo, true );
		}
		isInFinalBoardsMode = creditsData.isFinalBoards;
		shouldShowRewardPrompt = creditsData.showRewardPrompt;
		m_isDataSet = true;
	}

	protected event OnVideoFinished( target : weak< inkVideo > )
	{
		var isMusicVideo : Bool;
		isMusicVideo = target.GetName() == inkVideoRef.GetName( m_musicVideoEp1 );
		if( isMusicVideo )
		{
			FinishVideo();
		}
	}

	private function FinishVideo()
	{
		if( isEp1CreditsImplementation )
		{
			inkVideoRef.SetVisible( m_musicVideoEp1, false );
			inkTextRef.SetVisible( m_fastForward, true );
			PlayCredits();
		}
	}

	private function InitializeCredits()
	{
		if( isEp1CreditsImplementation && isInFinalBoardsMode )
		{
			m_isCounting = true;
			m_accumulatedTime = 0.0;
			isPreVideoFinished = false;
			inkCompoundRef.SetVisible( exitTooltipContainer, false );
			inkTextRef.SetVisible( m_fastForward, false );
			inkCompoundRef.SetVisible( m_creditsAnimEp1, true );
			inkVideoRef.SetVisible( m_musicVideoEp1, true );
			inkVideoRef.Play( m_musicVideoEp1 );
			inkVideoRef.RegisterToCallback( m_musicVideoEp1, 'OnVideoFinished', this, 'OnVideoFinished' );
		}
		else
		{
			PlayCredits();
			inkCompoundRef.SetVisible( m_creditsAnimEp1, false );
			inkVideoRef.SetVisible( m_musicVideoEp1, false );
			inkVideoRef.Stop( m_musicVideoEp1 );
		}
	}

	private function PlayCredits()
	{
		if( !( m_isDataSet ) )
		{
			inkImageRef.SetVisible( m_sceneTexture, false );
			inkVideoRef.SetVisible( m_binkVideo, true );
		}
		inkVideoRef.Play( m_backgroundVideo );
		isPreVideoFinished = true;
		inkVideoRef.SetTranslation( m_binkVideo, -400.0, 0.0 );
		inkCompoundRef.SetVisible( exitTooltipContainer, true );
	}

	private function PlayNextVideo()
	{
		var ratio : Float;
		var videoContainerSize : Vector2;
		if( m_currentBinkVideo >= m_binkVideos.Size() )
		{
			return;
		}
		videoContainerSize = inkCompoundRef.GetSize( m_videoContainer );
		inkVideoRef.SetVideoPath( m_binkVideo, BinkResource.GetPath( m_binkVideos[ m_currentBinkVideo ] ) );
		if( !( inkVideoRef.IsPlayingVideo( m_binkVideo ) ) )
		{
			inkVideoRef.Play( m_binkVideo );
			m_videoSummary = inkVideoRef.GetVideoWidgetSummary( m_binkVideo );
			ratio = ( ( Float )( m_videoSummary.width ) ) / ( ( Float )( m_videoSummary.height ) );
			inkVideoRef.SetSize( m_binkVideo, videoContainerSize.X, videoContainerSize.X / ratio );
		}
		m_currentBinkVideo += 1;
	}

}

