haj 0.3.7

the beautiful, cross-distro package manager and tui.
use crate::config::ColorConfig;
use ratatui::style::Color;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Theme {
    pub accent: Color,
    pub secondary: Color,
    pub heading: Color,
    pub text: Color,
    pub muted: Color,
    pub success: Color,
    pub warning: Color,
    pub error: Color,
    pub repo_core: Color,
    pub repo_extra: Color,
    pub repo_multilib: Color,
    pub repo_aur: Color,
}

struct ThemePreset {
    name: &'static str,
    accent: &'static str,
    secondary: &'static str,
    heading: &'static str,
    text: &'static str,
    muted: &'static str,
    success: &'static str,
    warning: &'static str,
    error: &'static str,
}

// Derived from btop themes (/usr/share/btop/themes) and Catppuccin palettes.
const PRESETS: &[ThemePreset] = &[
    ThemePreset {
        name: "catppuccin-latte",
        accent: "#209fb5",
        secondary: "#8839ef",
        heading: "#dc8a78",
        text: "#4c4f69",
        muted: "#7c7f93",
        success: "#40a02b",
        warning: "#df8e1d",
        error: "#d20f39",
    },
    ThemePreset {
        name: "catppuccin-frappe",
        accent: "#85c1dc",
        secondary: "#ca9ee6",
        heading: "#a5adce",
        text: "#c6d0f5",
        muted: "#737994",
        success: "#a6d189",
        warning: "#e5c890",
        error: "#e78284",
    },
    ThemePreset {
        name: "catppuccin-macchiato",
        accent: "#8bd5ca",
        secondary: "#c6a0f6",
        heading: "#f0c6c6",
        text: "#cad3f5",
        muted: "#6e738d",
        success: "#a6da95",
        warning: "#eed49f",
        error: "#ed8796",
    },
    ThemePreset {
        name: "catppuccin-mocha",
        accent: "#74c7ec",
        secondary: "#cba6f7",
        heading: "#f5e0dc",
        text: "#cdd6f4",
        muted: "#6c7086",
        success: "#a6e3a1",
        warning: "#f9e2af",
        error: "#f38ba8",
    },
    ThemePreset {
        name: "tokyo-night",
        accent: "#7dcfff",
        secondary: "#f7768e",
        heading: "#cfc9c2",
        text: "#cfc9c2",
        muted: "#565f89",
        success: "#9ece6a",
        warning: "#e0af68",
        error: "#f7768e",
    },
    ThemePreset {
        name: "tokyo-storm",
        accent: "#7dcfff",
        secondary: "#f7768e",
        heading: "#cfc9c2",
        text: "#cfc9c2",
        muted: "#565f89",
        success: "#9ece6a",
        warning: "#e0af68",
        error: "#f7768e",
    },
    ThemePreset {
        name: "dracula",
        accent: "#8be9fd",
        secondary: "#bd93f9",
        heading: "#ff79c6",
        text: "#f8f8f2",
        muted: "#6272a4",
        success: "#50fa7b",
        warning: "#ffb86c",
        error: "#ff5555",
    },
    ThemePreset {
        name: "nord",
        accent: "#88C0D0",
        secondary: "#8FBCBB",
        heading: "#81A1C1",
        text: "#D8DEE9",
        muted: "#4C566A",
        success: "#8FBCBB",
        warning: "#81A1C1",
        error: "#ECEFF4",
    },
    ThemePreset {
        name: "gruvbox-dark",
        accent: "#8EC07C",
        secondary: "#D3869B",
        heading: "#FABD2F",
        text: "#EBDBB2",
        muted: "#A89984",
        success: "#98971A",
        warning: "#D79921",
        error: "#CC241D",
    },
    ThemePreset {
        name: "gruvbox-material",
        accent: "#7daea3",
        secondary: "#ea6962",
        heading: "#d8a657",
        text: "#d4be98",
        muted: "#7c6f64",
        success: "#a9b665",
        warning: "#d8a657",
        error: "#ea6962",
    },
    ThemePreset {
        name: "onedark",
        accent: "#61afef",
        secondary: "#e06c75",
        heading: "#e5c07b",
        text: "#abb2bf",
        muted: "#5c6370",
        success: "#98c379",
        warning: "#e5c07b",
        error: "#e06c75",
    },
    ThemePreset {
        name: "everforest-dark",
        accent: "#7fbbb3",
        secondary: "#e67e80",
        heading: "#83c092",
        text: "#d3c6aa",
        muted: "#3d484d",
        success: "#a7c080",
        warning: "#dbbc7f",
        error: "#f85552",
    },
    ThemePreset {
        name: "kanagawa-wave",
        accent: "#7FB4CA",
        secondary: "#957FBB",
        heading: "#7aa89f",
        text: "#dcd7ba",
        muted: "#727169",
        success: "#98BB6C",
        warning: "#DCA561",
        error: "#E82424",
    },
    ThemePreset {
        name: "kanagawa-dragon",
        accent: "#8ea4a2",
        secondary: "#a292a3",
        heading: "#c4b28a",
        text: "#c5c9c5",
        muted: "#7a8382",
        success: "#87a987",
        warning: "#c4b28a",
        error: "#c4746e",
    },
    ThemePreset {
        name: "solarized-dark",
        accent: "#268bd2",
        secondary: "#d33682",
        heading: "#b58900",
        text: "#eee8d5",
        muted: "#586e75",
        success: "#bad600",
        warning: "#edb400",
        error: "#e02f30",
    },
    ThemePreset {
        name: "monokai",
        accent: "#66D9EF",
        secondary: "#F92672",
        heading: "#ccaefc",
        text: "#F8F8F2",
        muted: "#75715E",
        success: "#A6E22E",
        warning: "#E6DB74",
        error: "#F92672",
    },
    ThemePreset {
        name: "ayu",
        accent: "#59C2FF",
        secondary: "#D2A6FF",
        heading: "#DFBFFF",
        text: "#BFBDB6",
        muted: "#565B66",
        success: "#95E6CB",
        warning: "#E6B450",
        error: "#F07178",
    },
    ThemePreset {
        name: "flexoki-dark",
        accent: "#3AA99F",
        secondary: "#CE5D97",
        heading: "#4385BE",
        text: "#CECDC3",
        muted: "#878580",
        success: "#879A39",
        warning: "#DA702C",
        error: "#CE5D97",
    },
];

