class PerkTrainingControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#79367";
	default m_shouldScannerShowStatus = false;
	default m_hasPersonalLinkSlot = true;
	default m_shouldSkipNetrunnerMinigame = true;
	[ customEditor = "TweakDBGroupInheritance;Interactions.None" ]
	private editable var m_interactionTweakDBID : TweakDBID;
	private editable var m_loopTime : Float;
	private editable var m_jackinStartTime : Float;
	protected editable var m_isCorePerk : Bool;
	private persistent var m_perkGranted : Bool;
	private persistent var m_wasDetected : Bool;

	public function WasDetected() : Bool
	{
		return m_wasDetected;
	}

	public function SetDeviceAsDetected()
	{
		m_wasDetected = true;
	}

	public const function IsPerkGranted() : Bool
	{
		return m_perkGranted;
	}

	public function GetLoopTime() : Float
	{
		return m_loopTime;
	}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		var action : TogglePersonalLink;
		if( ShouldExposePersonalLinkAction() )
		{
			action = ActionTogglePersonalLink( context.processInitiatorObject );
			action.SetDurationValue( m_jackinStartTime );
			action.CreateInteraction( , , m_interactionTweakDBID );
			actions.PushBack( action );
			return true;
		}
		return false;
	}

	protected override function IsPersonalLinkActionIllegal() : Bool
	{
		return false;
	}

	protected const override function ShouldExposePersonalLinkAction() : Bool
	{
		return !( IsPerkGranted() ) && GetPersonalLinkStatus() == EPersonalLinkConnectionStatus.NOT_CONNECTED;
	}

	protected override function ResolvePersonalLinkConnection( evt : TogglePersonalLink, abortOperations : Bool )
	{
		if( IsPersonalLinkConnected() )
		{
			StartConnectionLoopCountdown( evt );
		}
	}

	private function StartConnectionLoopCountdown( evt : TogglePersonalLink )
	{
		var connectionEndedEvent : ConnectionEndedEvent;
		connectionEndedEvent = new ConnectionEndedEvent;
		connectionEndedEvent.SetTogglePersonalLinkAction( evt );
		QueuePSEventWithDelay( GetID(), GetClassName(), connectionEndedEvent, m_loopTime );
	}

	private function OnConnectionEnded( evt : ConnectionEndedEvent ) : EntityNotificationType
	{
		TryGrantPerk();
		DisconnectPersonalLink( evt.GetTogglePersonalLinkAction() );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function TryGrantPerk()
	{
		var playerDevelopmentData : PlayerDevelopmentData;
		var currentEspionageLevel : Int32;
		if( !( IsPerkGranted() ) )
		{
			playerDevelopmentData = PlayerDevelopmentSystem.GetData( GetPlayer( GetGameInstance() ) );
			currentEspionageLevel = playerDevelopmentData.GetProficiencyLevel( gamedataProficiencyType.Espionage );
			if( m_isCorePerk )
			{
				playerDevelopmentData.SetLevel( gamedataProficiencyType.Espionage, currentEspionageLevel + 3, telemetryLevelGainReason.Gameplay );
			}
			else
			{
				playerDevelopmentData.SetLevel( gamedataProficiencyType.Espionage, currentEspionageLevel + 1, telemetryLevelGainReason.Gameplay );
			}
			m_perkGranted = true;
		}
	}

}

class ConnectionEndedEvent extends Event
{
	private var m_togglePersonalLinkAction : TogglePersonalLink;

	public function GetTogglePersonalLinkAction() : TogglePersonalLink
	{
		return m_togglePersonalLinkAction;
	}

	public function SetTogglePersonalLinkAction( togglePersonalLinkAction : TogglePersonalLink )
	{
		m_togglePersonalLinkAction = togglePersonalLinkAction;
	}

}

