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_f32, 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 = 18.0;
    let toplam_genislik = ui.available_width() * width_percent;
    
    let buton_genislik = 18.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));

    // Remixicon karakterlerini kullanıyoruz
    let goz_ikonu = if *show_password { "\u{ECB5}" } else { "\u{ECB7}" };

    eframe::egui::Frame::none()
        .fill(eframe::egui::Color32::WHITE)
        .stroke(eframe::egui::Stroke::new(1.0_f32, 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);

                // 2. Remixicon İkonlu Buton
                let btn = eframe::egui::Button::new(
                    eframe::egui::RichText::new(goz_ikonu)
                        .size(14.0)
                        .color(eframe::egui::Color32::from_rgb(120, 120, 120))
                )
                .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();
                }
            }
        });
    });
}