impl Theme {
    fn from_color_fields(cfg: &ColorConfig) -> Self {
        Self {
            accent: parse_color(&cfg.accent, Color::Cyan),
            secondary: parse_color(&cfg.secondary, Color::Magenta),
            heading: parse_color(&cfg.heading, Color::LightMagenta),
            text: parse_color(&cfg.text, Color::White),
            muted: parse_color(&cfg.muted, Color::DarkGray),
            success: parse_color(&cfg.success, Color::Green),
            warning: parse_color(&cfg.warning, Color::Yellow),
            error: parse_color(&cfg.error, Color::Red),
            repo_core: parse_color(&cfg.repo_core, Color::Cyan),
            repo_extra: parse_color(&cfg.repo_extra, Color::Green),
            repo_multilib: parse_color(&cfg.repo_multilib, Color::Yellow),
            repo_aur: parse_color(&cfg.repo_aur, Color::Magenta),
        }
    }

    pub fn from_config(cfg: &ColorConfig) -> Self {
        let name = cfg.theme.trim().to_ascii_lowercase().replace('_', "-");
        if name == "custom" {
            return Self::from_color_fields(cfg);
        }
        if name != "default"
            && !name.is_empty()
            && let Some(preset) = preset_theme(&name)
        {
            return preset;
        }
        Self::from_color_fields(&ColorConfig::default())
    }
}

