use crate::{
    color::{ThemeColor, ThemeColorVariant},
    named_colors::ColorName,
};
use std::collections::HashMap;

#[cfg(feature = "serde")]
use serde::Deserialize;

/// A theme or color scheme represents a homogeneous combination of colors to
/// applly to an application.
///
/// Each color scheme must have a unique name that identifies it.
///
/// The default [`Theme`] has no defined colors and has `is_system` set to
/// `true` to indicate that it's a theme that follows the system wide
/// color scheme.
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[derive(Debug, Clone)]
pub struct Theme {
    /// Unique name for the theme
    pub name: String,
    /// Indicates that the [`Theme`] has no colors set and therefore follows
    /// the system wide color scheme.
    #[cfg_attr(feature = "serde", serde(default, skip))]
    pub is_system: bool,
    colors: HashMap<ColorName, ThemeColor>,
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            name: "Follow system".into(),
            is_system: true,
            colors: HashMap::default(),
        }
    }
}

impl Theme {
    pub fn new_from_iter<I: IntoIterator<Item = (ColorName, ThemeColor)>>(
        name: String,
        colors: I,
    ) -> Self {
        Self::new(name, HashMap::from_iter(colors))
    }

    pub fn new(name: String, colors: HashMap<ColorName, ThemeColor>) -> Self {
        Self {
            name,
            is_system: false,
            colors,
        }
    }

    pub fn to_css(&self, variant: ThemeColorVariant) -> String {
        self.colors
            .iter()
            .map(|(name, tcolor)| format!("@define-color {name} {};\n", tcolor.get(variant)))
            .collect::<String>()
    }

    pub fn get_provider(&self, variant: ThemeColorVariant) -> gtk::CssProvider {
        let provider = gtk::CssProvider::new();
        provider.load_from_data(&self.to_css(variant));
        provider
    }

    pub fn action_row(&self) -> adw::ActionRow {
        adw::ActionRow::builder()
            .title_lines(1)
            .title(&self.name)
            .build()
    }

