use iced::{theme, Color, Theme};

pub trait FromHexColor {
    fn from_hex_str(hex: &str) -> Option<Box<Self>>;
    fn from_hex(hex: u32) -> Self;
}

impl FromHexColor for Color {
    fn from_hex_str(hex: &str) -> Option<Box<Self>> {
        match u32::from_str_radix(hex.trim_start_matches('#'), 16) {
            Ok(hex) => Some(Box::new(Self::from_hex(hex))),
            Err(_) => None,
        }
    }

    fn from_hex(hex: u32) -> Self {
        let r = ((hex >> 16) & 0xFF) as f32 / 255f32;
        let g = ((hex >> 8) & 0xFF) as f32 / 255f32;
        let b = (hex & 0xFF) as f32 / 255f32;
        let a = ((hex >> 24) & 0xFF) as f32 / 255f32;
        Color::from_rgba(r, g, b, a)
    }
}

#[repr(u32)]
pub enum OneDark {
    Black = 0x282c34,
    Red = 0xe06c75,
    Green = 0x98c379,
    Yellow = 0xe5c07b,
    Blue = 0x61afef,
    Purple = 0xc678dd,
    Cyan = 0x56b6c2,
    // White = 0xdcdfe4,
    White = 0xabb2bf,
}

/// One Dark:
// Black 	40; 44; 52 	#282c34
// White 	171; 178; 191 	#abb2bf
// Light Red 	224; 108; 117 	#e06c75
// Dark Red 	190; 80; 70 	#be5046
// Green 	152; 195; 121 	#98c379
// Light Yellow 	229; 192; 123 	#e5c07b
// Dark Yellow 	209; 154; 102 	#d19a66
// Blue 	97; 175; 239 	#61afef
// Magenta 	198; 120; 221 	#c678dd
// Cyan 	86; 182; 194 	#56b6c2
// Gutter Grey 	76; 82; 99 	#4b5263
// Comment Grey 	92; 99; 112 	#5c6370

pub fn get_theme() -> Theme {
    Theme::custom(
        "Wellwell".to_string(),
        theme::Palette {
            background: Color::from_hex(OneDark::Black as u32),
            primary: Color::from_hex(OneDark::Blue as u32),
            text: Color::from_hex(0xabb2bf),
            success: Color::from_hex(0x98c379),
            danger: Color::from_hex(0xe06c75),
        },
    )
}
