class StopShortGlitchEvent extends Event
{
}

class BillboardDevice extends InteractiveDevice
{
	default m_controllerTypeName = 'BillboardDeviceController';
	protected var m_advUiComponent : IComponent;
	private var m_isShortGlitchActive : Bool;
	private var m_shortGlitchDelayID : DelayID;

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

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_advUiComponent = ( ( IComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ui' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( BillboardDeviceController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		if( ShouldRegisterToHUD() )
		{
			RegisterToHUDManagerByTask( true );
		}
		if( GetDevicePS().IsUsingDeviceAppearence() )
		{
			SetMeshAppearance( 'default_device' );
		}
	}

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

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

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

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

	private function TurnOffScreen()
	{
		if( m_advUiComponent != NULL )
		{
			m_advUiComponent.Toggle( false );
			ToggleLights( false );
		}
	}

	private function TurnOnScreen()
	{
		if( m_advUiComponent != NULL )
		{
			m_advUiComponent.Toggle( true );
			ToggleLights( true );
		}
	}

	protected function ToggleLights( on : Bool )
	{
		var evt : ToggleLightEvent;
		var settEvt : AdvanceChangeLightEvent;
		var settings : array< EditableGameLightSettings >;
		var i : Int32;
		if( !( GetDevicePS().IsUsingLights() ) )
		{
			return;
		}
		evt = new ToggleLightEvent;
		evt.toggle = on;
		QueueEvent( evt );
		if( on )
		{
			settings = GetDevicePS().GetLightsSettings();
			for( i = 0; i < settings.Size(); i += 1 )
			{
				settEvt = new AdvanceChangeLightEvent;
				settEvt.settings = settings[ i ];
				QueueEvent( settEvt );
			}
		}
	}

	protected const override function ShouldRegisterToHUD() : Bool
	{
		if( !( super.ShouldRegisterToHUD() ) )
		{
			return false;
		}
		if( m_wasVisible )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	private const override function GetController() : BillboardDeviceController
	{
		return ( ( BillboardDeviceController )( m_controller ) );
	}

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

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		var evt : AdvertGlitchEvent;
		if( intensity == 0.0 )
		{
			intensity = 1.0;
		}
		evt = new AdvertGlitchEvent;
		evt.SetShouldGlitch( intensity );
		QueueEvent( evt );
		PlaySound( this, GetDevicePS().GetGlitchSFX() );
		UpdateDeviceState();
	}

	protected override function StopGlitching()
	{
		var evt : AdvertGlitchEvent;
		evt = new AdvertGlitchEvent;
		evt.SetShouldGlitch( 0.0 );
		QueueEvent( evt );
		StopSound( this, GetDevicePS().GetGlitchSFX() );
	}

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

	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 event OnPhysicalDestructionEvent( evt : PhysicalDestructionEvent )
	{
		GetDevicePS().ForceDisableDevice();
	}

}

