using System.IO;
using UnityEditor;
using UnityEngine;

namespace Darkmatter.Libs.PlayerPrefs.Editor
{
    internal static class ProtectedPlayerPrefsSettingsUtility
    {
        internal const string DefaultSettingsDirectory = "Assets/Darkmatter/Data/Settings/Persistance/Resources";
        internal const string DefaultSettingsPath = DefaultSettingsDirectory + "/ProtectedPlayerPrefsSettings.asset";
        internal const string DocumentationPdfPath =
            "Assets/Darkmatter/Code/Libs/PlayerPrefs/Docs/ProtectedPlayerPrefsToolkit_Documentation.pdf";

        internal static ProtectedPlayerPrefsSettings FindSettingsAsset()
        {
            var guids = AssetDatabase.FindAssets("t:ProtectedPlayerPrefsSettings");
            if (guids.Length == 0)
            {
                return null;
            }

            var path = AssetDatabase.GUIDToAssetPath(guids[0]);
            return AssetDatabase.LoadAssetAtPath<ProtectedPlayerPrefsSettings>(path);
        }

        internal static ProtectedPlayerPrefsSettings GetOrCreateSettingsAsset()
        {
            var existing = FindSettingsAsset();
            if (existing != null)
            {
                return existing;
            }

            Directory.CreateDirectory(DefaultSettingsDirectory);

            var asset = ScriptableObject.CreateInstance<ProtectedPlayerPrefsSettings>();
            AssetDatabase.CreateAsset(asset, DefaultSettingsPath);
            AssetDatabase.SaveAssets();
            return asset;
        }

        internal static bool NeedsSetup()
        {
            var settings = GetOrCreateSettingsAsset();
            return !settings.HasConfiguredHash;
        }

        internal static void SavePassphrase(string passphrase)
        {
            var settings = GetOrCreateSettingsAsset();
            var hashedPassphrase = ProtectedPlayerPrefs.ComputePassphraseHash(passphrase);
            settings.SetHashedPassphrase(hashedPassphrase);
            EditorUtility.SetDirty(settings);
            AssetDatabase.SaveAssets();
            ProtectedPlayerPrefs.InitWithHash(hashedPassphrase);
        }

        internal static string GetSettingsPath()
        {
            var settings = FindSettingsAsset();
            return settings == null ? DefaultSettingsPath : AssetDatabase.GetAssetPath(settings);
        }

        internal static bool HasDocumentationPdf()
        {
            return File.Exists(Path.GetFullPath(DocumentationPdfPath));
        }

        internal static void OpenDocumentationPdf()
        {
            var absolutePath = Path.GetFullPath(DocumentationPdfPath);
            if (!File.Exists(absolutePath))
            {
                EditorUtility.DisplayDialog(
                    "Documentation Missing",
                    "The package PDF has not been generated yet.",
                    "OK");
                return;
            }

            EditorUtility.OpenWithDefaultApp(absolutePath);
        }

        internal static string GenerateRandomPassphrase()
        {
            return $"{System.Guid.NewGuid():N}{System.Guid.NewGuid():N}";
        }
    }
}
