class LcdScreenSignInkGameController extends DeviceInkGameControllerBase
{
	protected var m_messegeRecord : weak< ScreenMessageData_Record >;
	protected var m_replaceTextWithCustomNumber : Bool;
	protected var m_customNumber : Int32;
	private var m_onGlitchingStateChangedListener : CallbackHandle;
	private var m_onMessegeChangedListener : CallbackHandle;

	protected override function SetupWidgets()
	{
		if( !( m_isInitialized ) )
		{
			m_rootWidget.SetAnchor( inkEAnchor.Fill );
		}
	}

	protected event OnFillStreetSignData( selector : TweakDBIDSelector )
	{
		var screenRecord : LCDScreen_Record;
		var messageRecord : ScreenMessageData_Record;
		var fluffScreenSelector : LCDScreenSelector;
		if( selector == NULL )
		{
			return false;
		}
		screenRecord = TweakDBInterface.GetLCDScreenRecord( selector.GetRecordID() );
		if( screenRecord )
		{
			fluffScreenSelector = ( ( LCDScreenSelector )( selector ) );
			if( fluffScreenSelector )
			{
				InitializeCustomNumber( fluffScreenSelector.HasCustomNumber(), fluffScreenSelector.GetCustomNumber() );
				messageRecord = TweakDBInterface.GetScreenMessageDataRecord( fluffScreenSelector.GetCustomMessageID() );
			}
			if( messageRecord == NULL )
			{
				messageRecord = screenRecord.Message();
			}
			InitializeMessageRecord( messageRecord );
			ResolveMessegeRecord( m_messegeRecord );
		}
		else
		{
			messageRecord = TweakDBInterface.GetScreenMessageDataRecord( selector.GetRecordID() );
			if( messageRecord != NULL )
			{
				InitializeMessageRecord( messageRecord );
			}
			InitializeMessageRecord( messageRecord );
			ResolveMessegeRecord( m_messegeRecord );
		}
		Refresh( GetOwner().GetDeviceState() );
	}

	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_onMessegeChangedListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().MessegeData, this, 'OnMessegeChanged' );
		}
	}

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

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

	protected function GetMainLogicController() : LcdScreenILogicController
	{
		return ( ( LcdScreenILogicController )( m_rootWidget.GetController() ) );
	}

	protected event OnActionWidgetsUpdate( value : Variant )
	{
		var widgets : array< SActionWidgetPackage >;
		widgets = ( ( array< SActionWidgetPackage > )value );
		UpdateActionWidgets( widgets );
	}

	protected event OnMessegeChanged( value : Variant )
	{
		var record : ScreenMessageData_Record;
		var messageData : ScreenMessageData;
		messageData = ( ( ScreenMessageData )value );
		if( messageData == NULL )
		{
			return false;
		}
		record = messageData.m_messageRecord;
		if( record == NULL )
		{
			return false;
		}
		InitializeCustomNumber( messageData.m_replaceTextWithCustomNumber, messageData.m_customNumber );
		InitializeMessageRecord( record );
		ResolveMessegeRecord( m_messegeRecord );
	}

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

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

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

	public function TurnOn()
	{
		m_rootWidget.SetVisible( true );
		if( m_messegeRecord == NULL )
		{
			InitializeCustomNumber( GetOwner().HasCustomNumber(), GetOwner().GetCustomNumber() );
			InitializeMessageRecord( GetOwner().GetMessageRecord() );
		}
		GetMainLogicController().TurnOn();
	}

	private function InitializeCustomNumber( replaceTextWithCustomNumber : Bool, customNumber : Int32 )
	{
		m_replaceTextWithCustomNumber = replaceTextWithCustomNumber;
		m_customNumber = customNumber;
		GetMainLogicController().InitializeCustomNumber( replaceTextWithCustomNumber, customNumber );
	}

	private function InitializeMessageRecord( messageRecord : ScreenMessageData_Record )
	{
		GetMainLogicController().InitializeMessageRecord( messageRecord );
		m_messegeRecord = messageRecord;
	}

	protected virtual function ResolveMessegeRecord( record : weak< ScreenMessageData_Record > )
	{
		GetMainLogicController().ResolveMessegeRecord( record );
	}

}

