using UnityEditor;
using UnityEngine;

namespace MajacloudSdk.Editor
{
    /// <summary>Tools ▸ Majacloud ▸ Setup — paste keys, test the connection, open the sample.</summary>
    public sealed class MajacloudSetupWindow : EditorWindow
    {
        private readonly MajacloudSetupGui _gui = new MajacloudSetupGui();
        private Vector2 _scroll;

        [MenuItem("Tools/Majacloud/Setup", priority = 1)]
        public static void Open()
        {
            var window = GetWindow<MajacloudSetupWindow>(true, "Majacloud Setup");
            window.minSize = new Vector2(480, 440);
            window.Show();
        }

        private void OnEnable() => _gui.OnEnable();

        private void OnDisable() => _gui.OnDisable();

        private void OnGUI()
        {
            _scroll = EditorGUILayout.BeginScrollView(_scroll);
            EditorGUILayout.LabelField("Majacloud " + Majacloud.Version, EditorStyles.largeLabel);
            _gui.Draw();
            EditorGUILayout.EndScrollView();
        }
    }

    /// <summary>Pressing Play in a demo scene before setup opens the Setup window instead of leaving the player guessing.</summary>
    [InitializeOnLoad]
    internal static class MajacloudSetupReminder
    {
        static MajacloudSetupReminder() => EditorApplication.playModeStateChanged += OnPlayModeChanged;

        private static void OnPlayModeChanged(PlayModeStateChange state)
        {
            if (state != PlayModeStateChange.EnteredPlayMode || MajacloudSetupGui.LoadSettings() != null) return;
            var scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().path;
            if (!scene.StartsWith(MajacloudEditorPaths.SamplesRoot + "/")) return;
            Debug.LogWarning("[Majacloud] Not set up yet: paste your Base URL, Project ID and Client Key in the Setup window, press Test connection, then press Play again.");
            MajacloudSetupWindow.Open();
        }
    }

    internal static class MajacloudSettingsProvider
    {
        private static MajacloudSetupGui _gui;

        [SettingsProvider]
        public static SettingsProvider Create()
        {
            return new SettingsProvider("Project/Majacloud", SettingsScope.Project)
            {
                label = "Majacloud",
                guiHandler = _ =>
                {
                    _gui = _gui ?? new MajacloudSetupGui();
                    using (new EditorGUILayout.VerticalScope(GUILayout.MaxWidth(620))) _gui.Draw();
                },
                deactivateHandler = () => _gui?.OnDisable(),
                keywords = new[] { "majacloud", "leaderboard", "remote config", "cloud save", "backend" },
            };
        }
    }

    [CustomEditor(typeof(MajacloudSettings))]
    internal sealed class MajacloudSettingsEditor : UnityEditor.Editor
    {
        public override void OnInspectorGUI()
        {
            EditorGUILayout.HelpBox("Edit these values from Tools ▸ Majacloud ▸ Setup to test the connection and store the Remote Config public key.", MessageType.Info);
            if (GUILayout.Button("Open Majacloud Setup")) MajacloudSetupWindow.Open();
            EditorGUILayout.Space(6);
            DrawDefaultInspector();
        }
    }
}
