class LcdScreenInkGameController extends DeviceInkGameControllerBase
{
	protected var m_defaultUI : weak< inkCanvas >;
	protected var m_mainDisplayWidget : weak< inkVideo >;
	protected var m_messegeWidget : weak< inkText >;
	protected var m_backgroundWidget : weak< inkLeafWidget >;
	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 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 );
		}
		else
		{
			messageRecord = TweakDBInterface.GetScreenMessageDataRecord( selector.GetRecordID() );
			if( messageRecord != NULL )
			{
				InitializeMessageRecord( messageRecord );
			}
		}
		Refresh( GetOwner().GetDeviceState() );
	}

	protected override function SetupWidgets()
	{
		if( !( m_isInitialized ) )
		{
			m_defaultUI = ( ( inkCanvas )( GetWidget( 'default_ui' ) ) );
			m_messegeWidget = ( ( inkText )( GetWidget( 'default_ui/messege_text' ) ) );
			m_mainDisplayWidget = ( ( inkVideo )( GetWidget( 'main_display' ) ) );
			m_backgroundWidget = ( ( inkLeafWidget )( GetWidget( 'default_ui/messege_background' ) ) );
			m_rootWidget.SetAnchor( inkEAnchor.Fill );
		}
	}

	protected event OnUninitialize()
	{
		super.OnUninitialize();
		StopVideo();
	}

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

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

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

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

	private function InitializeMessageRecord( messageRecord : ScreenMessageData_Record )
	{
		var groupRecord : weak< ScreenMessagesList_Record >;
		var count : Int32;
		var rand : Int32;
		if( messageRecord != NULL )
		{
			groupRecord = messageRecord.MessageGroup();
			if( groupRecord != NULL )
			{
				count = groupRecord.GetMessagesCount();
				if( count > 0 )
				{
					rand = RandRange( 0, count );
					messageRecord = groupRecord.GetMessagesItem( rand );
				}
			}
		}
		m_messegeRecord = messageRecord;
	}

	protected virtual function ResolveMessegeRecord( record : weak< ScreenMessageData_Record > )
	{
		var fontPath : String;
		var fontstyle : CName;
		var verticalAlignment : CName;
		var horizontalAlignment : CName;
		if( record != NULL )
		{
			fontPath = record.FontPath();
			fontstyle = record.FontStyle();
			verticalAlignment = record.TextVerticalAlignment();
			horizontalAlignment = record.TextHorizontalAlignment();
			if( IsStringValid( fontPath ) )
			{
				m_messegeWidget.SetFontFamily( fontPath, fontstyle );
			}
			else if( IsNameValid( fontstyle ) )
			{
				m_messegeWidget.SetFontStyle( fontstyle );
			}
			if( IsNameValid( verticalAlignment ) )
			{
				m_messegeWidget.SetVerticalAlignment( m_messegeWidget.GetVerticalAlignmentEnumValue( verticalAlignment ) );
			}
			if( IsNameValid( horizontalAlignment ) )
			{
				m_messegeWidget.SetHorizontalAlignment( m_messegeWidget.GetHorizontalAlignmentEnumValue( horizontalAlignment ) );
			}
			m_messegeWidget.EnableAutoScroll( record.AutoScroll() );
			m_messegeWidget.SetFontSize( record.FontSize() );
			if( m_replaceTextWithCustomNumber )
			{
				m_messegeWidget.SetText( IntToString( m_customNumber ) );
			}
			else
			{
				m_messegeWidget.SetLocalizedTextScript( record.LocalizedDescription() );
			}
			m_messegeWidget.UpdateMargin( record.LeftMargin(), record.TopMargin(), record.RightMargin(), record.BottomMargin() );
			m_messegeWidget.SetTintColor( GetColorFromArray( record.TextColor() ) );
			m_messegeWidget.SetScrollTextSpeed( record.ScrollSpeed() );
			m_backgroundWidget.SetTintColor( GetColorFromArray( record.BackgroundColor() ) );
			m_backgroundWidget.SetOpacity( record.BackgroundOpacity() );
			SetBackgroundTexture( ( ( inkImage )( m_backgroundWidget ) ), record.BackgroundTextureID() );
		}
	}

	protected function GetColorFromArray( const calorArray : ref< array< Int32 > > ) : Color
	{
		var i : Int32;
		var color : Color;
		for( i = 0; i < calorArray.Size(); i += 1 )
		{
			if( i == 0 )
			{
				color.Red = ( ( Uint8 )( calorArray[ i ] ) );
			}
			else if( i == 1 )
			{
				color.Green = ( ( Uint8 )( calorArray[ i ] ) );
			}
			else if( i == 2 )
			{
				color.Blue = ( ( Uint8 )( calorArray[ i ] ) );
			}
			else if( i == 3 )
			{
				color.Alpha = ( ( Uint8 )( calorArray[ i ] ) );
			}
		}
		return color;
	}

	private function SetBackgroundTexture( imageWidget : weak< inkImage >, textureID : TweakDBID )
	{
		if( ( imageWidget != NULL ) && TDBID.IsValid( textureID ) )
		{
			InkImageUtils.RequestSetImage( this, imageWidget, textureID );
		}
	}

	private function SetBackgroundTexture( imageWidget : weak< inkImage >, textureRecord : weak< UIIcon_Record > )
	{
		if( ( imageWidget != NULL ) && ( textureRecord != NULL ) )
		{
			imageWidget.SetAtlasResource( textureRecord.AtlasResourcePath() );
			imageWidget.SetTexturePart( textureRecord.AtlasPartName() );
		}
	}

	private function SetBackgroundTexture( imageWidgetRef : inkImageRef, textureRecord : weak< UIIcon_Record > )
	{
		if( inkImageRef.IsValid( imageWidgetRef ) && ( textureRecord != NULL ) )
		{
			inkImageRef.SetAtlasResource( imageWidgetRef, textureRecord.AtlasResourcePath() );
			inkImageRef.SetTexturePart( imageWidgetRef, textureRecord.AtlasPartName() );
		}
	}

}

