class LcdScreen extends InteractiveDevice
{
	default m_controllerTypeName = 'LcdScreenController';
	protected var m_isShortGlitchActive : Bool;
	protected var m_shortGlitchDelayID : DelayID;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, 'ui', 'IWorldWidgetComponent', false );
		super.OnRequestComponents( ri );
	}

	public override function ResavePersistentData( ps : PersistentState ) : Bool
	{
		return false;
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_uiComponent = ( ( IWorldWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ui' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( LcdScreenController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		if( IsUIdirty() && m_isInsideLogicArea )
		{
			RefreshUI();
		}
	}

	protected override function CreateBlackboard()
	{
		m_blackboard = IBlackboard.Create( GetAllBlackboardDefs().LcdScreenBlackBoard );
	}

	public const override function GetBlackboardDef() : LcdScreenBlackBoardDef
	{
		return GetDevicePS().GetBlackboardDef();
	}

	protected const override function GetController() : LcdScreenController
	{
		return ( ( LcdScreenController )( m_controller ) );
	}

	public const override function GetDevicePS() : LcdScreenControllerPS
	{
		return GetController().GetPS();
	}

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		var glitchData : GlitchData;
		var evt : AdvertGlitchEvent;
		glitchData.state = glitchState;
		glitchData.intensity = intensity;
		if( intensity == 0.0 )
		{
			intensity = 1.0;
		}
		evt = new AdvertGlitchEvent;
		evt.SetShouldGlitch( intensity );
		QueueEvent( evt );
		GetBlackboard().SetVariant( GetBlackboardDef().GlitchData, glitchData, true );
		GetBlackboard().FireCallbacks();
	}

	protected override function StopGlitching()
	{
		var glitchData : GlitchData;
		var evt : AdvertGlitchEvent;
		evt = new AdvertGlitchEvent;
		evt.SetShouldGlitch( 0.0 );
		QueueEvent( evt );
		glitchData.state = EGlitchState.NONE;
		GetBlackboard().SetVariant( GetBlackboardDef().GlitchData, glitchData );
		GetBlackboard().FireCallbacks();
	}

	protected event OnHitEvent( hit : gameHitEvent )
	{
		super.OnHitEvent( hit );
		StartShortGlitch();
	}

	private function StartShortGlitch()
	{
		var evt : StopShortGlitchEvent;
		if( GetDevicePS().IsGlitching() )
		{
			return;
		}
		if( !( m_isShortGlitchActive ) )
		{
			evt = new StopShortGlitchEvent;
			StartGlitching( EGlitchState.DEFAULT, 1.0 );
			m_shortGlitchDelayID = GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, evt, 0.25 );
			m_isShortGlitchActive = true;
		}
	}

	protected event OnStopShortGlitch( evt : StopShortGlitchEvent )
	{
		m_isShortGlitchActive = false;
		if( !( GetDevicePS().IsGlitching() ) )
		{
			StopGlitching();
		}
	}

	protected override function TurnOnDevice()
	{
		super.TurnOnDevice();
		TurnOnScreen();
	}

	protected override function TurnOffDevice()
	{
		super.TurnOffDevice();
		TurnOffScreen();
	}

	protected override function CutPower()
	{
		super.CutPower();
		TurnOffScreen();
	}

	private function TurnOffScreen()
	{
		m_uiComponent.Toggle( false );
	}

	private function TurnOnScreen()
	{
		m_uiComponent.Toggle( true );
	}

	public const override function DeterminGameplayRole() : EGameplayRole
	{
		return EGameplayRole.Distract;
	}

	protected override function ApplyActiveStatusEffect( target : EntityID, statusEffect : TweakDBID )
	{
		if( IsActiveStatusEffectValid() && GetDevicePS().IsGlitching() )
		{
			GameInstance.GetStatusEffectSystem( GetGame() ).ApplyStatusEffect( target, statusEffect );
		}
	}

	protected override function UploadActiveProgramOnNPC( targetID : EntityID )
	{
		var evt : ExecutePuppetActionEvent;
		if( IsActiveProgramToUploadOnNPCValid() && GetDevicePS().IsGlitching() )
		{
			evt = new ExecutePuppetActionEvent;
			evt.actionID = GetActiveProgramToUploadOnNPC();
			QueueEventForEntityID( targetID, evt );
		}
	}

	public const function GetMessageRecord() : ScreenMessageData_Record
	{
		var id : TweakDBID;
		id = GetDevicePS().GetMessageRecordID();
		if( !( TDBID.IsValid( id ) ) )
		{
			return NULL;
		}
		return TweakDBInterface.GetScreenMessageDataRecord( id );
	}

	public const function HasCustomNumber() : Bool
	{
		return GetDevicePS().HasCustomNumber();
	}

	public const function GetCustomNumber() : Int32
	{
		return GetDevicePS().GetCustomNumber();
	}

	private function UpdateMessageRecordUI( messageData : ScreenMessageData )
	{
		if( messageData == NULL )
		{
			return;
		}
		if( !( messageData.m_messageRecord ) )
		{
			return;
		}
		if( GetDevicePS().IsON() )
		{
			GetBlackboard().SetVariant( GetBlackboardDef().MessegeData, messageData );
			GetBlackboard().FireCallbacks();
		}
	}

	protected event OnSetMessageRecord( evt : SetMessageRecordEvent )
	{
		var messageData : ScreenMessageData;
		messageData = new ScreenMessageData;
		messageData.m_messageRecord = TweakDBInterface.GetScreenMessageDataRecord( evt.m_messageRecordID );
		messageData.m_replaceTextWithCustomNumber = evt.m_replaceTextWithCustomNumber;
		messageData.m_customNumber = evt.m_customNumber;
		UpdateMessageRecordUI( messageData );
	}

}

