// WPFMA-SDK needs Unity 2022.3 (VRChat's version); on older Unity this file is skipped and WPFMAUnityVersionNotice explains why.
#if UNITY_2022_3_OR_NEWER
using System;
using System.IO;
using UnityEditor;

namespace WPFMA
{
    [InitializeOnLoad]
    internal static class WPFMAVrcfuryPopupPatch
    {
        private static bool initialized;

        static WPFMAVrcfuryPopupPatch()
        {
            EditorApplication.projectChanged += EnsurePopupSuppression;
            AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
            EditorApplication.delayCall += EnsurePopupSuppression;
        }

        private static void OnAfterAssemblyReload()
        {
            if (initialized)
                return;

            initialized = true;
            EditorApplication.delayCall += EnsurePopupSuppression;
        }

        private static void EnsurePopupSuppression()
        {
            if (initialized)
            {
            }

            EnsureUnityScriptUpdateConsentSuppressed();
            EnsureVrcfuryPopupSuppressed();
        }

        private static void EnsureUnityScriptUpdateConsentSuppressed()
        {
            try
            {
                var keys = new[]
                {
                    "ApiUpdater.DoNotAsk",
                    "ApiUpdater.DoNotAskToUpdateScripts",
                    "ApiUpdater.MovedFromExtractor.DoNotAsk",
                    "ApiUpdater.MovedFromExtractor.DoNotAskToUpdateScripts",
                    "UnityEditorInternal.APIUpdater.DoNotAsk",
                    "UnityEditorInternal.APIUpdater.MovedFromExtractor.DoNotAsk"
                };

                foreach (var key in keys)
                {
                    try
                    {
                        EditorPrefs.SetBool(key, true);
                        EditorPrefs.SetString(key + ".LastValue", "true");
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
        }

        private static void EnsureVrcfuryPopupSuppressed()
        {
            try
            {
                var detectorPath = Path.Combine("Packages", "com.vrcfury.vrcfury", "Editor-Avatars", "BadInstallDetector.cs");
                if (!File.Exists(detectorPath))
                    return;

                var current = File.ReadAllText(detectorPath);
                if (!current.Contains("DisplayDialog") && !current.Contains("Please download and import"))
                    return;

                var patched = @"using System.IO;
using System.Linq;
using UnityEditor;
using VF.Utils;

namespace VF {
    internal static class BadInstallDetector {
        [VFInit]
        private static void Init() {
            // WPFMA disables the upstream local-install popup for managed installs.
        }
    }
}";

                File.WriteAllText(detectorPath, patched);
            }
            catch
            {
            }
        }
    }
}
#endif
