using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
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 httpUrlRow;
private VisualElement httpServerCommandSection;
private TextField httpServerCommandField;
private Button copyHttpServerCommandButton;
private Label httpServerCommandHint;
private TextField httpUrlField;
private Button startHttpServerButton;
private Button stopHttpServerButton;
private VisualElement unitySocketPortRow;
private TextField unityPortField;
private VisualElement statusIndicator;
private Label connectionStatusLabel;
private Button connectionToggleButton;
private VisualElement healthIndicator;
private Label healthStatusLabel;
private VisualElement healthRow;
private Button testConnectionButton;
private bool connectionToggleInProgress;
private bool httpServerToggleInProgress;
private Task verificationTask;
private string lastHealthStatus;
private double lastLocalServerRunningPollTime;
private bool lastLocalServerRunning;
// Health status constants
private const string HealthStatusUnknown = "Unknown";
private const string HealthStatusHealthy = "Healthy";
private const string HealthStatusPingFailed = "Ping Failed";
private const string HealthStatusUnhealthy = "Unhealthy";
// Events
public event Action OnManualConfigUpdateRequested;
public event Action OnTransportChanged;
public VisualElement Root { get; private set; }
public McpConnectionSection(VisualElement root)
{
Root = root;
CacheUIElements();
InitializeUI();
RegisterCallbacks();
}
private void CacheUIElements()
{
transportDropdown = Root.Q("transport-dropdown");
httpUrlRow = Root.Q("http-url-row");
httpServerCommandSection = Root.Q("http-server-command-section");
httpServerCommandField = Root.Q("http-server-command");
copyHttpServerCommandButton = Root.Q