using UnityEngine;

namespace TD.Core
{
    /// <summary>
    /// Canonical color palette for player slots, used by editor gizmos to color volumes by owner.
    /// </summary>
    /// <remarks>
    /// Color names follow the 9-player map design doc (orange, green, blue, purple, yellow, gray,
    /// teal, olive, dark gray). Red is reserved for enemy units on the minimap, so Player 1 uses
    /// orange instead. The exact RGB values have been tuned for readability when drawn as
    /// translucent gizmos against Unity's default scene-view background. Tweak the constants below
    /// if any color reads poorly in practice.
    ///
    /// The "ErrorPink" color is reserved for diagnostic use: if a volume's owner is
    /// <see cref="PlayerSlot.None"/> (which should never happen on a valid volume), the gizmo
    /// renders in error pink to make the bug obvious.
    /// </remarks>
    public static class PlayerColors
    {
        // Player colors. Hex values are RGB; alpha is set per-gizmo at draw time.
        // Values are tuned to be saturated enough to read against Unity's default scene background.
        private static readonly Color Player1Orange = HexRGB(0xE0, 0x7A, 0x2E);   // orange (red reserved for enemies)
        private static readonly Color Player2Green = HexRGB(0x3A, 0xC0, 0x4A);   // green
        private static readonly Color Player3Blue = HexRGB(0x3A, 0x7A, 0xE0);   // blue
        private static readonly Color Player4Purple = HexRGB(0xA0, 0x4A, 0xC0);   // purple
        private static readonly Color Player5Yellow = HexRGB(0xE0, 0xC8, 0x3A);   // yellow
        private static readonly Color Player6Gray = HexRGB(0xB0, 0xB0, 0xB8);   // gray (slightly cool)
        private static readonly Color Player7Teal = HexRGB(0x3A, 0xC0, 0xB8);   // teal
        private static readonly Color Player8Olive = HexRGB(0x9A, 0x9A, 0x3A);   // olive
        private static readonly Color Player9DarkGray = HexRGB(0x60, 0x60, 0x68);   // dark gray (slightly cool)

        // Non-player colors.
        private static readonly Color GoalGold = HexRGB(0xE0, 0xB0, 0x20);   // gold
        private static readonly Color MapAreaCyan = HexRGB(0xB0, 0xD0, 0xE0);   // muted cyan (background)
        private static readonly Color ErrorPink = HexRGB(0xFF, 0x4A, 0xC8);   // diagnostic

        /// <summary>
        /// Returns the canonical color for a player slot. Returns the diagnostic error pink if
        /// <paramref name="slot"/> is <see cref="PlayerSlot.None"/> or out of range.
        /// </summary>
        /// <param name="slot">The player slot to look up.</param>
        public static Color Get(PlayerSlot slot)
        {
            switch (slot)
            {
                case PlayerSlot.Player1: return Player1Orange;
                case PlayerSlot.Player2: return Player2Green;
                case PlayerSlot.Player3: return Player3Blue;
                case PlayerSlot.Player4: return Player4Purple;
                case PlayerSlot.Player5: return Player5Yellow;
                case PlayerSlot.Player6: return Player6Gray;
                case PlayerSlot.Player7: return Player7Teal;
                case PlayerSlot.Player8: return Player8Olive;
                case PlayerSlot.Player9: return Player9DarkGray;
                case PlayerSlot.None:
                default:
                    return ErrorPink;
            }
        }

        /// <summary>The canonical color used for goal volumes. Not tied to any player.</summary>
        public static Color Goal => GoalGold;

        /// <summary>
        /// The canonical color used for MapAreaVolume gizmos. Muted cyan — distinct from any
        /// player color and from goal gold. Intended to read as background context rather than
        /// as a gameplay element.
        /// </summary>
        public static Color MapArea => MapAreaCyan;

        /// <summary>Diagnostic color used when a volume has an invalid/missing owner.</summary>
        public static Color Error => ErrorPink;

        /// <summary>
        /// Returns a copy of <paramref name="color"/> with its alpha channel replaced by
        /// <paramref name="alpha"/>. Convenience for translucent gizmo fills.
        /// </summary>
        public static Color WithAlpha(Color color, float alpha)
        {
            color.a = alpha;
            return color;
        }

        // Helper: build a Color from 0-255 RGB byte channels (alpha defaults to 1.0).
        private static Color HexRGB(byte r, byte g, byte b)
        {
            return new Color(r / 255f, g / 255f, b / 255f, 1f);
        }
    }
}