using UnityEditor;
using UnityEngine;

namespace Darkmatter.Libs.PlayerPrefs.Editor
{
    [InitializeOnLoad]
    internal static class ProtectedPlayerPrefsSetupBootstrap
    {
        private const string WindowShownSessionKey =
            "Darkmatter.Libs.PlayerPrefs.Editor.ProtectedPlayerPrefsSetupBootstrap.WindowShown";

        static ProtectedPlayerPrefsSetupBootstrap()
        {
            EditorApplication.delayCall += TryOpenSetupWindow;
        }

        private static void TryOpenSetupWindow()
        {
            if (Application.isBatchMode || EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            if (SessionState.GetBool(WindowShownSessionKey, false))
            {
                return;
            }

            SessionState.SetBool(WindowShownSessionKey, true);

            if (!ProtectedPlayerPrefsSettingsUtility.NeedsSetup())
            {
                return;
            }

            ProtectedPlayerPrefsGettingStartedWindow.OpenWindow();
        }
    }
}
