use eframe::egui::{self, Color32, Rect, Stroke};
use egui::epaint::Mesh;

/// Verilen `Visuals` yapısına ComboBox stillerini uygular.
pub fn apply_visuals(v: &mut egui::Visuals) {
    v.widgets.inactive.weak_bg_fill = Color32::from_rgb(224, 224, 224);
    v.widgets.hovered.weak_bg_fill  = Color32::from_rgb(200, 200, 200);
    v.widgets.active.weak_bg_fill   = Color32::from_rgb(180, 180, 180);

    v.widgets.inactive.fg_stroke = Stroke::new(1.0, Color32::from_rgb(50, 50, 50));
    v.widgets.hovered.fg_stroke  = Stroke::new(1.5, Color32::WHITE);
    v.widgets.active.fg_stroke   = Stroke::new(2.0, Color32::WHITE);

    v.widgets.inactive.bg_stroke = Stroke::new(1.0, Color32::from_rgb(155, 155, 155));
    v.widgets.hovered.bg_stroke  = Stroke::new(1.0, Color32::from_rgb(155, 155, 155));
    v.widgets.active.bg_stroke   = Stroke::new(1.0, Color32::from_rgb(155, 155, 155));
}

fn is_dark_ui(ui: &egui::Ui) -> bool {
    let bg = ui.visuals().panel_fill;
    bg.r() < 128 && bg.g() < 128 && bg.b() < 128
}

fn gradient_top(ui: &egui::Ui) -> Color32 {
    if is_dark_ui(ui) {
        Color32::from_rgb(40, 40, 45)
    } else {
        Color32::from_rgb(249, 249, 249)
    }
}

fn gradient_bot(ui: &egui::Ui) -> Color32 {
    if is_dark_ui(ui) {
        Color32::from_rgb(30, 30, 35)
    } else {
        Color32::from_rgb(233, 233, 233)
    }
}

/// Açık/koyu temaya uygun gradyan mesh arkaplanı çizer (dropdown öğeleri için).
pub fn draw_gradient_bg(ui: &mut egui::Ui, rect: Rect) {
    let top = gradient_top(ui);
    let bot = gradient_bot(ui);
    let mut mesh = Mesh::default();
    let idx = mesh.vertices.len() as u32;
    mesh.vertices.push(egui::epaint::Vertex {
        pos: egui::pos2(rect.left(), rect.top()),
        uv: egui::pos2(0.0, 0.0),
        color: top,
    });
    mesh.vertices.push(egui::epaint::Vertex {
        pos: egui::pos2(rect.right(), rect.top()),
        uv: egui::pos2(0.0, 0.0),
        color: top,
    });
    mesh.vertices.push(egui::epaint::Vertex {
        pos: egui::pos2(rect.right(), rect.bottom()),
        uv: egui::pos2(0.0, 0.0),
        color: bot,
    });
    mesh.vertices.push(egui::epaint::Vertex {
        pos: egui::pos2(rect.left(), rect.bottom()),
        uv: egui::pos2(0.0, 0.0),
        color: bot,
    });
    mesh.indices.extend_from_slice(&[idx, idx + 1, idx + 2, idx + 2, idx + 3, idx]);
    ui.painter().add(egui::Shape::mesh(mesh));
}
