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 = 10.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); 
    egui::Frame::none()
    .fill(egui::Color32::from_rgb(255, 255, 255))
    .stroke(egui::Stroke::new(1.0, egui::Color32::from_gray(200)))
    .rounding(4.0)
    .inner_margin(eframe::egui::Margin::symmetric(2.0, 2.0))
    .show(ui, |ui| {
    ui.add_sized(
        boyut,
        eframe::egui::TextEdit::singleline(text)
            .vertical_align(eframe::egui::Align::Center)
            .frame(false),
    )
    }).inner
}


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 buton_genislik = 32.0;
    let alan_genislik = toplam_genislik - buton_genislik - 4.0; 

    let mut response: Option<eframe::egui::Response> = None;

    ui.visuals_mut().override_text_color = Some(eframe::egui::Color32::from_rgb(30, 30, 30));

    // SVG içeriklerini string olarak tanımlıyoruz
    let goz_acik_svg = r#"<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M2.036 12.322a1.012 1.012 0 0 1 0-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178Z" /><path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" /></svg>"#;
    let goz_kapali_svg = r#"<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M3.98 8.223A10.477 10.477 0 0 0 1.934 12C3.226 16.338 7.244 19.5 12 19.5c.993 0 1.953-.138 2.863-.395M6.228 6.228A10.451 10.451 0 0 1 12 4.5c4.756 0 8.773 3.162 10.065 7.498a10.522 10.522 0 0 1-4.293 5.774M6.228 6.228 3 3m3.228 3.228 3.65 3.65m7.894 7.894L21 21m-3.228-3.228-3.65-3.65m0 0a3 3 0 1 0-4.243-4.243m4.242 4.242L9.88 9.88" /></svg>"#;

    eframe::egui::Frame::none()
        .fill(eframe::egui::Color32::WHITE)
        .stroke(eframe::egui::Stroke::new(1.0, eframe::egui::Color32::from_rgb(200, 200, 200)))
        .rounding(6.0)
        .inner_margin(eframe::egui::Margin::symmetric(2.0, 2.0))
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                // 1. Şifre Metin Alanı
                let r = ui.add_sized(
                    eframe::egui::vec2(alan_genislik, height),
                    eframe::egui::TextEdit::singleline(text)
                        .password(!*show_password)
                        .frame(false)                        
                );
                response = Some(r);

                // Duruma göre ilgili SVG string'ini seçip byte dizisine (&[u8]) çeviriyoruz
                let (uri, svg_bytes) = if *show_password {
                    ("bytes://goz_acik.svg", goz_acik_svg.as_bytes())
                } else {
                    ("bytes://goz_kapali.svg", goz_kapali_svg.as_bytes())
                };

                // Image::from_bytes kullanarak imajı güvenli bir şekilde yüklüyoruz
                let svg_gosterici = eframe::egui::Image::from_bytes(uri, svg_bytes)
                    .tint(eframe::egui::Color32::from_rgb(120, 120, 120))
                    .max_width(18.0)
                    .max_height(18.0);

                // 2. SVG İkonlu Buton
                let btn = eframe::egui::Button::image(svg_gosterici)
                    .frame(false)
                    .min_size(eframe::egui::vec2(buton_genislik, height));

                if ui.add(btn).on_hover_cursor(eframe::egui::CursorIcon::PointingHand).clicked() {
                    *show_password = !*show_password;
                }
            });
        });

    response.expect("Metin alanı yüklenemedi")
}

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)
                    .tint(crate::ui::theme::c_text())
            );
        });
        
        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")),
        ];

        crate::ui::theme::show_combo_box(ui, "language_select_combo", current_lang_label, available_width, |ui, w| {
            for (code, label, image_source) in languages.iter() {
                let resp = crate::ui::theme::selectable_value_custom(ui, w, |ui, rect| {
                    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 resp.clicked() {
                    state.language = code.to_string();
                }
            }
        });
    });
}