// 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.Editor
{
    internal sealed class WPFMAProjectKindWindow : EditorWindow
    {
        internal static Color Background => WPFMATheme.Shift(new Color(17f / 255f, 20f / 255f, 25f / 255f));
        internal static Color Panel => WPFMATheme.Shift(new Color(30f / 255f, 36f / 255f, 44f / 255f));
        internal static Color PanelHover => WPFMATheme.Shift(new Color(38f / 255f, 46f / 255f, 57f / 255f));
        internal static Color Border => WPFMATheme.Shift(new Color(48f / 255f, 57f / 255f, 69f / 255f));
        internal static Color Accent => WPFMATheme.Shift(new Color(126f / 255f, 182f / 255f, 222f / 255f));
        internal static Color AccentDim => WPFMATheme.Shift(new Color(36f / 255f, 90f / 255f, 131f / 255f));
        internal static Color AccentText => WPFMATheme.Shift(new Color(14f / 255f, 24f / 255f, 33f / 255f));
        internal static Color TitleText => WPFMATheme.Shift(new Color(0.9f, 0.92f, 0.94f));
        internal static Color BodyText => WPFMATheme.Shift(new Color(0.72f, 0.76f, 0.8f));
        internal static Color DimText => WPFMATheme.Shift(new Color(110f / 255f, 124f / 255f, 139f / 255f));

        private static readonly Vector2 WindowSize = new Vector2(540f, 360f);

        private static WPFMAInstaller.ProjectKind picked;

        [NonSerialized] private GUIStyle titleStyle;
        [NonSerialized] private GUIStyle bodyStyle;
        [NonSerialized] private GUIStyle cardTitleStyle;
        [NonSerialized] private GUIStyle cardBodyStyle;
        [NonSerialized] private GUIStyle itemStyle;
        [NonSerialized] private GUIStyle buttonLabelStyle;
        [NonSerialized] private GUIStyle hintStyle;
        [NonSerialized] private GUIStyle laterStyle;

        internal static WPFMAInstaller.ProjectKind Ask()
        {
            picked = WPFMAInstaller.ProjectKind.Undecided;

            var window = CreateInstance<WPFMAProjectKindWindow>();
            window.titleContent = new GUIContent("WPFMA-SDK Setup");
            window.minSize = WindowSize;
            window.maxSize = WindowSize;
            window.wantsMouseMove = true;

            var main = EditorGUIUtility.GetMainWindowPosition();
            window.position = new Rect(
                main.x + (main.width - WindowSize.x) * 0.5f,
                main.y + (main.height - WindowSize.y) * 0.5f,
                WindowSize.x, WindowSize.y);

            window.ShowModal();
            return picked;
        }

        private void OnGUI()
        {
            EnsureStyles();

            var e = Event.current;
            if (e.type == EventType.MouseMove)
                Repaint();

            if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Escape)
            {
                Choose(WPFMAInstaller.ProjectKind.Undecided);
                return;
            }

            float width = position.width;
            EditorGUI.DrawRect(new Rect(0f, 0f, width, position.height), Background);

            GUI.Label(new Rect(24f, 18f, width - 48f, 28f), "Set up this project", titleStyle);
            GUI.Label(new Rect(24f, 48f, width - 48f, 36f),
                "What are you making? VRChat keeps avatar and world projects apart, so WPFMA-SDK only installs what fits.",
                bodyStyle);

            const float top = 96f;
            const float gap = 16f;
            const float cardHeight = 200f;
            float cardWidth = (width - 48f - gap) * 0.5f;

            DrawCard(new Rect(24f, top, cardWidth, cardHeight), WPFMAInstaller.ProjectKind.Avatars,
                "Avatars", "Make and upload avatars.",
                new[] { "VRChat Avatar SDK", "Modular Avatar + NDMF", "Avatar Compressor", "FACS Utilities" },
                "Set up for avatars");

            DrawCard(new Rect(24f + cardWidth + gap, top, cardWidth, cardHeight), WPFMAInstaller.ProjectKind.Worlds,
                "Worlds", "Build and upload worlds.",
                new[] { "VRChat World SDK", "EasyQuestSwitch", "FBX Exporter + Timeline", "Exporter and panel tools" },
                "Set up for worlds");

            float footer = top + cardHeight + 18f;
            GUI.Label(new Rect(24f, footer, width - 48f - 132f, 30f),
                "Not sure yet? Nothing gets installed, and you'll be asked again next time Unity starts.",
                hintStyle);

            var later = new Rect(width - 24f - 120f, footer + 2f, 120f, 26f);
            bool laterHover = later.Contains(e.mousePosition);
            EditorGUI.DrawRect(later, laterHover ? AccentDim : Border);
            EditorGUI.DrawRect(new Rect(later.x + 1f, later.y + 1f, later.width - 2f, later.height - 2f),
                laterHover ? PanelHover : Background);
            GUI.Label(later, "Decide later", laterStyle);
            EditorGUIUtility.AddCursorRect(later, MouseCursor.Link);

            if (e.type == EventType.MouseDown && e.button == 0 && laterHover)
            {
                e.Use();
                Choose(WPFMAInstaller.ProjectKind.Undecided);
            }
        }

        private void DrawCard(Rect rect, WPFMAInstaller.ProjectKind kind, string title, string subtitle,
            string[] items, string action)
        {
            var e = Event.current;
            bool hover = rect.Contains(e.mousePosition);

            EditorGUI.DrawRect(rect, hover ? Accent : Border);
            var inner = new Rect(rect.x + 1f, rect.y + 1f, rect.width - 2f, rect.height - 2f);
            EditorGUI.DrawRect(inner, hover ? PanelHover : Panel);
            EditorGUI.DrawRect(new Rect(inner.x, inner.y, inner.width, 3f), hover ? Accent : AccentDim);

            float x = inner.x + 16f;
            float w = inner.width - 32f;

            GUI.Label(new Rect(x, inner.y + 14f, w, 24f), title, cardTitleStyle);
            GUI.Label(new Rect(x, inner.y + 38f, w, 18f), subtitle, cardBodyStyle);

            float y = inner.y + 66f;
            foreach (var item in items)
            {
                EditorGUI.DrawRect(new Rect(x + 2f, y + 7f, 4f, 4f), hover ? Accent : AccentDim);
                GUI.Label(new Rect(x + 14f, y, w - 14f, 18f), item, itemStyle);
                y += 20f;
            }

            var button = new Rect(x, inner.yMax - 42f, w, 28f);
            EditorGUI.DrawRect(button, hover ? Accent : AccentDim);
            buttonLabelStyle.normal.textColor = hover ? AccentText : TitleText;
            GUI.Label(button, action, buttonLabelStyle);

            EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);

            if (e.type == EventType.MouseDown && e.button == 0 && hover)
            {
                e.Use();
                Choose(kind);
            }
        }

        private void Choose(WPFMAInstaller.ProjectKind kind)
        {
            picked = kind;
            Close();
            GUIUtility.ExitGUI();
        }

        private void EnsureStyles()
        {
            if (titleStyle != null)
                return;

            titleStyle = new GUIStyle(EditorStyles.boldLabel)
            {
                fontSize = 18,
                normal = { textColor = TitleText }
            };

            bodyStyle = new GUIStyle(EditorStyles.label)
            {
                fontSize = 12,
                wordWrap = true,
                normal = { textColor = BodyText }
            };

            cardTitleStyle = new GUIStyle(EditorStyles.boldLabel)
            {
                fontSize = 16,
                normal = { textColor = Accent }
            };

            cardBodyStyle = new GUIStyle(EditorStyles.label)
            {
                fontSize = 11,
                normal = { textColor = DimText }
            };

            itemStyle = new GUIStyle(EditorStyles.label)
            {
                fontSize = 12,
                normal = { textColor = BodyText }
            };

            buttonLabelStyle = new GUIStyle(EditorStyles.boldLabel)
            {
                fontSize = 12,
                alignment = TextAnchor.MiddleCenter
            };

            hintStyle = new GUIStyle(EditorStyles.label)
            {
                fontSize = 11,
                wordWrap = true,
                normal = { textColor = DimText }
            };

            laterStyle = new GUIStyle(EditorStyles.label)
            {
                fontSize = 12,
                alignment = TextAnchor.MiddleCenter,
                normal = { textColor = BodyText }
            };
        }
    }
}
#endif
