class LcdScreenILogicController extends inkLogicController
{
	protected editable var m_defaultUI : inkWidgetRef;
	protected editable var m_mainDisplayWidget : inkVideoRef;
	protected editable var m_messegeWidget : inkTextRef;
	protected editable var m_backgroundWidget : inkImageRef;
	protected var m_messegeRecord : weak< ScreenMessageData_Record >;
	protected var m_replaceTextWithCustomNumber : Bool;
	protected var m_customNumber : Int32;

	protected event OnInitialize()
	{
		GetRootWidget().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 );
		}
	}

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

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

	public function TurnOff()
	{
		inkVideoRef.UnregisterFromCallback( m_mainDisplayWidget, 'OnVideoFinished', this, 'OnVideoFinished' );
	}

	public function TurnOn()
	{
		ResolveMessegeRecord( m_messegeRecord );
	}

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

	public function InitializeMessageRecord( messageRecord : ScreenMessageData_Record )
	{
		var groupRecord : weak< ScreenMessagesList_Record >;
		var count : Int32;
		var rand : Int32;
		if( messageRecord == m_messegeRecord )
		{
			return;
		}
		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;
	}

	public 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 ) )
			{
				inkTextRef.SetFontFamily( m_messegeWidget, fontPath, fontstyle );
			}
			else if( IsNameValid( fontstyle ) )
			{
				inkTextRef.SetFontStyle( m_messegeWidget, fontstyle );
			}
			if( IsNameValid( verticalAlignment ) )
			{
				inkTextRef.SetVerticalAlignment( m_messegeWidget, inkTextRef.GetVerticalAlignmentEnumValue( m_messegeWidget, verticalAlignment ) );
			}
			if( IsNameValid( horizontalAlignment ) )
			{
				inkTextRef.SetHorizontalAlignment( m_messegeWidget, inkTextRef.GetHorizontalAlignmentEnumValue( m_messegeWidget, horizontalAlignment ) );
			}
			inkTextRef.EnableAutoScroll( m_messegeWidget, record.AutoScroll() );
			inkTextRef.SetFontSize( m_messegeWidget, record.FontSize() );
			if( m_replaceTextWithCustomNumber )
			{
				inkTextRef.SetText( m_messegeWidget, IntToString( m_customNumber ) );
			}
			else
			{
				inkTextRef.SetLocalizedTextScript( m_messegeWidget, record.LocalizedDescription() );
			}
			inkTextRef.UpdateMargin( m_messegeWidget, record.LeftMargin(), record.TopMargin(), record.RightMargin(), record.BottomMargin() );
			inkTextRef.SetTintColor( m_messegeWidget, GetColorFromArray( record.TextColor() ) );
			inkTextRef.SetScrollTextSpeed( m_messegeWidget, record.ScrollSpeed() );
			inkImageRef.SetTintColor( m_backgroundWidget, GetColorFromArray( record.BackgroundColor() ) );
			inkImageRef.SetOpacity( m_backgroundWidget, record.BackgroundOpacity() );
			SetTexture( m_backgroundWidget, record.BackgroundTextureID() );
		}
	}

	private 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;
	}

}

