class ArcadeMachineInkGameController extends DeviceInkGameControllerBase
{
	private var m_defaultUI : weak< inkCanvas >;
	private var m_mainDisplayWidget : weak< inkVideo >;
	private var m_counterWidget : weak< inkText >;
	private var m_onGlitchingStateChangedListener : CallbackHandle;

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

	protected override function SetupWidgets()
	{
		if( !( m_isInitialized ) )
		{
			m_defaultUI = ( ( inkCanvas )( GetWidget( 'default_ui' ) ) );
			m_mainDisplayWidget = ( ( inkVideo )( GetWidget( 'main_display' ) ) );
			m_rootWidget.SetVisible( false );
		}
	}

	public override function UpdateActionWidgets( const widgetsData : ref< array< SActionWidgetPackage > > ) {}

	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' );
		}
	}

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

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

	protected event OnTimeToDepartChanged( value : String )
	{
		m_counterWidget.SetTextDirect( value );
	}

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

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

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

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

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

	public function TurnOn()
	{
		m_rootWidget.SetVisible( true );
		PlayVideo( GetOwner().GetArcadeGameVideo(), true, '' );
	}

}

