use eframe::egui;

pub struct Theme;

impl Theme {
    // ── Global Design System (Tactical DNA) ──
    pub const BASE: egui::Color32 = egui::Color32::from_rgb(15, 18, 21); // #0F1215 Deep Matte Slate
    pub const PANEL_OPACITY: f32 = 0.4;
    pub const BORDER: egui::Color32 = egui::Color32::from_rgba_premultiplied(255, 255, 255, 26); // #ffffff1a

    pub const SIGNAL_GREEN: egui::Color32 = egui::Color32::from_rgb(0, 255, 136); // #00FF88
    pub const COBALT_BLUE: egui::Color32 = egui::Color32::from_rgb(74, 144, 226); // #4A90E2
    pub const TACTICAL_AMBER: egui::Color32 = egui::Color32::from_rgb(255, 179, 0); // #FFB300
    pub const CRIMSON_ALERT: egui::Color32 = egui::Color32::from_rgb(255, 75, 75);  // #FF4B4B

    pub const TEXT_PRIMARY: egui::Color32 = egui::Color32::from_rgb(224, 224, 224); // #E0E0E0
    pub const TEXT_SECONDARY: egui::Color32 = egui::Color32::from_rgb(138, 138, 138); // #8A8A8A
    pub const TEXT_DIM: egui::Color32 = egui::Color32::from_rgb(60, 60, 70);

    // ── Icons (FontAwesome) ──
    pub const ICON_BRAND: &'static str = "\u{f58f}"; // headset
    pub const ICON_CONNECTIONS: &'static str = "\u{f0ac}"; // globe
    pub const ICON_BOOKMARKS: &'static str = "\u{f005}"; // star
    pub const ICON_SELF: &'static str = "\u{f4fe}"; // user-gear
    pub const ICON_PERMISSIONS: &'static str = "\u{f3ed}"; // shield
    pub const ICON_TOOLS: &'static str = "\u{f085}"; // gears
    pub const ICON_INFO: &'static str = "\u{f05a}"; // info-circle

    pub fn apply(ctx: &egui::Context) {
        let mut style = (*ctx.global_style()).clone();

        style.visuals.dark_mode = true;
        style.visuals.panel_fill = Self::BASE;
        style.visuals.window_fill = Self::BASE.linear_multiply(Self::PANEL_OPACITY);
        style.visuals.extreme_bg_color = egui::Color32::BLACK;

        style.visuals.widgets.noninteractive.bg_fill = Self::BASE;
        style.visuals.widgets.inactive.bg_fill = egui::Color32::from_rgba_premultiplied(20, 20, 25, 100);
        style.visuals.widgets.hovered.bg_fill = egui::Color32::from_rgba_premultiplied(40, 40, 50, 150);
        style.visuals.widgets.active.bg_fill = Self::COBALT_BLUE;

        style.visuals.widgets.noninteractive.fg_stroke = egui::Stroke::new(1.0, Self::TEXT_SECONDARY);
        style.visuals.widgets.inactive.fg_stroke = egui::Stroke::new(1.0, Self::TEXT_PRIMARY);
        style.visuals.widgets.hovered.fg_stroke = egui::Stroke::new(1.0, egui::Color32::WHITE);
        style.visuals.widgets.active.fg_stroke = egui::Stroke::new(1.0, egui::Color32::WHITE);

        style.visuals.selection.bg_fill = Self::COBALT_BLUE.linear_multiply(0.2);
        style.visuals.selection.stroke = egui::Stroke::new(1.0, Self::COBALT_BLUE);

        style.visuals.window_corner_radius = egui::CornerRadius::same(4);
        style.visuals.widgets.noninteractive.corner_radius = egui::CornerRadius::same(2);
        style.visuals.widgets.inactive.corner_radius = egui::CornerRadius::same(2);
        style.visuals.widgets.hovered.corner_radius = egui::CornerRadius::same(2);
        style.visuals.widgets.active.corner_radius = egui::CornerRadius::same(2);

        style.spacing.item_spacing = egui::vec2(12.0, 10.0);
        style.spacing.window_margin = egui::Margin::same(16);

        ctx.set_global_style(style);

        Self::setup_fonts(ctx);
    }

    fn setup_fonts(ctx: &egui::Context) {
        let mut fonts = egui::FontDefinitions::default();

        // 1. Atkinson Hyperlegible (Primary)
        if let Ok(regular_data) = std::fs::read("assets/fonts/Atkinson-Regular.ttf") {
            fonts.font_data.insert(
                "atkinson-regular".to_owned(),
                egui::FontData::from_owned(regular_data).into(),
            );
        }

        if let Ok(bold_data) = std::fs::read("assets/fonts/Atkinson-Bold.ttf") {
            fonts.font_data.insert(
                "atkinson-bold".to_owned(),
                egui::FontData::from_owned(bold_data).into(),
            );
        }

        // 2. FontAwesome (Icons)
        if let Ok(icon_data) = std::fs::read("assets/fonts/FontAwesome-Solid.otf") {
            fonts.font_data.insert(
                "font-awesome".to_owned(),
                egui::FontData::from_owned(icon_data).into(),
            );
        }

        // Configure Proportional (Main text + Icons merged)
        fonts.families.get_mut(&egui::FontFamily::Proportional).unwrap()
            .insert(0, "atkinson-regular".to_owned());
        fonts.families.get_mut(&egui::FontFamily::Proportional).unwrap()
            .push("font-awesome".to_owned());

        // Configure Monospace
        fonts.families.get_mut(&egui::FontFamily::Monospace).unwrap()
            .push("atkinson-regular".to_owned());

        ctx.set_fonts(fonts);
    }

    pub fn glass_frame() -> egui::Frame {
        egui::Frame::NONE
            .fill(Self::BASE.linear_multiply(Self::PANEL_OPACITY))
            .stroke(egui::Stroke::new(1.0, Self::BORDER))
            .corner_radius(egui::CornerRadius::same(4))
            .inner_margin(12)
    }
}