pub fn preset_theme(name: &str) -> Option<Theme> {
    let key = name.trim().to_ascii_lowercase().replace('_', "-");
    let p = PRESETS.iter().find(|p| p.name == key)?;
    let accent = parse_color(p.accent, Color::Cyan);
    let secondary = parse_color(p.secondary, Color::Magenta);
    let success = parse_color(p.success, Color::Green);
    let warning = parse_color(p.warning, Color::Yellow);
    Some(Theme {
        accent,
        secondary,
        heading: parse_color(p.heading, Color::LightMagenta),
        text: parse_color(p.text, Color::White),
        muted: parse_color(p.muted, Color::DarkGray),
        success,
        warning,
        error: parse_color(p.error, Color::Red),
        repo_core: accent,
        repo_extra: success,
        repo_multilib: warning,
        repo_aur: secondary,
    })
}

pub fn parse_color(spec: &str, fallback: Color) -> Color {
    let s = spec.trim();
    if let Some(hex) = s.strip_prefix('#')
        && hex.len() == 6
        && let (Ok(r), Ok(g), Ok(b)) = (
            u8::from_str_radix(&hex[0..2], 16),
            u8::from_str_radix(&hex[2..4], 16),
            u8::from_str_radix(&hex[4..6], 16),
        )
    {
        return Color::Rgb(r, g, b);
    }
    if let Ok(idx) = s.parse::<u8>() {
        return Color::Indexed(idx);
    }
    match s.to_ascii_lowercase().replace('-', "_").as_str() {
        "black" => Color::Black,
        "red" => Color::Red,
        "green" => Color::Green,
        "yellow" => Color::Yellow,
        "blue" => Color::Blue,
        "magenta" => Color::Magenta,
        "cyan" => Color::Cyan,
        "gray" | "grey" => Color::Gray,
        "dark_gray" | "dark_grey" => Color::DarkGray,
        "light_red" => Color::LightRed,
        "light_green" => Color::LightGreen,
        "light_yellow" => Color::LightYellow,
        "light_blue" => Color::LightBlue,
        "light_magenta" => Color::LightMagenta,
        "light_cyan" => Color::LightCyan,
        "white" => Color::White,
        "reset" => Color::Reset,
        _ => fallback,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_color_variants_and_theme_defaults() {
        assert_eq!(
            parse_color("#8bd5ca", Color::White),
            Color::Rgb(139, 213, 202)
        );
        assert_eq!(parse_color("208", Color::White), Color::Indexed(208));
        assert_eq!(
            parse_color("light-magenta", Color::White),
            Color::LightMagenta
        );
        assert_eq!(parse_color("light_cyan", Color::White), Color::LightCyan);
        assert_eq!(parse_color("not_a_color", Color::Cyan), Color::Cyan);

        let theme = Theme::from_config(&ColorConfig::default());
        assert_eq!(theme.accent, Color::Cyan);
        assert_eq!(theme.heading, Color::LightMagenta);
        assert_eq!(theme.repo_aur, Color::Magenta);
    }

    #[test]
    fn test_preset_and_custom_theme_selection() {
        for name in [
            "catppuccin-latte",
            "catppuccin-frappe",
            "catppuccin-macchiato",
            "catppuccin-mocha",
            "tokyo-night",
            "tokyo_night",
            "tokyo-storm",
            "dracula",
            "nord",
            "gruvbox-dark",
            "gruvbox-material",
            "onedark",
            "everforest-dark",
            "kanagawa-wave",
            "kanagawa-dragon",
            "solarized-dark",
            "monokai",
            "ayu",
            "flexoki-dark",
        ] {
            let preset = preset_theme(name).expect("preset must resolve");
            assert!(matches!(preset.accent, Color::Rgb(_, _, _)));
        }

        // Preset ignores custom fields unless theme = "custom".
        let mut cfg = ColorConfig {
            theme: "dracula".into(),
            accent: "#112233".into(),
            ..Default::default()
        };
        assert_eq!(
            Theme::from_config(&cfg).accent,
            Color::Rgb(0x8b, 0xe9, 0xfd)
        );

        cfg.theme = "custom".into();
        assert_eq!(
            Theme::from_config(&cfg).accent,
            Color::Rgb(0x11, 0x22, 0x33)
        );
    }
}