class Intercom extends InteractiveDevice
{
	default m_controllerTypeName = 'IntercomController';
	private var m_isShortGlitchActive : Bool;
	private var m_shortGlitchDelayID : DelayID;
	protected var m_dialStartSound : CName;
	default m_dialStartSound = 'dev_intercom_dial_start';
	protected var m_dialStopSound : CName;
	default m_dialStopSound = 'dev_intercom_dial_stop';
	protected var m_distractionStartSound : CName;
	default m_distractionStartSound = 'dev_intercom_distraction_start';
	protected var m_distractionStopSound : CName;
	default m_distractionStopSound = 'dev_intercom_distraction_stop';
	protected var m_answeredSound : CName;
	default m_answeredSound = 'dev_intercom_answered';

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

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_uiComponent = ( ( worlduiWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'terminalui' ) ) );
		super.OnTakeControl( ri );
		m_controller = ( ( IntercomController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

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

	protected override function OnVisibilityChanged()
	{
		if( IsReadyForUI() && IsUIdirty() )
		{
			RefreshUI();
		}
	}

	protected event OnQuestPickUpCall( evt : QuestPickUpCall )
	{
		UpdateDisplayUI( IntercomStatus.TALKING );
		PlaySoundEvent( this, m_answeredSound );
		UpdateDeviceState();
	}

	protected event OnQuestHangUpCall( evt : QuestHangUpCall )
	{
		UpdateDisplayUI( IntercomStatus.CALL_ENDED );
		PlaySoundEvent( this, m_dialStopSound );
		UpdateDeviceState();
	}

	protected event OnStartCall( evt : StartCall )
	{
		if( evt.IsStarted() )
		{
			UpdateDisplayUI( IntercomStatus.CALLING );
			PlaySoundEvent( this, m_dialStartSound );
		}
		else
		{
			UpdateDisplayUI( IntercomStatus.CALL_MISSED );
			PlaySoundEvent( this, m_dialStopSound );
		}
		UpdateDeviceState();
	}

	protected event OnResetIntercom( evt : DelayEvent )
	{
		UpdateDisplayUI( IntercomStatus.DEFAULT );
		UpdateDeviceState();
	}

	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( GetAllBlackboardDefs().TVDeviceBlackboard.GlitchData, glitchData );
		GetBlackboard().FireCallbacks();
		PlaySoundEvent( this, m_distractionStartSound );
		GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.Start, 'hack_fx' );
	}

	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( GetAllBlackboardDefs().TVDeviceBlackboard.GlitchData, glitchData );
		GetBlackboard().FireCallbacks();
		PlaySoundEvent( this, m_distractionStopSound );
	}

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

	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 OnHitEvent( hit : gameHitEvent )
	{
		super.OnHitEvent( hit );
		StartShortGlitch();
	}

	protected function UpdateDisplayUI( status : IntercomStatus )
	{
		GetBlackboard().SetVariant( GetAllBlackboardDefs().IntercomBlackboard.Status, status, true );
		GetBlackboard().FireCallbacks();
		m_isUIdirty = true;
	}

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

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

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

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

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

}

