using System;
using System.Threading.Tasks;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Models;
using MCPForUnity.Editor.Services;
using MCPForUnity.Editor.Services.Transport;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace MCPForUnity.Editor.Windows.Components.Connection
{
///
/// Controller for the Connection section of the MCP For Unity editor window.
/// Handles transport protocol, HTTP/stdio configuration, connection status, and health checks.
///
public class McpConnectionSection
{
// Transport protocol enum
private enum TransportProtocol
{
HTTPLocal,
HTTPRemote,
Stdio
}
// UI Elements
private EnumField transportDropdown;
private VisualElement transportMismatchWarning;
private Label transportMismatchText;
private VisualElement httpUrlRow;
private VisualElement httpServerControlRow;
private Foldout manualCommandFoldout;
private VisualElement httpServerCommandSection;
private TextField httpServerCommandField;
private Button copyHttpServerCommandButton;
private Label httpServerCommandHint;
private TextField httpUrlField;
private Button startHttpServerButton;
private VisualElement unitySocketPortRow;
private TextField unityPortField;
private VisualElement statusIndicator;
private Label connectionStatusLabel;
private Button connectionToggleButton;
// API Key UI Elements (for remote-hosted mode)
private VisualElement apiKeyRow;
private TextField apiKeyField;
private Button getApiKeyButton;
private Button clearApiKeyButton;
private string cachedLoginUrl;
private bool connectionToggleInProgress;
private bool httpServerToggleInProgress;
private Task verificationTask;
private string lastHealthStatus;
private double lastLocalServerRunningPollTime;
private bool lastLocalServerRunning;
// Reference to Advanced section for health status updates
private Action onHealthStatusUpdate;
// Events
public event Action OnManualConfigUpdateRequested;
public event Action OnTransportChanged;
public VisualElement Root { get; private set; }
public void SetHealthStatusUpdateCallback(Action callback)
{
onHealthStatusUpdate = callback;
}
public McpConnectionSection(VisualElement root)
{
Root = root;
CacheUIElements();
InitializeUI();
RegisterCallbacks();
}
private void CacheUIElements()
{
transportDropdown = Root.Q("transport-dropdown");
transportMismatchWarning = Root.Q("transport-mismatch-warning");
transportMismatchText = Root.Q