class ConfessionalInkGameController extends DeviceInkGameControllerBase
{
	private var m_defaultUI : weak< inkCanvas >;
	private var m_mainDisplayWidget : weak< inkVideo >;
	private var m_messegeWidget : weak< inkText >;
	private var m_defaultTextWidget : weak< inkText >;
	private var m_actionsList : weak< inkWidget >;
	private var m_RunningAnimation : inkAnimProxy;
	private var m_isConfessing : Bool;
	private var m_onGlitchingStateChangedListener : CallbackHandle;
	private var m_onConfessListener : CallbackHandle;

	protected event OnUninitialize()
	{
		super.OnUninitialize();
		m_mainDisplayWidget.Stop();
	}

	protected override function SetupWidgets()
	{
		if( !( m_isInitialized ) )
		{
			m_defaultUI = ( ( inkCanvas )( GetWidget( 'default_ui' ) ) );
			m_messegeWidget = ( ( inkText )( GetWidget( 'default_ui/messege_text' ) ) );
			m_defaultTextWidget = ( ( inkText )( GetWidget( 'default_ui/default_text' ) ) );
			m_mainDisplayWidget = ( ( inkVideo )( GetWidget( 'main_display' ) ) );
			m_actionsList = GetWidget( 'default_ui/actions_list' );
			m_rootWidget.SetVisible( false );
			m_rootWidget.SetAnchor( inkEAnchor.Fill );
			m_messegeWidget.SetVisible( false );
		}
	}

	public override function UpdateActionWidgets( const widgetsData : ref< array< SActionWidgetPackage > > )
	{
		var i : Int32;
		var widget : inkWidget;
		super.UpdateActionWidgets( widgetsData );
		for( i = 0; i < widgetsData.Size(); i += 1 )
		{
			if( widgetsData[ i ].wasInitalized == true )
			{
				widget = GetActionWidget( widgetsData[ i ] );
				if( widget == NULL )
				{
					CreateActionWidgetAsync( m_actionsList, widgetsData[ i ] );
				}
				else
				{
					InitializeActionWidget( widget, widgetsData[ i ] );
				}
			}
		}
	}

	public override function Refresh( state : EDeviceStatus )
	{
		SetupWidgets();
		switch( state )
		{
			case EDeviceStatus.ON:
				TurnOn();
			break;
			case EDeviceStatus.OFF:
				TurnOff();
			break;
			case EDeviceStatus.UNPOWERED:
				break;
			case EDeviceStatus.DISABLED:
				break;
			default:
				break;
		}
		super.Refresh( state );
	}

	protected override function RegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.RegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			m_onGlitchingStateChangedListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, this, 'OnGlitchingStateChanged' );
			m_onConfessListener = blackboard.RegisterListenerBool( GetOwner().GetBlackboardDef().IsConfessing, this, 'OnConfess' );
		}
	}

	protected override function UnRegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.UnRegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, m_onGlitchingStateChangedListener );
			blackboard.UnregisterListenerBool( GetOwner().GetBlackboardDef().IsConfessing, m_onConfessListener );
		}
	}

	protected override function GetOwner() : ConfessionBooth
	{
		return ( ( ConfessionBooth )( GetOwnerEntity() ) );
	}

	private override function StartGlitchingScreen( glitchData : GlitchData )
	{
		if( glitchData.state == EGlitchState.DEFAULT )
		{
		}
		else
		{
			ResetConfessionState();
			StopVideo();
			m_defaultUI.SetVisible( false );
			PlayVideo( R"base\movies\misc\distraction_generic.bk2", true, '' );
		}
	}

	private override function StopGlitchingScreen()
	{
		StopVideo();
		m_defaultUI.SetVisible( true );
	}

	public function PlayVideo( videoPath : ResRef, looped : Bool, audioEvent : CName )
	{
		m_mainDisplayWidget.SetVideoPath( videoPath );
		m_mainDisplayWidget.SetLoop( looped );
		if( IsNameValid( audioEvent ) )
		{
			m_mainDisplayWidget.SetAudioEvent( audioEvent );
		}
		m_mainDisplayWidget.Play();
	}

	private function ResetConfessionState()
	{
		if( m_RunningAnimation && m_RunningAnimation.IsPlaying() )
		{
			m_RunningAnimation.Stop();
			m_actionsList.SetVisible( true );
			if( m_isConfessing )
			{
				StopVideo();
			}
		}
	}

	private function PlayConfessMessegeAnimation()
	{
		if( m_RunningAnimation && m_RunningAnimation.IsPlaying() )
		{
			m_RunningAnimation.Stop();
		}
		m_RunningAnimation = PlayLibraryAnimation( 'messegeIn' );
		m_RunningAnimation.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnMessegeAnimFinished' );
		m_actionsList.SetVisible( false );
		m_defaultTextWidget.SetVisible( false );
	}

	protected event OnVideoFinished( target : weak< inkVideo > )
	{
		if( m_isConfessing )
		{
			StopConfessing();
		}
	}

	protected event OnMessegeAnimFinished( e : inkAnimProxy )
	{
		var evt : ConfessionCompletedEvent;
		if( m_RunningAnimation )
		{
			m_RunningAnimation.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnMessegeAnimFinished' );
		}
		m_actionsList.SetVisible( true );
		m_defaultTextWidget.SetVisible( true );
		evt = new ConfessionCompletedEvent;
		GetOwner().QueueEvent( evt );
	}

	public function StopVideo()
	{
		m_mainDisplayWidget.Stop();
	}

	public function TurnOff()
	{
		m_rootWidget.SetVisible( false );
		ResetConfessionState();
	}

	public function TurnOn()
	{
		m_rootWidget.SetVisible( true );
		RequestActionWidgetsUpdate();
	}

	protected event OnConfess( value : Bool )
	{
		if( value )
		{
			StartConfessing();
		}
		else
		{
			StopConfessing();
		}
	}

	private function StartConfessing()
	{
		if( m_isConfessing )
		{
			return;
		}
		m_isConfessing = true;
		m_defaultUI.SetVisible( false );
		if( m_mainDisplayWidget != NULL )
		{
			m_mainDisplayWidget.RegisterToCallback( 'OnVideoFinished', this, 'OnVideoFinished' );
		}
		PlayVideo( R"base\movies\misc\confessional_booth\confessional_s.bk2", false, '' );
	}

	private function StopConfessing()
	{
		if( m_isConfessing )
		{
			m_isConfessing = false;
			GetBlackboard().SetBool( ( ( ConfessionalBlackboardDef )( GetOwner().GetBlackboardDef() ) ).IsConfessing, false );
			StopVideo();
			m_defaultUI.SetVisible( true );
			PlayConfessMessegeAnimation();
			if( m_mainDisplayWidget != NULL )
			{
				m_mainDisplayWidget.UnregisterFromCallback( 'OnVideoFinished', this, 'OnVideoFinished' );
			}
		}
	}

}

