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

namespace WPFMA
{
    internal static class WPFMAToolSection
    {
        internal static Color AccentBlue => WPFMATheme.Shift(new Color(126f / 255f, 182f / 255f, 222f / 255f));
        internal static Color ToolbarBackground => WPFMATheme.Shift(new Color(24f / 255f, 29f / 255f, 37f / 255f));
        internal static Color ToolbarEdge => WPFMATheme.Shift(new Color(11f / 255f, 14f / 255f, 18f / 255f));
        internal static Color FaintText => WPFMATheme.Shift(new Color(78f / 255f, 91f / 255f, 105f / 255f));
        internal static Color MetaText => WPFMATheme.Shift(new Color(143f / 255f, 163f / 255f, 181f / 255f));
        internal static readonly Color WarnAmber = new Color(239f / 255f, 159f / 255f, 39f / 255f);
        internal static readonly Color ErrorRed = new Color(226f / 255f, 75f / 255f, 74f / 255f);
        internal static readonly Color OkGreen = new Color(93f / 255f, 202f / 255f, 165f / 255f);

        internal static void Draw(GUIStyle sectionStyle, string title, string detail, string prefKey, Action body)
        {
            EditorGUILayout.Space(10f);
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(8f);
            EditorGUILayout.BeginVertical(sectionStyle);

            try
            {
                if (Strip(title, detail, prefKey))
                    body();
            }
            finally
            {
                EditorGUILayout.EndVertical();
                GUILayout.Space(8f);
                EditorGUILayout.EndHorizontal();
            }
        }

        internal static GUIStyle Hint(Color color)
        {
            return new GUIStyle(EditorStyles.miniLabel)
            {
                wordWrap = true,
                normal = { textColor = color }
            };
        }

        internal static void Reveal(int page, string prefKey)
        {
            EditorPrefs.SetInt("WPFMA.Tools.Page", page);
            if (!string.IsNullOrEmpty(prefKey))
                EditorPrefs.SetBool(prefKey, true);
            WPFMAWindow.OpenAt(WPFMAWindow.Tab.Tools);
        }

        internal static bool AvatarProject
        {
            get { return WPFMA.Editor.WPFMAInstaller.PanelKind != WPFMA.Editor.WPFMAInstaller.ProjectKind.Worlds; }
        }

        private static bool Strip(string title, string detail, string prefKey)
        {
            bool expanded = EditorPrefs.GetBool(prefKey, false);

            var rect = GUILayoutUtility.GetRect(0f, 24f, GUILayout.ExpandWidth(true));
            EditorGUI.DrawRect(rect, ToolbarBackground);
            EditorGUI.DrawRect(new Rect(rect.x, rect.y, rect.width, 1f), ToolbarEdge);

            GUI.Label(new Rect(rect.x + 8f, rect.y, 14f, rect.height), expanded ? "▾" : "▸",
                new GUIStyle(EditorStyles.label) { fontSize = 12, normal = { textColor = AccentBlue } });

            GUI.Label(new Rect(rect.x + 24f, rect.y, 240f, rect.height), title,
                new GUIStyle(EditorStyles.label) { fontSize = 12, normal = { textColor = AccentBlue } });

            GUI.Label(new Rect(rect.xMax - 264f, rect.y, 260f, rect.height), detail ?? string.Empty,
                new GUIStyle(EditorStyles.label)
                {
                    fontSize = 11,
                    alignment = TextAnchor.MiddleRight,
                    normal = { textColor = FaintText }
                });

            EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);

            var evt = Event.current;
            if (evt.type == EventType.MouseDown && evt.button == 0 && rect.Contains(evt.mousePosition))
            {
                expanded = !expanded;
                EditorPrefs.SetBool(prefKey, expanded);
                evt.Use();
            }

            return expanded;
        }
    }
}
#endif
