//! VML color parsing.

use crate::docx::error::Result;
use crate::docx::model::*;

/// Parse a VML color value (§14.1.2.1): `#RRGGBB`, `RRGGBB` hex, or named color.
pub(super) fn parse_color(s: &str) -> Result<VmlColor> {
    let hex = s.strip_prefix('#').unwrap_or(s);
    if hex.len() == 6 && hex.bytes().all(|b| b.is_ascii_hexdigit()) {
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0);
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0);
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0);
        return Ok(VmlColor::Rgb(r, g, b));
    }
    match parse_named_color(s) {
        Some(named) => Ok(VmlColor::Named(named)),
        None => Err(crate::docx::error::ParseError::InvalidAttributeValue {
            attr: "fillcolor".into(),
            value: s.into(),
            reason: "unrecognized VML color name per §14.1.2.1".into(),
        }),
    }
}

fn parse_named_color(s: &str) -> Option<VmlNamedColor> {
    // Case-insensitive match per CSS spec.
    Some(match s.to_ascii_lowercase().as_str() {
        // CSS2.1 standard colors.
        "black" => VmlNamedColor::Black,
        "silver" => VmlNamedColor::Silver,
        "gray" | "grey" => VmlNamedColor::Gray,
        "white" => VmlNamedColor::White,
        "maroon" => VmlNamedColor::Maroon,
        "red" => VmlNamedColor::Red,
        "purple" => VmlNamedColor::Purple,
        "fuchsia" => VmlNamedColor::Fuchsia,
        "green" => VmlNamedColor::Green,
        "lime" => VmlNamedColor::Lime,
        "olive" => VmlNamedColor::Olive,
        "yellow" => VmlNamedColor::Yellow,
        "navy" => VmlNamedColor::Navy,
        "blue" => VmlNamedColor::Blue,
        "teal" => VmlNamedColor::Teal,
        "aqua" => VmlNamedColor::Aqua,
        "orange" => VmlNamedColor::Orange,
        // SVG/CSS3 extended colors.
        "aliceblue" => VmlNamedColor::AliceBlue,
        "antiquewhite" => VmlNamedColor::AntiqueWhite,
        "beige" => VmlNamedColor::Beige,
        "bisque" => VmlNamedColor::Bisque,
        "blanchedalmond" => VmlNamedColor::BlanchedAlmond,
        "blueviolet" => VmlNamedColor::BlueViolet,
        "brown" => VmlNamedColor::Brown,
        "burlywood" => VmlNamedColor::BurlyWood,
        "cadetblue" => VmlNamedColor::CadetBlue,
        "chartreuse" => VmlNamedColor::Chartreuse,
        "chocolate" => VmlNamedColor::Chocolate,
        "coral" => VmlNamedColor::Coral,
        "cornflowerblue" => VmlNamedColor::CornflowerBlue,
        "cornsilk" => VmlNamedColor::Cornsilk,
        "crimson" => VmlNamedColor::Crimson,
        "cyan" => VmlNamedColor::Cyan,
        "darkblue" => VmlNamedColor::DarkBlue,
        "darkcyan" => VmlNamedColor::DarkCyan,
        "darkgoldenrod" => VmlNamedColor::DarkGoldenrod,
        "darkgray" | "darkgrey" => VmlNamedColor::DarkGray,
        "darkgreen" => VmlNamedColor::DarkGreen,
        "darkkhaki" => VmlNamedColor::DarkKhaki,
        "darkmagenta" => VmlNamedColor::DarkMagenta,
        "darkolivegreen" => VmlNamedColor::DarkOliveGreen,
        "darkorange" => VmlNamedColor::DarkOrange,
        "darkorchid" => VmlNamedColor::DarkOrchid,
        "darkred" => VmlNamedColor::DarkRed,
        "darksalmon" => VmlNamedColor::DarkSalmon,
        "darkseagreen" => VmlNamedColor::DarkSeaGreen,
        "darkslateblue" => VmlNamedColor::DarkSlateBlue,
        "darkslategray" | "darkslategrey" => VmlNamedColor::DarkSlateGray,
        "darkturquoise" => VmlNamedColor::DarkTurquoise,
        "darkviolet" => VmlNamedColor::DarkViolet,
        "deeppink" => VmlNamedColor::DeepPink,
        "deepskyblue" => VmlNamedColor::DeepSkyBlue,
        "dimgray" | "dimgrey" => VmlNamedColor::DimGray,
        "dodgerblue" => VmlNamedColor::DodgerBlue,
        "firebrick" => VmlNamedColor::Firebrick,
        "floralwhite" => VmlNamedColor::FloralWhite,
        "forestgreen" => VmlNamedColor::ForestGreen,
        "gainsboro" => VmlNamedColor::Gainsboro,
        "ghostwhite" => VmlNamedColor::GhostWhite,
        "gold" => VmlNamedColor::Gold,
        "goldenrod" => VmlNamedColor::Goldenrod,
        "greenyellow" => VmlNamedColor::GreenYellow,
        "honeydew" => VmlNamedColor::Honeydew,
        "hotpink" => VmlNamedColor::HotPink,
        "indianred" => VmlNamedColor::IndianRed,
        "indigo" => VmlNamedColor::Indigo,
        "ivory" => VmlNamedColor::Ivory,
        "khaki" => VmlNamedColor::Khaki,
        "lavender" => VmlNamedColor::Lavender,
        "lavenderblush" => VmlNamedColor::LavenderBlush,
        "lawngreen" => VmlNamedColor::LawnGreen,
        "lemonchiffon" => VmlNamedColor::LemonChiffon,
        "lightblue" => VmlNamedColor::LightBlue,
        "lightcoral" => VmlNamedColor::LightCoral,
        "lightcyan" => VmlNamedColor::LightCyan,
        "lightgoldenrodyellow" => VmlNamedColor::LightGoldenrodYellow,
        "lightgray" | "lightgrey" => VmlNamedColor::LightGray,
        "lightgreen" => VmlNamedColor::LightGreen,
        "lightpink" => VmlNamedColor::LightPink,
        "lightsalmon" => VmlNamedColor::LightSalmon,
        "lightseagreen" => VmlNamedColor::LightSeaGreen,
        "lightskyblue" => VmlNamedColor::LightSkyBlue,
        "lightslategray" | "lightslategrey" => VmlNamedColor::LightSlateGray,
        "lightsteelblue" => VmlNamedColor::LightSteelBlue,
        "lightyellow" => VmlNamedColor::LightYellow,
        "limegreen" => VmlNamedColor::LimeGreen,
        "linen" => VmlNamedColor::Linen,
        "magenta" => VmlNamedColor::Magenta,
        "mediumaquamarine" => VmlNamedColor::MediumAquamarine,
        "mediumblue" => VmlNamedColor::MediumBlue,
        "mediumorchid" => VmlNamedColor::MediumOrchid,
        "mediumpurple" => VmlNamedColor::MediumPurple,
        "mediumseagreen" => VmlNamedColor::MediumSeaGreen,
        "mediumslateblue" => VmlNamedColor::MediumSlateBlue,
        "mediumspringgreen" => VmlNamedColor::MediumSpringGreen,
        "mediumturquoise" => VmlNamedColor::MediumTurquoise,
        "mediumvioletred" => VmlNamedColor::MediumVioletRed,
        "midnightblue" => VmlNamedColor::MidnightBlue,
        "mintcream" => VmlNamedColor::MintCream,
        "mistyrose" => VmlNamedColor::MistyRose,
        "moccasin" => VmlNamedColor::Moccasin,
        "navajowhite" => VmlNamedColor::NavajoWhite,
        "oldlace" => VmlNamedColor::OldLace,
        "olivedrab" => VmlNamedColor::OliveDrab,
        "orangered" => VmlNamedColor::OrangeRed,
        "orchid" => VmlNamedColor::Orchid,
        "palegoldenrod" => VmlNamedColor::PaleGoldenrod,
        "palegreen" => VmlNamedColor::PaleGreen,
        "paleturquoise" => VmlNamedColor::PaleTurquoise,
        "palevioletred" => VmlNamedColor::PaleVioletRed,
        "papayawhip" => VmlNamedColor::PapayaWhip,
        "peachpuff" => VmlNamedColor::PeachPuff,
        "peru" => VmlNamedColor::Peru,
        "pink" => VmlNamedColor::Pink,
        "plum" => VmlNamedColor::Plum,
        "powderblue" => VmlNamedColor::PowderBlue,
        "rosybrown" => VmlNamedColor::RosyBrown,
        "royalblue" => VmlNamedColor::RoyalBlue,
        "saddlebrown" => VmlNamedColor::SaddleBrown,
        "salmon" => VmlNamedColor::Salmon,
        "sandybrown" => VmlNamedColor::SandyBrown,
        "seagreen" => VmlNamedColor::SeaGreen,
        "seashell" => VmlNamedColor::Seashell,
        "sienna" => VmlNamedColor::Sienna,
        "skyblue" => VmlNamedColor::SkyBlue,
        "slateblue" => VmlNamedColor::SlateBlue,
        "slategray" | "slategrey" => VmlNamedColor::SlateGray,
        "snow" => VmlNamedColor::Snow,
        "springgreen" => VmlNamedColor::SpringGreen,
        "steelblue" => VmlNamedColor::SteelBlue,
        "tan" => VmlNamedColor::Tan,
        "thistle" => VmlNamedColor::Thistle,
        "tomato" => VmlNamedColor::Tomato,
        "turquoise" => VmlNamedColor::Turquoise,
        "violet" => VmlNamedColor::Violet,
        "wheat" => VmlNamedColor::Wheat,
        "whitesmoke" => VmlNamedColor::WhiteSmoke,
        "yellowgreen" => VmlNamedColor::YellowGreen,
        // VML system colors.
        "buttonface" => VmlNamedColor::ButtonFace,
        "buttonhighlight" => VmlNamedColor::ButtonHighlight,
        "buttonshadow" => VmlNamedColor::ButtonShadow,
        "buttontext" => VmlNamedColor::ButtonText,
        "captiontext" => VmlNamedColor::CaptionText,
        "graytext" => VmlNamedColor::GrayText,
        "highlight" => VmlNamedColor::Highlight,
        "highlighttext" => VmlNamedColor::HighlightText,
        "inactiveborder" => VmlNamedColor::InactiveBorder,
        "inactivecaption" => VmlNamedColor::InactiveCaption,
        "inactivecaptiontext" => VmlNamedColor::InactiveCaptionText,
        "infobackground" => VmlNamedColor::InfoBackground,
        "infotext" => VmlNamedColor::InfoText,
        "menu" => VmlNamedColor::Menu,
        "menutext" => VmlNamedColor::MenuText,
        "scrollbar" => VmlNamedColor::Scrollbar,
        "threeddarkshadow" => VmlNamedColor::ThreeDDarkShadow,
        "threedface" => VmlNamedColor::ThreeDFace,
        "threedhighlight" => VmlNamedColor::ThreeDHighlight,
        "threedlightshadow" => VmlNamedColor::ThreeDLightShadow,
        "threedshadow" => VmlNamedColor::ThreeDShadow,
        "window" => VmlNamedColor::Window,
        "windowframe" => VmlNamedColor::WindowFrame,
        "windowtext" => VmlNamedColor::WindowText,
        _ => return None,
    })
}
