// 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 UnityEditor;
using UnityEngine;

namespace WPFMA
{
    /// <summary>
    /// The package checker as a tool: check any .unitypackage or this project for DLLs and token loggers, see
    /// what's in quarantine, and the switches for checking files as they come in.
    /// </summary>
    internal static class WPFMASafetySection
    {
        private const string PrefKey = "WPFMA.Tools.PackageCheckerExpanded";

        private static SafetyReport lastProject;

        internal static void Draw(GUIStyle sectionStyle)
        {
            int held = WPFMASafety.Quarantined().Count;
            string detail = held > 0 ? held + (held == 1 ? " file in quarantine" : " files in quarantine")
                : lastProject != null ? "project: " + lastProject.Badge
                : WPFMASafety.GuardOn ? "watching new files" : "not watching new files";
            WPFMAToolSection.Draw(sectionStyle, "Package checker", detail, PrefKey, () => Body(held));
        }

        private static void Body(int held)
        {
            EditorGUILayout.LabelField(
                "Looks inside .unitypackages and your project for DLLs and token loggers: scripts that read your Discord token or " +
                "browser passwords, send data to webhooks, start with Windows, or hide what they run.",
                WPFMAToolSection.Hint(WPFMAToolSection.MetaText));
            EditorGUILayout.Space(4f);

            using (new EditorGUILayout.HorizontalScope())
            {
                if (GUILayout.Button("Check a package...", GUILayout.Height(24f)))
                {
                    string picked = EditorUtility.OpenFilePanel("Check a package", string.Empty, "unitypackage");
                    if (!string.IsNullOrEmpty(picked))
                    {
                        var report = WPFMASafety.CheckNow(picked);
                        WPFMASafetyWindow.ShowPackage(report, choice => WPFMASafety.ImportChoice(report, picked, true, choice));
                    }
                    GUIUtility.ExitGUI();
                }
                if (GUILayout.Button("Check this project", GUILayout.Height(24f)))
                {
                    lastProject = WPFMASafety.CheckProject();
                    WPFMASafetyWindow.ShowProject(lastProject);
                    GUIUtility.ExitGUI();
                }
            }
            EditorGUILayout.LabelField("Checking a package doesn't import it. You choose after seeing what's inside.",
                WPFMAToolSection.Hint(WPFMAToolSection.FaintText));

            EditorGUILayout.Space(4f);
            EditorGUILayout.LabelField("New files", EditorStyles.boldLabel);
            bool guard = WPFMASafety.GuardOn;
            bool editedGuard = EditorGUILayout.ToggleLeft(new GUIContent("Check new scripts and DLLs as they come in",
                "Also catches packages you double-click and files you copy in. Anything that looks like a token logger is moved to WPFMA-Quarantine for you to accept or deny."), guard);
            if (editedGuard != guard)
                WPFMASafety.GuardOn = editedGuard;
            using (new EditorGUI.DisabledScope(!editedGuard))
            {
                bool hold = WPFMASafety.HoldRisky;
                bool editedHold = EditorGUILayout.ToggleLeft(new GUIContent("Hold new DLLs until I say they're OK",
                    "New DLLs (and scripts that do risky things) wait in WPFMA-Quarantine until you accept or deny them. Dangerous files are always held."), hold);
                if (editedHold != hold)
                    WPFMASafety.HoldRisky = editedHold;
            }

            EditorGUILayout.Space(4f);
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField(held == 0 ? "Nothing in quarantine." : held + (held == 1 ? " file" : " files") + " in quarantine.",
                    WPFMAToolSection.Hint(held > 0 ? WPFMAToolSection.WarnAmber : WPFMAToolSection.FaintText));
                if (GUILayout.Button("Open", EditorStyles.miniButton, GUILayout.Width(60f)))
                {
                    WPFMASafetyWindow.ShowQuarantine();
                    GUIUtility.ExitGUI();
                }
            }

            int trusted = WPFMASafety.TrustedCount;
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField(trusted == 0 ? "No files trusted yet." : trusted + (trusted == 1 ? " file" : " files") + " you said were OK.",
                    WPFMAToolSection.Hint(WPFMAToolSection.FaintText));
                using (new EditorGUI.DisabledScope(trusted == 0))
                {
                    if (GUILayout.Button("Forget", EditorStyles.miniButton, GUILayout.Width(60f)) &&
                        EditorUtility.DisplayDialog("Forget trusted files?", "DLLs you said were OK will be checked and held back again next time they arrive.", "Forget", "Cancel"))
                        WPFMASafety.ForgetTrusted();
                }
            }

            EditorGUILayout.Space(2f);
            EditorGUILayout.LabelField("It catches the common tricks, but no checker can promise a file is safe. Only use packages from people you trust.",
                WPFMAToolSection.Hint(WPFMAToolSection.FaintText));
        }
    }
}
#endif
