// Asset Naming Manager — a coloured dot on offending assets, drawn in the Project window itself.
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace AssetNamingManager
{
    /// <summary>
    /// Draws a small severity dot next to any asset that appeared in the last scan, right in the
    /// Project browser. It is the cheapest way to make a report visible where people actually work —
    /// nobody keeps a report window open, but everybody has the Project window open.
    /// </summary>
    [InitializeOnLoad]
    public static class NamingManagerProjectOverlay
    {
        static readonly Dictionary<string, NamingSeverity> Worst =
            new Dictionary<string, NamingSeverity>(System.StringComparer.Ordinal);

        static NamingManagerProjectOverlay()
        {
            EditorApplication.projectWindowItemOnGUI -= OnItemGUI;
            EditorApplication.projectWindowItemOnGUI += OnItemGUI;
        }

        /// <summary>Feeds the overlay from a fresh scan. Pass null to clear it.</summary>
        public static void SetReport(ScanReport report)
        {
            Worst.Clear();

            if (report != null)
            {
                foreach (var v in report.violations)
                {
                    if (string.IsNullOrEmpty(v.guid)) continue;

                    NamingSeverity current;
                    if (!Worst.TryGetValue(v.guid, out current) || v.severity > current)
                        Worst[v.guid] = v.severity;
                }
            }

            EditorApplication.RepaintProjectWindow();
        }

        /// <summary>Forgets everything. Called when the user turns the overlay off.</summary>
        public static void Clear()
        {
            SetReport(null);
        }

        /// <summary>How many assets currently carry a dot.</summary>
        public static int MarkedCount
        {
            get { return Worst.Count; }
        }

        static void OnItemGUI(string guid, Rect rect)
        {
            if (Worst.Count == 0) return;
            if (!NamingManagerSettings.ShowProjectOverlay) return;

            NamingSeverity severity;
            if (!Worst.TryGetValue(guid, out severity)) return;

            // The Project window has a one-column list mode and a grid mode with a much taller row.
            bool grid = rect.height > 20f;
            const float size = 6f;

            var dot = grid
                ? new Rect(rect.x + 4f, rect.y + 4f, size, size)
                : new Rect(rect.xMax - size - 2f, rect.y + (rect.height - size) * 0.5f, size, size);

            var previous = GUI.color;
            GUI.color = ColorFor(severity);
            GUI.DrawTexture(dot, EditorGUIUtility.whiteTexture);
            GUI.color = previous;
        }

        // The overlay draws with IMGUI, so it cannot read the window's USS. These mirror
        // NamingManagerTheme's fill values; keep them in step if you change the palette.
        static Color ColorFor(NamingSeverity severity)
        {
            bool dark = EditorGUIUtility.isProSkin;

            switch (severity)
            {
                case NamingSeverity.Error:
                    return dark ? new Color(0.902f, 0.361f, 0.361f) : new Color(0.631f, 0.137f, 0.137f);
                case NamingSeverity.Warning:
                    return dark ? new Color(0.949f, 0.690f, 0.180f) : new Color(0.435f, 0.302f, 0.035f);
                default:
                    return dark ? new Color(0.353f, 0.588f, 0.918f) : new Color(0.157f, 0.329f, 0.580f);
            }
        }
    }
}
