class BackdoorInkGameController extends MasterDeviceInkGameControllerBase
{
	private editable var m_IdleGroup : inkWidgetRef;
	private editable var m_ConnectedGroup : inkWidgetRef;
	private editable var m_IntroAnimationName : CName;
	private editable var m_IdleAnimationName : CName;
	private editable var m_GlitchAnimationName : CName;
	private var m_RunningAnimation : inkAnimProxy;
	private var m_onGlitchingListener : CallbackHandle;
	private var m_onIsInDefaultStateListener : CallbackHandle;
	private var m_onShutdownModuleListener : CallbackHandle;
	private var m_onBootModuleListener : CallbackHandle;

	protected override function RegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.RegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			m_onGlitchingListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, this, 'OnGlitching' );
			m_onIsInDefaultStateListener = blackboard.RegisterListenerBool( GetOwner().GetBlackboardDef().isInDefaultState, this, 'OnIsInDefaultState' );
			m_onShutdownModuleListener = blackboard.RegisterListenerInt( GetOwner().GetBlackboardDef().shutdownModule, this, 'OnShutdownModule' );
			m_onBootModuleListener = blackboard.RegisterListenerInt( GetOwner().GetBlackboardDef().bootModule, this, 'OnBootModule' );
		}
	}

	protected override function UnRegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.UnRegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, m_onGlitchingListener );
			blackboard.UnregisterListenerBool( GetOwner().GetBlackboardDef().isInDefaultState, m_onIsInDefaultStateListener );
			blackboard.UnregisterListenerInt( GetOwner().GetBlackboardDef().shutdownModule, m_onShutdownModuleListener );
			blackboard.UnregisterListenerInt( GetOwner().GetBlackboardDef().bootModule, m_onBootModuleListener );
		}
	}

	protected override function UpdateThumbnailWidgets( const widgetsData : ref< array< SThumbnailWidgetPackage > > )
	{
		super.UpdateThumbnailWidgets( widgetsData );
	}

	protected override function UpdateDeviceWidgets( const widgetsData : ref< array< SDeviceWidgetPackage > > )
	{
		super.UpdateDeviceWidgets( widgetsData );
	}

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

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

	protected function TurnOn()
	{
		GetRootWidget().SetVisible( true );
		inkWidgetRef.SetVisible( m_IdleGroup, true );
		inkWidgetRef.SetVisible( m_ConnectedGroup, false );
		PlayIntroAnimation();
	}

	protected function TurnOff()
	{
		GetRootWidget().SetVisible( false );
		PlayAnimation( '' );
	}

	private function PlayIntroAnimation()
	{
		if( m_RunningAnimation && m_RunningAnimation.IsPlaying() )
		{
			m_RunningAnimation.Stop();
		}
		m_RunningAnimation = PlayLibraryAnimation( m_IntroAnimationName );
		m_RunningAnimation.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnIntroFinished' );
	}

	protected event OnIntroFinished( e : inkAnimProxy )
	{
		if( m_RunningAnimation )
		{
			m_RunningAnimation.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnIntroFinished' );
		}
		PlayAnimation( m_IdleAnimationName );
	}

	protected function PlayAnimation( animName : CName )
	{
		var playbackOptions : inkAnimOptions;
		if( m_RunningAnimation && m_RunningAnimation.IsPlaying() )
		{
			m_RunningAnimation.Stop();
		}
		if( IsNameValid( animName ) )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_RunningAnimation = PlayLibraryAnimation( animName, playbackOptions );
		}
	}

	protected virtual function StartGlitching()
	{
		PlayAnimation( m_GlitchAnimationName );
		inkWidgetRef.SetVisible( m_IdleGroup, false );
		inkWidgetRef.SetVisible( m_ConnectedGroup, true );
	}

	private function StopGlitching()
	{
		PlayAnimation( m_IdleAnimationName );
		inkWidgetRef.SetVisible( m_IdleGroup, true );
		inkWidgetRef.SetVisible( m_ConnectedGroup, false );
	}

	protected event OnGlitching( value : Bool )
	{
		if( value )
		{
			StartGlitching();
		}
		else
		{
			StopGlitching();
		}
	}

	protected event OnIsInDefaultState( value : Bool )
	{
		if( value )
		{
		}
		else
		{
			EnableHackedGroup();
		}
	}

	protected event OnShutdownModule( value : Int32 )
	{
		ShutdownModule( value );
	}

	protected event OnBootModule( value : Int32 )
	{
		BootModule( value );
	}

	protected virtual function ShutdownModule( module : Int32 ) {}

	protected virtual function BootModule( module : Int32 ) {}

	protected virtual function EnableHackedGroup() {}
}

