class DebugRemoteConnectionEvent extends Event
{
}

class BreachViewTimeListener extends TimeDilationListener
{
	var myOwner : weak< GameObject >;

	protected event OnFinished( reason : CName )
	{
		if( ( ( ScriptedPuppet )( myOwner ) ) )
		{
			( ( ScriptedPuppet )( myOwner ) ).OnDiveFinished( reason );
		}
		else if( ( ( AccessPoint )( myOwner ) ) )
		{
			( ( AccessPoint )( myOwner ) ).OnDiveFinished( reason );
		}
	}

	public function SetOwner( owner : GameObject )
	{
		myOwner = owner;
	}

}

class AccessPoint extends InteractiveMasterDevice
{
	default m_controllerTypeName = 'AccessPointController';
	[ category = "Network Visualisation" ]
	private instanceeditable var m_networkName : String;
	default m_networkName = "Local Network 1";
	private var m_isPlayerInBreachView : Bool;
	private var m_isRevealed : Bool;
	private var m_breachViewTimeListener : BreachViewTimeListener;
	private var upload_program_listener_id : Uint32;

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

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

	protected const override function ShouldRegisterToHUD() : Bool
	{
		if( GetDevicePS().IsVirtual() )
		{
			return false;
		}
		else
		{
			return super.ShouldRegisterToHUD();
		}
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		ToggleLights( !( GetDevicePS().IsBreached() ) );
		upload_program_listener_id = GameInstance.GetQuestsSystem( GetGame() ).RegisterEntity( 'upload_program', GetEntityID() );
	}

	protected function ToggleLights( on : Bool )
	{
		var evt : ToggleLightEvent;
		evt = new ToggleLightEvent;
		evt.toggle = on;
		QueueEvent( evt );
	}

	protected event OnGameDetached()
	{
		GameInstance.GetQuestsSystem( GetGame() ).UnregisterEntity( 'upload_program', upload_program_listener_id );
	}

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

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

	protected override function InitiatePersonalLinkWorkspot( puppet : GameObject )
	{
		super.InitiatePersonalLinkWorkspot( puppet );
	}

	protected override function PerformDive( attempt : Int32, isRemote : Bool )
	{
		super.PerformDive( attempt, isRemote );
	}

	protected event OnAccessPointMiniGameStatus( evt : AccessPointMiniGameStatus )
	{
		super.OnAccessPointMiniGameStatus( evt );
		if( evt.minigameState == HackingMinigameState.Succeeded )
		{
			ToggleLights( false );
		}
	}

	protected override function TerminateConnection()
	{
		super.TerminateConnection();
	}

	protected event OnValidate( evt : Validate )
	{
		if( GetDevicePS().IsBreached() )
		{
			ToggleLights( false );
		}
	}

	public const override function CanRevealRemoteActionsWheel() : Bool
	{
		return false;
	}

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

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

	protected function BootModule( module : Int32 )
	{
		GetBlackboard().SetInt( GetAllBlackboardDefs().BackdoorBlackboard.bootModule, module );
		GetBlackboard().FireCallbacks();
	}

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

	public function IsRevealed() : Bool
	{
		return m_isRevealed;
	}

	protected event OnDebugRemoteConnectionEvent( evt : DebugRemoteConnectionEvent )
	{
		GetDevicePS().DebugBreachConnectedDevices();
	}

	protected event OnFactChangedEvent( evt : FactChangedEvent )
	{
		if( evt.GetFactName() == 'upload_program' )
		{
			UploadProgram();
		}
	}

	private function UploadProgram()
	{
		var programID : Int32;
		programID = GameInstance.GetQuestsSystem( GetGame() ).GetFact( 'upload_program' );
		GetDevicePS().UploadProgram( programID );
	}

	protected override function TogglePersonalLink( toggle : Bool, puppet : GameObject )
	{
		super.TogglePersonalLink( toggle, puppet );
	}

	public function OnDiveFinished( reason : CName )
	{
		var action : DeviceAction;
		var player : GameObject;
		if( GetDevicePS().IsPersonalLinkConnected() && GetDevicePS().HasPersonalLinkSlot() )
		{
			player = GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerMainGameObject();
			action = GetDevicePS().GetActionByName( 'TogglePersonalLink', GetDevicePS().GenerateContext( gamedeviceRequestType.Direct, GetInteractionClearance(), player, GetEntityID() ) );
			ExecuteAction( action, player );
			GetDevicePS().RefreshSlaves_Event();
		}
	}

	public const override function IsControllingDevices() : Bool
	{
		return false;
	}

	protected override function EvaluateProximityRevealInteractionLayerState()
	{
		var player : PlayerPuppet;
		player = ( ( PlayerPuppet )( GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerControlledGameObject() ) );
		if( PlayerDevelopmentSystem.GetInstance( player ).IsNewPerkBought( player, gamedataNewPerkType.Intelligence_Left_Milestone_1 ) > 0 )
		{
			EnableProximityRevealInteractionLayer( true );
		}
		else
		{
			super.EvaluateProximityRevealInteractionLayerState();
		}
	}

}