    /// Returns a list of built-in themes. Without any additional feature flags
    /// this will return an empty list.
    pub fn default_themes() -> Vec<Self> {
        vec![
            // adapted from https://github.com/lassekongo83/adw-colors/
            // provided by https://github.com/davidphilipbarr
            #[cfg(feature = "theme_solarized")]
            Self::new_from_iter(
                "Solarized".into(),
                [
                    (ColorName::WindowBg, ThemeColor::new("#eee8d5", "#002b36")),
                    (ColorName::WindowFg, ThemeColor::new("#586e75", "#eee8d5")),
                    (ColorName::Accent, ThemeColor::new("#dbd6c4", "#586e75")),
                    (ColorName::AccentBg, ThemeColor::new("#dbd6c4", "#1f444d")),
                    (ColorName::AccentFg, ThemeColor::new("#073642", "#eee8d5")),
                    (
                        ColorName::HeaderbarBg,
                        ThemeColor::new("#f5efde", "#07313b"),
                    ),
                    (
                        ColorName::HeaderbarFg,
                        ThemeColor::new("#586e75", "#eee8d5"),
                    ),
                    (
                        ColorName::HeaderbarBackdrop,
                        ThemeColor::new("#fdf6e3", "#002b36"),
                    ),
                    (ColorName::PopoverBg, ThemeColor::new("#fdf6e3", "#123a44")),
                    (ColorName::PopoverFg, ThemeColor::new("#073642", "#eee8d5")),
                    (
                        ColorName::DialogBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::DialogFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (ColorName::SidebarBg, ThemeColor::new("#f5efde", "#07313b")),
                    (ColorName::SidebarFg, ThemeColor::new("#586e75", "#eee8d5")),
                    (
                        ColorName::SidebarBackdrop,
                        ThemeColor::new("#fdf6e3", "#002b36"),
                    ),
                    (
                        ColorName::SidebarShade,
                        ThemeColor::new("RGB(0 0 6 / 7%)", "RGB(0 0 6 / 25%)"),
                    ),
                    (
                        ColorName::SidebarBorder,
                        ThemeColor::new("RGB(0 0 6 / 7%)", "RGB(0 0 6 / 36%)"),
                    ),
                    (
                        ColorName::SecondarySidebarBg,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarFg,
                        ThemeColor::new("RGB(0 0 6 / 80%)", "white"),
                    ),
                    (
                        ColorName::SecondarySidebarBackdrop,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarShade,
                        ThemeColor::new("@sidebar_shade_color", "@sidebar_shade_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBorder,
                        ThemeColor::new("@sidebar_border_color", "@sidebar_border_color"),
                    ),
                    (ColorName::ViewBg, ThemeColor::new("#fdf6e3", "#073642")),
                    (ColorName::ViewFg, ThemeColor::new("#073642", "#eee8d5")),
                    (ColorName::CardBg, ThemeColor::new("#eee8d5", "#123a44")),
                    (ColorName::CardFg, ThemeColor::new("#073642", "#fdf6e3")),
                    (ColorName::WarningBg, ThemeColor::new("#CB4B16", "#CB4B16")),
                    (ColorName::WarningFg, ThemeColor::new("#eee8d5", "#eee8d5")),
                    (ColorName::Warning, ThemeColor::new("#CB4B16", "#CB4B16")),
                    (ColorName::ErrorBg, ThemeColor::new("#DC322F", "#DC322F")),
                    (ColorName::ErrorFg, ThemeColor::new("#eee8d5", "#eee8d5")),
                    (ColorName::Error, ThemeColor::new("#DC322F", "#DC322F")),
                    (ColorName::SuccessBg, ThemeColor::new("#859900", "#859900")),
                    (ColorName::SuccessFg, ThemeColor::new("#eee8d5", "#eee8d5")),
                    (ColorName::Success, ThemeColor::new("#859900", "#859900")),
                    (
                        ColorName::DestructiveBg,
                        ThemeColor::new("#D33682", "#D33682"),
                    ),
                    (
                        ColorName::DestructiveFg,
                        ThemeColor::new("#eee8d5", "#eee8d5"),
                    ),
                    (
                        ColorName::Destructive,
                        ThemeColor::new("#D33682", "#D33682"),
                    ),
                ],
            ),
            // adapted from https://github.com/claymorwan/adw-catppuccin/
            #[cfg(feature = "theme_catppuccin")]
            Self::new_from_iter(
                "Catppuccin Frappe".into(),
                [
                    (ColorName::Accent, ThemeColor::new("#ca9ee6", "#ca9ee6")),
                    (ColorName::AccentBg, ThemeColor::new("#ca9ee6", "#ca9ee6")),
                    (ColorName::AccentFg, ThemeColor::new("#232634", "#232634")),
                    (ColorName::WindowBg, ThemeColor::new("#303446", "#303446")),
                    (ColorName::WindowFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (
                        ColorName::HeaderbarBg,
                        ThemeColor::new("#303446", "#303446"),
                    ),
                    (
                        ColorName::HeaderbarFg,
                        ThemeColor::new("#c6d0f5", "#c6d0f5"),
                    ),
                    (ColorName::PopoverBg, ThemeColor::new("#414559", "#414559")),
                    (ColorName::PopoverFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (
                        ColorName::DialogBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::DialogFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (ColorName::SidebarBg, ThemeColor::new("#303446", "#303446")),
                    (ColorName::SidebarFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (
                        ColorName::SidebarBackdrop,
                        ThemeColor::new("#303446", "#303446"),
                    ),
                    (
                        ColorName::SidebarShade,
                        ThemeColor::new("RGB(0 0 6 / 25%)", "RGB(0 0 6 / 25%)"),
                    ),
                    (
                        ColorName::SidebarBorder,
                        ThemeColor::new("RGB(0 0 6 / 36%)", "RGB(0 0 6 / 36%)"),
                    ),
                    (
                        ColorName::SecondarySidebarBg,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarFg,
                        ThemeColor::new("#c6d0f5", "#c6d0f5"),
                    ),
                    (
                        ColorName::SecondarySidebarBackdrop,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarShade,
                        ThemeColor::new("@sidebar_shade_color", "@sidebar_shade_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBorder,
                        ThemeColor::new("@sidebar_border_color", "@sidebar_border_color"),
                    ),
                    (ColorName::ViewBg, ThemeColor::new("#303446", "#303446")),
                    (ColorName::ViewFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (ColorName::CardBg, ThemeColor::new("#414559", "#414559")),
                    (ColorName::CardFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (
                        ColorName::ThumbnailBg,
                        ThemeColor::new("#414559", "#414559"),
                    ),
                    (
                        ColorName::ThumbnailFg,
                        ThemeColor::new("#c6d0f5", "#c6d0f5"),
                    ),
                    (ColorName::WarningBg, ThemeColor::new("#e78284", "#e78284")),
                    (ColorName::WarningFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (ColorName::Warning, ThemeColor::new("#e78284", "#e78284")),
                    (ColorName::ErrorBg, ThemeColor::new("#e78284", "#e78284")),
                    (ColorName::ErrorFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (ColorName::Error, ThemeColor::new("#e78284", "#e78284")),
                    (ColorName::SuccessBg, ThemeColor::new("#a6d189", "#a6d189")),
                    (ColorName::SuccessFg, ThemeColor::new("#c6d0f5", "#c6d0f5")),
                    (ColorName::Success, ThemeColor::new("#a6d189", "#a6d189")),
                    (
                        ColorName::DestructiveBg,
                        ThemeColor::new("#e78284", "#e78284"),
                    ),
                    (
                        ColorName::DestructiveFg,
                        ThemeColor::new("#232634", "#232634"),
                    ),
                    (
                        ColorName::Destructive,
                        ThemeColor::new("#e78284", "#e78284"),
                    ),
                ],
            ),
            #[cfg(feature = "theme_catppuccin")]
            Self::new_from_iter(
                "Catppuccin Latte".into(),
                [
                    (ColorName::Accent, ThemeColor::new("#8839ef", "#8839ef")),
                    (ColorName::AccentBg, ThemeColor::new("#8839ef", "#8839ef")),
                    (ColorName::AccentFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (ColorName::WindowBg, ThemeColor::new("#eff1f5", "#eff1f5")),
                    (ColorName::WindowFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (
                        ColorName::HeaderbarBg,
                        ThemeColor::new("#eff1f5", "#eff1f5"),
                    ),
                    (
                        ColorName::HeaderbarFg,
                        ThemeColor::new("#4c4f69", "#4c4f69"),
                    ),
                    (ColorName::PopoverBg, ThemeColor::new("#ccd0da", "#ccd0da")),
                    (ColorName::PopoverFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (
                        ColorName::DialogBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::DialogFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (ColorName::SidebarBg, ThemeColor::new("#eff1f5", "#eff1f5")),
                    (ColorName::SidebarFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (
                        ColorName::SidebarBackdrop,
                        ThemeColor::new("#eff1f5", "#eff1f5"),
                    ),
                    (
                        ColorName::SidebarShade,
                        ThemeColor::new("RGB(0 0 6 / 25%)", "RGB(0 0 6 / 25%)"),
                    ),
                    (
                        ColorName::SidebarBorder,
                        ThemeColor::new("RGB(0 0 6 / 36%)", "RGB(0 0 6 / 36%)"),
                    ),
                    (
                        ColorName::SecondarySidebarBg,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarFg,
                        ThemeColor::new("#4c4f69", "#4c4f69"),
                    ),
                    (
                        ColorName::SecondarySidebarBackdrop,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarShade,
                        ThemeColor::new("@sidebar_shade_color", "@sidebar_shade_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBorder,
                        ThemeColor::new("@sidebar_border_color", "@sidebar_border_color"),
                    ),
                    (ColorName::ViewBg, ThemeColor::new("#eff1f5", "#eff1f5")),
                    (ColorName::ViewFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (ColorName::CardBg, ThemeColor::new("#ccd0da", "#ccd0da")),
                    (ColorName::CardFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (
                        ColorName::ThumbnailBg,
                        ThemeColor::new("#ccd0da", "#ccd0da"),
                    ),
                    (
                        ColorName::ThumbnailFg,
                        ThemeColor::new("#4c4f69", "#4c4f69"),
                    ),
                    (ColorName::WarningBg, ThemeColor::new("#d20f39", "#d20f39")),
                    (ColorName::WarningFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (ColorName::Warning, ThemeColor::new("#d20f39", "#d20f39")),
                    (ColorName::ErrorBg, ThemeColor::new("#d20f39", "#d20f39")),
                    (ColorName::ErrorFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (ColorName::Error, ThemeColor::new("#d20f39", "#d20f39")),
                    (ColorName::SuccessBg, ThemeColor::new("#40a02b", "#40a02b")),
                    (ColorName::SuccessFg, ThemeColor::new("#4c4f69", "#4c4f69")),
                    (ColorName::Success, ThemeColor::new("#40a02b", "#40a02b")),
                    (
                        ColorName::DestructiveBg,
                        ThemeColor::new("#d20f39", "#d20f39"),
                    ),
                    (
                        ColorName::DestructiveFg,
                        ThemeColor::new("#dce0e8", "#dce0e8"),
                    ),
                    (
                        ColorName::Destructive,
                        ThemeColor::new("#d20f39", "#d20f39"),
                    ),
                ],
            ),
            #[cfg(feature = "theme_catppuccin")]
            Self::new_from_iter(
                "Catppuccin Macchiato".into(),
                [
                    (ColorName::Accent, ThemeColor::new("#c6a0f6", "#c6a0f6")),
                    (ColorName::AccentBg, ThemeColor::new("#c6a0f6", "#c6a0f6")),
                    (ColorName::AccentFg, ThemeColor::new("#181926", "#181926")),
                    (ColorName::WindowBg, ThemeColor::new("#24273a", "#24273a")),
                    (ColorName::WindowFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (
                        ColorName::HeaderbarBg,
                        ThemeColor::new("#24273a", "#24273a"),
                    ),
                    (
                        ColorName::HeaderbarFg,
                        ThemeColor::new("#cad3f5", "#cad3f5"),
                    ),
                    (ColorName::PopoverBg, ThemeColor::new("#363a4f", "#363a4f")),
                    (ColorName::PopoverFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (
                        ColorName::DialogBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::DialogFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (ColorName::SidebarBg, ThemeColor::new("#24273a", "#24273a")),
                    (ColorName::SidebarFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (
                        ColorName::SidebarBackdrop,
                        ThemeColor::new("#24273a", "#24273a"),
                    ),
                    (
                        ColorName::SidebarShade,
                        ThemeColor::new("RGB(0 0 6 / 25%)", "RGB(0 0 6 / 25%)"),
                    ),
                    (
                        ColorName::SidebarBorder,
                        ThemeColor::new("RGB(0 0 6 / 36%)", "RGB(0 0 6 / 36%)"),
                    ),
                    (
                        ColorName::SecondarySidebarBg,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarFg,
                        ThemeColor::new("#cad3f5", "#cad3f5"),
                    ),
                    (
                        ColorName::SecondarySidebarBackdrop,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarShade,
                        ThemeColor::new("@sidebar_shade_color", "@sidebar_shade_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBorder,
                        ThemeColor::new("@sidebar_border_color", "@sidebar_border_color"),
                    ),
                    (ColorName::ViewBg, ThemeColor::new("#24273a", "#24273a")),
                    (ColorName::ViewFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (ColorName::CardBg, ThemeColor::new("#363a4f", "#363a4f")),
                    (ColorName::CardFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (
                        ColorName::ThumbnailBg,
                        ThemeColor::new("#363a4f", "#363a4f"),
                    ),
                    (
                        ColorName::ThumbnailFg,
                        ThemeColor::new("#cad3f5", "#cad3f5"),
                    ),
                    (ColorName::WarningBg, ThemeColor::new("#ed8796", "#ed8796")),
                    (ColorName::WarningFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (ColorName::Warning, ThemeColor::new("#ed8796", "#ed8796")),
                    (ColorName::ErrorBg, ThemeColor::new("#ed8796", "#ed8796")),
                    (ColorName::ErrorFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (ColorName::Error, ThemeColor::new("#ed8796", "#ed8796")),
                    (ColorName::SuccessBg, ThemeColor::new("#a6da95", "#a6da95")),
                    (ColorName::SuccessFg, ThemeColor::new("#cad3f5", "#cad3f5")),
                    (ColorName::Success, ThemeColor::new("#a6da95", "#a6da95")),
                    (
                        ColorName::DestructiveBg,
                        ThemeColor::new("#ed8796", "#ed8796"),
                    ),
                    (
                        ColorName::DestructiveFg,
                        ThemeColor::new("#181926", "#181926"),
                    ),
                    (
                        ColorName::Destructive,
                        ThemeColor::new("#ed8796", "#ed8796"),
                    ),
                ],
            ),
            #[cfg(feature = "theme_catppuccin")]
            Self::new_from_iter(
                "Catppuccin Mocha".into(),
                [
                    (ColorName::Accent, ThemeColor::new("#cba6f7", "#cba6f7")),
                    (ColorName::AccentBg, ThemeColor::new("#cba6f7", "#cba6f7")),
                    (ColorName::AccentFg, ThemeColor::new("#11111b", "#11111b")),
                    (ColorName::WindowBg, ThemeColor::new("#1e1e2e", "#1e1e2e")),
                    (ColorName::WindowFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (
                        ColorName::HeaderbarBg,
                        ThemeColor::new("#1e1e2e", "#1e1e2e"),
                    ),
                    (
                        ColorName::HeaderbarFg,
                        ThemeColor::new("#cdd6f4", "#cdd6f4"),
                    ),
                    (ColorName::PopoverBg, ThemeColor::new("#313244", "#313244")),
                    (ColorName::PopoverFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (
                        ColorName::DialogBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::DialogFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (ColorName::SidebarBg, ThemeColor::new("#1e1e2e", "#1e1e2e")),
                    (ColorName::SidebarFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (
                        ColorName::SidebarBackdrop,
                        ThemeColor::new("#1e1e2e", "#1e1e2e"),
                    ),
                    (
                        ColorName::SidebarShade,
                        ThemeColor::new("RGB(0 0 6 / 25%)", "RGB(0 0 6 / 25%)"),
                    ),
                    (
                        ColorName::SidebarBorder,
                        ThemeColor::new("RGB(0 0 6 / 36%)", "RGB(0 0 6 / 36%)"),
                    ),
                    (
                        ColorName::SecondarySidebarBg,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarFg,
                        ThemeColor::new("#cdd6f4", "#cdd6f4"),
                    ),
                    (
                        ColorName::SecondarySidebarBackdrop,
                        ThemeColor::new("@sidebar_backdrop_color", "@sidebar_backdrop_color"),
                    ),
                    (
                        ColorName::SecondarySidebarShade,
                        ThemeColor::new("@sidebar_shade_color", "@sidebar_shade_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBorder,
                        ThemeColor::new("@sidebar_border_color", "@sidebar_border_color"),
                    ),
                    (ColorName::ViewBg, ThemeColor::new("#1e1e2e", "#1e1e2e")),
                    (ColorName::ViewFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (ColorName::CardBg, ThemeColor::new("#313244", "#313244")),
                    (ColorName::CardFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (
                        ColorName::ThumbnailBg,
                        ThemeColor::new("#313244", "#313244"),
                    ),
                    (
                        ColorName::ThumbnailFg,
                        ThemeColor::new("#cdd6f4", "#cdd6f4"),
                    ),
                    (ColorName::WarningBg, ThemeColor::new("#f38ba8", "#f38ba8")),
                    (ColorName::WarningFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (ColorName::Warning, ThemeColor::new("#f38ba8", "#f38ba8")),
                    (ColorName::ErrorBg, ThemeColor::new("#f38ba8", "#f38ba8")),
                    (ColorName::ErrorFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (ColorName::Error, ThemeColor::new("#f38ba8", "#f38ba8")),
                    (ColorName::SuccessBg, ThemeColor::new("#a6e3a1", "#a6e3a1")),
                    (ColorName::SuccessFg, ThemeColor::new("#cdd6f4", "#cdd6f4")),
                    (ColorName::Success, ThemeColor::new("#a6e3a1", "#a6e3a1")),
                    (
                        ColorName::DestructiveBg,
                        ThemeColor::new("#f38ba8", "#f38ba8"),
                    ),
                    (
                        ColorName::DestructiveFg,
                        ThemeColor::new("#11111b", "#11111b"),
                    ),
                    (
                        ColorName::Destructive,
                        ThemeColor::new("#f38ba8", "#f38ba8"),
                    ),
                ],
            ),
            // adapted from https://github.com/Fausto-Korpsvart/Everforest-GTK-Theme/
            #[cfg(feature = "theme_everforest")]
            Self::new_from_iter(
                "Everforest".into(),
                [
                    (ColorName::AccentBg, ThemeColor::new("#3a94c5", "#7fbbb3")),
                    (
                        ColorName::AccentFg,
                        ThemeColor::new("#fffbef", "rgba(39, 46, 51, 0.87)"),
                    ),
                    (ColorName::Accent, ThemeColor::new("#3a94c5", "#7fbbb3")),
                    (
                        ColorName::DestructiveBg,
                        ThemeColor::new("#e67e80", "#f85552"),
                    ),
                    (
                        ColorName::DestructiveFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (
                        ColorName::Destructive,
                        ThemeColor::new("#e67e80", "#f85552"),
                    ),
                    (ColorName::SuccessBg, ThemeColor::new("#a7c080", "#8da101")),
                    (
                        ColorName::SuccessFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (ColorName::Success, ThemeColor::new("#a7c080", "#8da101")),
                    (ColorName::WarningBg, ThemeColor::new("#dbbc7f", "#dfa000")),
                    (
                        ColorName::WarningFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "rgba(39, 46, 51, 0.87)"),
                    ),
                    (ColorName::Warning, ThemeColor::new("#dbbc7f", "#dfa000")),
                    (ColorName::ErrorBg, ThemeColor::new("#e67e80", "#f85552")),
                    (
                        ColorName::ErrorFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (ColorName::Error, ThemeColor::new("#e67e80", "#f85552")),
                    (ColorName::WindowBg, ThemeColor::new("#fffbef", "#272e33")),
                    (
                        ColorName::WindowFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (ColorName::ViewBg, ThemeColor::new("#e8e5d5", "#293136")),
                    (
                        ColorName::ViewFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (
                        ColorName::HeaderbarBg,
                        ThemeColor::new("#fffbef", "#272e33"),
                    ),
                    (
                        ColorName::HeaderbarFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (
                        ColorName::HeaderbarBorder,
                        ThemeColor::new("rgba(39, 46, 51, 0.12)", "rgba(255, 251, 239, 0.12)"),
                    ),
                    (
                        ColorName::HeaderbarBackdrop,
                        ThemeColor::new("#fffbef", "#272e33"),
                    ),
                    (
                        ColorName::HeaderbarShade,
                        ThemeColor::new("rgba(39, 46, 51, 0.12)", "rgba(255, 251, 239, 0.12)"),
                    ),
                    (ColorName::CardBg, ThemeColor::new("#e8e5d5", "#293136")),
                    (
                        ColorName::CardFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (
                        ColorName::CardShade,
                        ThemeColor::new("rgba(39, 46, 51, 0.12)", "rgba(255, 251, 239, 0.12)"),
                    ),
                    (ColorName::DialogBg, ThemeColor::new("#e0dcc7", "#293136")),
                    (
                        ColorName::DialogFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (ColorName::PopoverBg, ThemeColor::new("#fffbef", "#232a2e")),
                    (
                        ColorName::PopoverFg,
                        ThemeColor::new("rgba(39, 46, 51, 0.87)", "#fffbef"),
                    ),
                    (
                        ColorName::Shade,
                        ThemeColor::new("rgba(39, 46, 51, 0.12)", "rgba(255, 251, 239, 0.12)"),
                    ),
                    (
                        ColorName::SidebarBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::SidebarFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (
                        ColorName::SidebarShade,
                        ThemeColor::new("@popover_shade_color", "@popover_shade_color"),
                    ),
                    (
                        ColorName::SidebarBackdrop,
                        ThemeColor::new("@window_bg_color", "@window_bg_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBg,
                        ThemeColor::new("@popover_bg_color", "@popover_bg_color"),
                    ),
                    (
                        ColorName::SecondarySidebarFg,
                        ThemeColor::new("@popover_fg_color", "@popover_fg_color"),
                    ),
                    (
                        ColorName::SecondarySidebarShade,
                        ThemeColor::new("@popover_shade_color", "@popover_shade_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBackdrop,
                        ThemeColor::new("@window_bg_color", "@window_bg_color"),
                    ),
                    (
                        ColorName::ScrollbarOutline,
                        ThemeColor::new("rgba(39, 46, 51, 0.12)", "rgba(255, 251, 239, 0.12)"),
                    ),
                    (
                        ColorName::SidebarBorder,
                        ThemeColor::new("@scrollbar_outline_color", "@scrollbar_outline_color"),
                    ),
                    (
                        ColorName::SecondarySidebarBorder,
                        ThemeColor::new("@scrollbar_outline_color", "@scrollbar_outline_color"),
                    ),
                ],
            ),
        ]
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "serde")]
    #[test]
    fn deserialize_json_theme() {
        use super::Theme;
        use crate::named_colors::ColorName;

        let theme: Theme = serde_json::from_str(
            r###"{
                "name": "Example",
                "colors": {
                    "Accent": {
                        "light": "#ff0000",
                        "dark": "#aa0000"
                    }
                }
            }"###,
        )
        .unwrap();
        assert_eq!(theme.colors.len(), 1);
        assert_eq!(
            theme
                .colors
                .get(&ColorName::Accent)
                .unwrap()
                .get(crate::color::ThemeColorVariant::Light),
            "#ff0000"
        );
    }
}
