use eframe::egui;

#[macro_export]
macro_rules! draw_icon {
    ($ui:expr, $path:literal, $size:expr, $color:expr) => {
        $ui.add(
            eframe::egui::Image::new(eframe::egui::include_image!($path))
                .max_width($size)
                .max_height($size)
                .tint($color),
        )
    };
}

pub fn form_input_text(
    ui: &mut eframe::egui::Ui, 
    text: &mut String, 
)-> eframe::egui::Response {

    let width_percent = 0.90; // Mevcut genişliğin %90'ı
    let height = 32.0;       // Standart yükseklik
    let hesaplanan_genislik = ui.available_width() * width_percent;
    let boyut = eframe::egui::vec2(hesaplanan_genislik, height);
    
    ui.spacing_mut().button_padding = eframe::egui::vec2(10.0, 10.0); 
    ui.add_sized(
        boyut,
        eframe::egui::TextEdit::singleline(text)
            .vertical_align(eframe::egui::Align::Center)
    )
}

pub fn form_input_password(
    ui: &mut eframe::egui::Ui, 
    text: &mut String,
    show_password: &mut bool,
) -> eframe::egui::Response {
    let width_percent = 0.90;
    let height = 32.0;
    let toplam_genislik = ui.available_width() * width_percent;
    let dugme_genislik = 40.0;
    let alan_genislik = toplam_genislik - dugme_genislik - 4.0;
    let boyut = eframe::egui::vec2(alan_genislik, height);

    ui.spacing_mut().button_padding = eframe::egui::vec2(10.0, 10.0);

    ui.horizontal(|ui| {
        let r = ui.add_sized(
            boyut,
            eframe::egui::TextEdit::singleline(text)
                .password(!*show_password)
                .vertical_align(eframe::egui::Align::Center),
        );
        let göz = if *show_password { "🙈" } else { "👁" };
        if ui.add(
            eframe::egui::Button::new(göz)
                .min_size(eframe::egui::vec2(dugme_genislik, height))
        ).clicked() {
            *show_password = !*show_password;
        }
        r
    }).inner
}

pub fn form_label(ui: &mut eframe::egui::Ui, text: &str) {
    ui.label(
        eframe::egui::RichText::new(text)
            .strong()
            .color(crate::ui::theme::c_text()) // Atomic temandan rengi çekiyor
    );
}

pub fn form_checkbox(ui: &mut eframe::egui::Ui, checked: &mut bool, text: &str) {
    ui.checkbox(checked, eframe::egui::RichText::new(text).strong().color(crate::ui::theme::c_text()));
}



pub fn language_selector_ui(ui: &mut eframe::egui::Ui, state: &mut crate::installer::GlobalState) {
    ui.horizontal(|ui| {
        // Satır yüksekliğini tam olarak 32px'e eşitliyoruz
        ui.set_height(32.0);

        // 1. Sol taraftaki ikon alanı
        ui.vertical(|ui| {
            ui.add_space(4.0);
            ui.add(
                eframe::egui::Image::new(eframe::egui::include_image!("../assets/icons/language-icon.svg"))
                    .max_width(40.0)
            );
        });
        
        ui.add_space(6.0);

        // 2. ComboBox alanı (%90 genişlik payı)
        let available_width = ui.available_width() * 0.90;
        let current_lang_label = if state.language == "tr" { "Türkçe" } else { "English" };

        let languages = [
            ("tr", "Türkçe", eframe::egui::include_image!("../assets/flags/tr.svg")),
            ("en", "English", eframe::egui::include_image!("../assets/flags/us.svg")),
        ];

        // Standart egui makrosu - Tema motoru sayesinde otomatik olarak oval ve renk geçişli hissi verir
        eframe::egui::ComboBox::from_id_source("language_select_combo")
            .selected_text(current_lang_label)
            .width(available_width) // Seçim kutusunu yatayda genişletir
            .show_ui(ui, |ui| {
                // Açılan alt listenin genişliğini de üst butonla eşitliyoruz (HTML SelectBox Mantığı)
                ui.set_min_width(available_width);
                ui.set_max_width(available_width);

                for (code, label, image_source) in languages.iter() {
                    let item_height = 28.0;
                    
                    // Eleman kutularını sarmalayarak tam genişlikte tıklanabilir yapıyoruz
                    let (rect, item_resp) = ui.allocate_exact_size(
                        egui::vec2(available_width, item_height),
                        egui::Sense::click(),
                    );
                    {
                        use egui::epaint::Mesh;
                        let top = egui::Color32::from_rgb(249, 249, 249);
                        let bot = egui::Color32::from_rgb(233, 233, 233);
                        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));
                    }
                    ui.put(rect, |ui: &mut egui::Ui| {
                        ui.horizontal(|ui| {
                            ui.add_space(6.0);
                            ui.add(eframe::egui::Image::new(image_source.clone()).max_width(18.0));
                            ui.add_space(4.0);
                            ui.label(*label);
                        }).response
                    });
                    if item_resp.clicked() {
                        state.language = code.to_string();
                    }
                    item_resp.on_hover_cursor(egui::CursorIcon::PointingHand);
                }
            });
    });
}