class DisplayGlass extends InteractiveDevice
{
	default m_controllerTypeName = 'DisplayGlassController';
	protected var m_collider : IPlacedComponent;
	protected var m_mesh : IPlacedComponent;
	protected var m_isDestroyed : Bool;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, 'tv_ui', 'worlduiWidgetComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'glass_dst', 'IPlacedComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'block_vision', 'IPlacedComponent', false );
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_uiComponent = ( ( worlduiWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'tv_ui' ) ) );
		m_collider = ( ( IPlacedComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'block_vision' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( DisplayGlassController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected event OnPersitentStateInitialized( evt : GameAttachedEvent )
	{
		super.OnPersitentStateInitialized( evt );
	}

	protected event OnDetach()
	{
		super.OnDetach();
	}

	protected event OnToggleGlassTint( evt : ToggleGlassTint )
	{
		UpdateGlassState();
	}

	protected event OnToggleGlassTintHack( evt : ToggleGlassTintHack )
	{
		UpdateGlassState();
	}

	protected event OnQuestForceTintGlass( evt : QuestForceTintGlass )
	{
		UpdateGlassState();
	}

	protected event OnQuestForceClearGlass( evt : QuestForceClearGlass )
	{
		UpdateGlassState();
	}

	protected function UpdateGlassState()
	{
		if( !( m_isDestroyed ) )
		{
			ToggleTintGlass( GetDevicePS().IsTinted() );
		}
		UpdateDeviceState();
	}

	protected event OnPhysicalDestructionEvent( evt : PhysicalDestructionEvent )
	{
		if( ( evt.levelOfDestruction == 0 ) && !( m_isDestroyed ) )
		{
			m_collider.Toggle( false );
			m_isDestroyed = true;
			GetDevicePS().ForceDisableDevice();
		}
	}

	private function ToggleTintGlass( on : Bool )
	{
		var ps : DisplayGlassControllerPS;
		var componentName : CName;
		if( !( m_isDestroyed ) )
		{
			ps = GetDevicePS();
			if( ps.UsesAppearances() )
			{
				if( m_mesh )
				{
					componentName = 'glass_dst';
				}
				if( on )
				{
					SetMeshAppearance( ps.GetTintAppearance(), componentName );
				}
				else
				{
					SetMeshAppearance( ps.GetClearAppearance(), componentName );
				}
			}
			else
			{
				m_uiComponent.Toggle( on );
			}
			m_collider.Toggle( on );
		}
	}

	protected override function TurnOnDevice()
	{
		ToggleTintGlass( GetDevicePS().IsTinted() );
	}

	protected override function TurnOffDevice()
	{
		ToggleTintGlass( false );
	}

	protected override function CutPower()
	{
		ToggleTintGlass( false );
	}

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

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

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

}

