class IntercomInkGameController extends DeviceInkGameControllerBase
{
	[ category = "Widget Refs" ]
	private editable var m_actionsList : inkWidgetRef;
	private var m_mainDisplayWidget : weak< inkVideo >;
	private var m_buttonRef : weak< CallActionWidgetController >;
	private var m_state : IntercomStatus;
	private var m_onUpdateStatusListener : CallbackHandle;
	private var m_onGlitchingStateChangedListener : CallbackHandle;

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

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

	protected override function RegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.RegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			m_onUpdateStatusListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().Status, this, 'OnUpdateStatus' );
			m_onGlitchingStateChangedListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, this, 'OnGlitchingStateChanged' );
		}
	}

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

	protected event OnUpdateStatus( value : Variant )
	{
		m_state = ( ( IntercomStatus )value );
		switch( m_state )
		{
			case IntercomStatus.CALLING:
				m_buttonRef.CallStarted();
			break;
			case IntercomStatus.TALKING:
				m_buttonRef.CallPickedUp();
			break;
			case IntercomStatus.CALL_ENDED:
				m_buttonRef.CallEnded();
			break;
			case IntercomStatus.CALL_MISSED:
				m_buttonRef.CallMissed();
			break;
			default:
				m_buttonRef.ResetIntercom();
			Refresh( GetOwner().GetDeviceState() );
			break;
		}
	}

	public override function Refresh( state : EDeviceStatus )
	{
		SetupWidgets();
		if( m_state == IntercomStatus.DEFAULT )
		{
			RequestActionWidgetsUpdate();
		}
		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 );
	}

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

	protected event OnActionWidgetSpawned( widget : inkWidget, userData : IScriptable )
	{
		widget.SetAnchor( inkEAnchor.Centered );
		super.OnActionWidgetSpawned( widget, userData );
		m_buttonRef = ( ( CallActionWidgetController )( widget.GetController() ) );
	}

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

	private override function StopGlitchingScreen()
	{
		StopVideo();
	}

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

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

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

	private function TurnOn()
	{
		m_rootWidget.SetVisible( true );
	}

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

}

