class VideoCarouselController extends inkLogicController
{
	[ category = "Video" ]
	private editable var m_videoTitleRef : inkTextRef;
	[ category = "Video" ]
	private editable var m_videoDescriptionRef : inkTextRef;
	[ category = "Video" ]
	private editable var m_videoWidgetRef : inkVideoRef;
	[ category = "Controls" ]
	private editable var m_switchLeftArrow : inkWidgetRef;
	[ category = "Controls" ]
	private editable var m_switchRightArrow : inkWidgetRef;
	[ category = "Controls" ]
	private const editable var m_switchDotIndicators : array< inkWidgetRef >;
	private var m_videoWidget : weak< inkVideo >;
	private var m_videoSwitchLeftArrow : weak< inkButtonController >;
	private var m_videoSwitchRightArrow : weak< inkButtonController >;
	private var m_videos : array< VideoCarouselData >;
	private var m_currentVideo : Int32;
	private var m_isPaused : Bool;
	default m_isPaused = false;

	protected event OnInitialize()
	{
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnGlobalRelease' );
		m_videoSwitchLeftArrow = ( ( inkButtonController )( inkWidgetRef.GetControllerByType( m_switchLeftArrow, 'inkButtonController' ) ) );
		m_videoSwitchRightArrow = ( ( inkButtonController )( inkWidgetRef.GetControllerByType( m_switchRightArrow, 'inkButtonController' ) ) );
		m_videoSwitchLeftArrow.RegisterToCallback( 'OnButtonStateChanged', this, 'OnSwitchLeftArrowClicked' );
		m_videoSwitchRightArrow.RegisterToCallback( 'OnButtonStateChanged', this, 'OnSwitchRightArrowClicked' );
		m_videoWidget = ( ( inkVideo )( inkVideoRef.Get( m_videoWidgetRef ) ) );
		m_videoWidget.RegisterToCallback( 'OnVideoFinished', this, 'OnVideoFinished' );
	}

	protected event OnUninitialize()
	{
		UnregisterFromGlobalInputCallback( 'OnPostOnRelease', this, 'OnGlobalRelease' );
		UnregisterFromCallback( 'OnButtonStateChanged', this, 'OnSwitchLeftArrowClicked' );
		UnregisterFromCallback( 'OnButtonStateChanged', this, 'OnSwitchRightArrowClicked' );
		m_videoWidget.UnregisterFromCallback( 'OnVideoFinished', this, 'OnVideoFinished' );
	}

	private function OnSwitchLeftArrowClicked( controller : weak< inkButtonController >, oldState : inkEButtonState, newState : inkEButtonState )
	{
		if( newState == inkEButtonState.Press )
		{
			PlaySound( 'Button', 'OnPress' );
			SwapVideo( ECustomFilterDPadNavigationOption.SelectPrev );
		}
	}

	private function OnSwitchRightArrowClicked( controller : weak< inkButtonController >, oldState : inkEButtonState, newState : inkEButtonState )
	{
		if( newState == inkEButtonState.Press )
		{
			PlaySound( 'Button', 'OnPress' );
			SwapVideo( ECustomFilterDPadNavigationOption.SelectNext );
		}
	}

	protected event OnGlobalRelease( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'video_next' ) )
		{
			PlaySound( 'Button', 'OnPress' );
			SwapVideo( ECustomFilterDPadNavigationOption.SelectNext );
		}
		else if( evt.IsAction( 'video_prior' ) )
		{
			PlaySound( 'Button', 'OnPress' );
			SwapVideo( ECustomFilterDPadNavigationOption.SelectPrev );
		}
	}

	public function PopulateVideos( videos : array< VideoCarouselData > )
	{
		m_videos = videos;
		m_currentVideo = 0;
		SetSwitchDotIndicators( m_currentVideo );
		inkTextRef.SetLocalizedText( m_videoTitleRef, m_videos[ m_currentVideo ].videoTitleKey );
		inkTextRef.SetLocalizedText( m_videoDescriptionRef, m_videos[ m_currentVideo ].videoDescriptionKey );
		m_videoWidget.SetVideoPath( m_videos[ m_currentVideo ].videoResPath );
		m_videoWidget.SetLoop( false );
		m_videoWidget.Play();
	}

	protected event OnVideoFinished( target : weak< inkVideo > )
	{
		SwapVideo( ECustomFilterDPadNavigationOption.SelectNext );
	}

	private function SetSwitchDotIndicators( index : Int32 )
	{
		var i, arraySize : Int32;
		arraySize = m_switchDotIndicators.Size();
		for( i = 0; i < arraySize; i += 1 )
		{
			inkWidgetRef.SetState( m_switchDotIndicators[ i ], 'Default' );
		}
		inkWidgetRef.SetState( m_switchDotIndicators[ index ], 'Selected' );
	}

	private function SwapVideo( option : ECustomFilterDPadNavigationOption )
	{
		m_videoWidget.Stop();
		switch( option )
		{
			case ECustomFilterDPadNavigationOption.SelectNext:
				m_currentVideo = ( ( m_currentVideo < ( m_videos.Size() - 1 ) ) ? ( m_currentVideo + 1 ) : ( 0 ) );
			break;
			case ECustomFilterDPadNavigationOption.SelectPrev:
				m_currentVideo = ( ( m_currentVideo > 0 ) ? ( m_currentVideo - 1 ) : ( m_videos.Size() - 1 ) );
			break;
		}
		SetSwitchDotIndicators( m_currentVideo );
		inkTextRef.SetLocalizedText( m_videoTitleRef, m_videos[ m_currentVideo ].videoTitleKey );
		inkTextRef.SetLocalizedText( m_videoDescriptionRef, m_videos[ m_currentVideo ].videoDescriptionKey );
		m_videoWidget.SetVideoPath( m_videos[ m_currentVideo ].videoResPath );
		m_videoWidget.Play();
	}

	public function PauseVideo( pause : Bool )
	{
		m_isPaused = pause;
		if( pause )
		{
			m_videoWidget.Pause();
		}
		else if( !( pause ) && m_videoWidget.IsPaused() )
		{
			m_videoWidget.Resume();
		}
		else if( ( !( pause ) && !( m_videoWidget.IsPaused() ) ) && !( m_videoWidget.IsPlayingVideo() ) )
		{
			m_videoWidget.Play();
		}
	}

}

struct VideoCarouselData
{
	editable var videoTitleKey : CName;
	editable var videoDescriptionKey : CName;
	editable var videoResPath : ResRef;
}

