using UnityEditor;
using UnityEngine;

namespace UnityAssistant
{
    public static class BridgeMenu
    {
        private const string Root = "Gumo/";

        [MenuItem(Root + "Status…", priority = 0)]
        public static void OpenStatus() => BridgeStatusWindow.Open();

        [MenuItem(Root + "Start Bridge", priority = 20)]
        public static void StartBridge() => BridgeServer.Start();

        [MenuItem(Root + "Start Bridge", true)]
        public static bool ValidateStart() => !BridgeServer.IsRunning;

        [MenuItem(Root + "Stop Bridge", priority = 21)]
        public static void StopBridge() => BridgeServer.Stop();

        [MenuItem(Root + "Stop Bridge", true)]
        public static bool ValidateStop() => BridgeServer.IsRunning;

        [MenuItem(Root + "Restart Bridge", priority = 22)]
        public static void RestartBridge() => BridgeServer.Restart();

        [MenuItem(Root + "Auto-start on Editor Launch", priority = 40)]
        public static void ToggleAutoStart()
        {
            BridgePrefs.AutoStart = !BridgePrefs.AutoStart;
        }

        [MenuItem(Root + "Auto-start on Editor Launch", true)]
        public static bool ValidateAutoStart()
        {
            Menu.SetChecked(Root + "Auto-start on Editor Launch", BridgePrefs.AutoStart);
            return true;
        }

        [MenuItem(Root + "Reindex Project", priority = 60)]
        public static void Reindex() => IndexerTools.IndexProject(new System.Collections.Generic.Dictionary<string, object> { ["force"] = true });

        [MenuItem(Root + "Copy Bridge URL", priority = 80)]
        public static void CopyUrl()
        {
            EditorGUIUtility.systemCopyBuffer = $"http://127.0.0.1:{BridgeServer.PortNumber}/";
            Debug.Log($"[Gumo] URL copied to clipboard.");
        }
    }
}
