using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Common
{
    public static class ColorExtensions
    {
        /// <summary>
        /// 将十六进制颜色字符串转换为 System.Windows.Media.Color 对象
        /// 支持格式: #RGB, #RGBA, #RRGGBB, #RRGGBBAA
        /// </summary>
        /// <param name="hex">十六进制颜色字符串</param>
        /// <returns>转换后的 Color 对象</returns>
        /// <exception cref="ArgumentException">当输入格式无效时抛出</exception>
        public static Color FromHex(this string hex)
        {
            if (string.IsNullOrWhiteSpace(hex))
                throw new ArgumentException("颜色字符串不能为空", nameof(hex));

            // 移除前缀（如果有）
            hex = hex.TrimStart('#');
            if (hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                hex = hex.Substring(2);

            // 根据长度解析不同格式
            switch (hex.Length)
            {
                case 3: // RGB
                    return Color.FromRgb(
                        ParseComponent(hex[0], hex[0]),
                        ParseComponent(hex[1], hex[1]),
                        ParseComponent(hex[2], hex[2]));

                case 4: // RGBA
                    return Color.FromArgb(
                        ParseComponent(hex[3], hex[3]),
                        ParseComponent(hex[0], hex[0]),
                        ParseComponent(hex[1], hex[1]),
                        ParseComponent(hex[2], hex[2]));

                case 6: // RRGGBB
                    return Color.FromRgb(
                        ParseComponent(hex[0], hex[1]),
                        ParseComponent(hex[2], hex[3]),
                        ParseComponent(hex[4], hex[5]));

                case 8: // RRGGBBAA
                    return Color.FromArgb(
                        ParseComponent(hex[6], hex[7]),
                        ParseComponent(hex[0], hex[1]),
                        ParseComponent(hex[2], hex[3]),
                        ParseComponent(hex[4], hex[5]));

                default:
                    throw new ArgumentException($"无效的十六进制颜色格式: {hex}", nameof(hex));
            }
        }

        /// <summary>
        /// 解析两个十六进制字符为一个字节值
        /// </summary>
        private static byte ParseComponent(char c1, char c2)
        {
            return byte.Parse(new string(new[] { c1, c2 }), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 将 Color 对象转换为十六进制字符串表示（默认格式: #RRGGBB）
        /// </summary>
        /// <param name="color">要转换的颜色</param>
        /// <param name="includeAlpha">是否包含透明度通道</param>
        /// <returns>十六进制颜色字符串</returns>
        public static string ToHexString(this Color color, bool includeAlpha = false)
        {
            return includeAlpha
                ? $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}"
                : $"#{color.R:X2}{color.G:X2}{color.B:X2}";
        }
    }
}
