class DropPoint extends BasicDistractionDevice
{
	default m_controllerTypeName = 'DropPointController';
	private var m_isShortGlitchActive : Bool;
	private var m_shortGlitchDelayID : DelayID;
	private var m_mappinID : NewMappinID;
	private var m_mappinInSystem : Bool;

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

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

	protected override function ResolveGameplayState()
	{
		var request : AttachVendorRequest;
		var vendorID : TweakDBID;
		super.ResolveGameplayState();
		if( IsUIdirty() && m_isInsideLogicArea )
		{
			RefreshUI();
		}
		vendorID = TDBID.Create( ( ( DropPointControllerPS )( GetDevicePS() ) ).GetVendorRecordPath() );
		if( TDBID.IsValid( vendorID ) )
		{
			request = new AttachVendorRequest;
			request.owner = this;
			request.vendorID = vendorID;
			MarketSystem.GetInstance( GetGame() ).QueueRequest( request );
		}
	}

	protected event OnGameAttached()
	{
		super.OnGameAttached();
		if( GetDevicePS().IsON() )
		{
			HandleMappinRregistration( true );
		}
	}

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

	protected override function DeactivateDevice()
	{
		TurnOffDevice();
		HandleMappinRregistration( false );
	}

	protected override function CutPower()
	{
		TurnOffScreen();
		HandleMappinRregistration( false );
	}

	protected override function TurnOffDevice()
	{
		TurnOffScreen();
		HandleMappinRregistration( false );
	}

	protected override function TurnOnDevice()
	{
		TurnOnScreen();
		HandleMappinRregistration( true );
	}

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

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

	protected event OnUpdateDropPointEvent( evt : UpdateDropPointEvent )
	{
		m_isUIdirty = true;
		if( evt.isEnabled && GetDevicePS().IsON() )
		{
			HandleMappinVisibility( true );
		}
		else
		{
			HandleMappinVisibility( false );
		}
		RefreshUI();
		DetermineInteractionStateByTask();
	}

	public const function GetDropPointSystem() : DropPointSystem
	{
		return ( ( DropPointSystem )( GameInstance.GetScriptableSystemsContainer( GetGame() ).Get( 'DropPointSystem' ) ) );
	}

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

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

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

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

	public const override function IsDropPoint() : Bool
	{
		return true;
	}

	private function IsMappinRegistered() : Bool
	{
		var invalidID : NewMappinID;
		return m_mappinID != invalidID;
	}

	private function RegisterMappin()
	{
		var mappinData : MappinData;
		if( !( IsMappinRegistered() ) )
		{
			mappinData.mappinType = T"Mappins.DropPointDynamicMappin";
			mappinData.variant = gamedataMappinVariant.ServicePointDropPointVariant;
			mappinData.visibleThroughWalls = false;
			m_mappinID = GetMappinSystem().RegisterMappinWithObject( mappinData, this, 'poi_mappin' );
		}
	}

	private function UnregisterMappin()
	{
		var invalidID : NewMappinID;
		if( IsMappinRegistered() )
		{
			GetMappinSystem().UnregisterMappin( m_mappinID );
			m_mappinID = invalidID;
		}
	}

	private function RegisterDropPointMappinInSystem( optional force : Bool )
	{
		var request : RegisterDropPointMappinRequest;
		if( !( force ) && m_mappinInSystem )
		{
			return;
		}
		request = new RegisterDropPointMappinRequest;
		request.ownerID = GetEntityID();
		request.position = GetWorldPosition();
		request.trackingAlternativeMappinID = m_mappinID;
		GetDropPointSystem().QueueRequest( request );
		m_mappinInSystem = true;
	}

	private function UnregisterDropPointMappinInSystem( optional force : Bool )
	{
		var request : UnregisterDropPointMappinRequest;
		if( !( force ) && !( m_mappinInSystem ) )
		{
			return;
		}
		request = new UnregisterDropPointMappinRequest;
		request.ownerID = GetEntityID();
		request.removeFromSystem = true;
		GetDropPointSystem().QueueRequest( request );
		m_mappinInSystem = false;
	}

	private function HandleMappinRregistration( show : Bool, optional force : Bool )
	{
		if( show )
		{
			if( GetDropPointSystem().IsEnabled() )
			{
				RegisterMappin();
			}
			RegisterDropPointMappinInSystem( force );
		}
		else
		{
			UnregisterMappin();
			UnregisterDropPointMappinInSystem( force );
		}
	}

	private function HandleMappinVisibility( show : Bool )
	{
		if( show )
		{
			if( GetDropPointSystem().IsEnabled() )
			{
				RegisterMappin();
			}
			RegisterDropPointMappinInSystem( false );
		}
		else
		{
			UnregisterMappin();
		}
	}

	private function GetMappinSystem() : MappinSystem
	{
		return GameInstance.GetMappinSystem( GetGame() );
	}

}

