using UnityEditor;
using UnityEngine;

namespace Darkmatter.Libs.PlayerPrefs.Editor
{
    public class ProtectedPlayerPrefsGettingStartedWindow : EditorWindow
    {
        private string _passphrase = string.Empty;
        private string _confirmPassphrase = string.Empty;
        private Vector2 _scrollPosition;

        [MenuItem("Tools/Darkmatter/Protected PlayerPrefs/Getting Started")]
        public static void OpenWindow()
        {
            var window = GetWindow<ProtectedPlayerPrefsGettingStartedWindow>(
                true,
                "Protected PlayerPrefs Setup",
                true);
            window.minSize = new Vector2(520f, 520f);
        }

        private void OnGUI()
        {
            _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);

            EditorGUILayout.LabelField("Protected PlayerPrefs Setup", EditorStyles.boldLabel);
            EditorGUILayout.Space(4);
            EditorGUILayout.HelpBox(
                "Set a project-specific hash password before shipping the package. The raw password is not stored - only its SHA-256 hash is saved in the settings asset.",
                MessageType.Info);

            DrawChecklist();
            DrawSettingsSummary();
            DrawPassphraseForm();
            DrawActions();

            EditorGUILayout.EndScrollView();
        }

        private static void DrawChecklist()
        {
            EditorGUILayout.LabelField("Before You Save", EditorStyles.boldLabel);
            EditorGUILayout.LabelField("1. Use a unique project-specific password.");
            EditorGUILayout.LabelField("2. Store it in your team password manager.");
            EditorGUILayout.LabelField("3. Changing it later will make existing protected values unreadable.");
            EditorGUILayout.Space(8);
        }

        private static void DrawSettingsSummary()
        {
            EditorGUILayout.LabelField("Settings Asset", EditorStyles.boldLabel);

            var settingsAsset = ProtectedPlayerPrefsSettingsUtility.GetOrCreateSettingsAsset();
            using (new EditorGUI.DisabledScope(true))
            {
                EditorGUILayout.ObjectField(
                    "Asset",
                    settingsAsset,
                    typeof(ProtectedPlayerPrefsSettings),
                    false);
                EditorGUILayout.TextField("Path", ProtectedPlayerPrefsSettingsUtility.GetSettingsPath());
            }

            var status = settingsAsset.HasConfiguredHash
                ? "Configured"
                : "Hash password missing";
            EditorGUILayout.LabelField("Status", status);
            EditorGUILayout.Space(8);
        }

        private void DrawPassphraseForm()
        {
            EditorGUILayout.LabelField("Hash Password", EditorStyles.boldLabel);
            _passphrase = EditorGUILayout.PasswordField("Password", _passphrase);
            _confirmPassphrase = EditorGUILayout.PasswordField("Confirm", _confirmPassphrase);

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Generate Strong Password"))
            {
                _passphrase = ProtectedPlayerPrefsSettingsUtility.GenerateRandomPassphrase();
                _confirmPassphrase = _passphrase;
                GUI.FocusControl(null);
            }

            if (GUILayout.Button("Clear"))
            {
                _passphrase = string.Empty;
                _confirmPassphrase = string.Empty;
                GUI.FocusControl(null);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space(8);
        }

        private void DrawActions()
        {
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Save Hash Password", GUILayout.Height(32f)))
            {
                SaveHashPassword();
            }

            if (GUILayout.Button("Open PlayerPrefs Editor", GUILayout.Height(32f)))
            {
                PlayerPrefsEditorWindow.Open();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Ping Settings Asset"))
            {
                var settingsAsset = ProtectedPlayerPrefsSettingsUtility.GetOrCreateSettingsAsset();
                EditorGUIUtility.PingObject(settingsAsset);
            }

            EditorGUI.BeginDisabledGroup(!ProtectedPlayerPrefsSettingsUtility.HasDocumentationPdf());
            if (GUILayout.Button("Open Package Docs"))
            {
                ProtectedPlayerPrefsSettingsUtility.OpenDocumentationPdf();
            }
            EditorGUI.EndDisabledGroup();
            EditorGUILayout.EndHorizontal();
        }

        private void SaveHashPassword()
        {
            if (string.IsNullOrWhiteSpace(_passphrase))
            {
                EditorUtility.DisplayDialog(
                    "Hash Password Required",
                    "Enter a password before saving the Protected PlayerPrefs settings.",
                    "OK");
                return;
            }

            if (_passphrase.Length < 12)
            {
                EditorUtility.DisplayDialog(
                    "Password Too Short",
                    "Use at least 12 characters so the generated hash is project-specific and hard to guess.",
                    "OK");
                return;
            }

            if (_passphrase != _confirmPassphrase)
            {
                EditorUtility.DisplayDialog(
                    "Passwords Do Not Match",
                    "The confirmation value does not match the password.",
                    "OK");
                return;
            }

            ProtectedPlayerPrefsSettingsUtility.SavePassphrase(_passphrase);
            ShowNotification(new GUIContent("Protected PlayerPrefs hash saved."));
            Close();
        }
    }
}
