use eframe::egui;
use rust_i18n::t;
use crate::installer::{GlobalState, InstallerStep};
use crate::ui::theme;
use crate::funct::{form_label, form_input_text, form_input_password};

#[derive(Default)]
pub struct BootloaderStep {
    show_password: bool,
    timeout_str: String,
}

impl InstallerStep for BootloaderStep {
    fn name(&self) -> String {
        t!("bootloader").to_string()
    }

    fn on_enter(&mut self, state: &mut GlobalState) {
        if self.timeout_str.is_empty() {
            self.timeout_str = state.bootloader_timeout.to_string();
        }
        if state.bootloader_device.is_empty() {
            if let Some(ref disk) = state.selected_disk {
                state.bootloader_device = disk.clone();
            }
        }
    }

    fn show(&mut self, ui: &mut egui::Ui, state: &mut GlobalState) {
        theme::section_heading(ui, &t!("bootloader_title"));
        ui.label(
            egui::RichText::new(t!("bootloader_description"))
                .color(theme::c_text_dim())
                .size(13.0),
        );
        ui.add_space(12.0);

        // ─── Önyükleyici Formu ──────────────────────────────────
        egui::Frame::group(ui.style())
            .fill(theme::c_bg_widget())
            .rounding(6.0)
            .inner_margin(egui::Margin::symmetric(10.0, 10.0))
            .show(ui, |ui| {
                ui.set_width(ui.available_width());
                ui.vertical(|ui| {
                    ui.add_space(10.0);

                    // Hedef aygıt
                    form_label(ui, &t!("bootloader_device_label"));
                    ui.horizontal(|ui| {
                        let mut device_list: Vec<String> = state.available_disks.iter()
                            .map(|d| d.name.clone())
                            .collect();
                        if device_list.is_empty() {
                            device_list.push(state.bootloader_device.clone());
                        }
                        let current = if state.bootloader_device.is_empty() {
                            device_list[0].clone()
                        } else {
                            state.bootloader_device.clone()
                        };
                        theme::show_combo_box(ui, "boot_device_select", current.as_str(), ui.available_width(), |ui, w| {
                            for dev in &device_list {
                                theme::selectable_value(ui, &mut state.bootloader_device, dev.clone(), dev.as_str(), w);
                            }
                        });
                    });
                    ui.label(
                        egui::RichText::new(t!("bootloader_device_hint"))
                            .size(11.0)
                            .color(theme::c_text_dim()),
                    );
                    ui.add_space(6.0);

                    // Timeout
                    form_label(ui, &t!("bootloader_timeout_label"));
                    ui.horizontal(|ui| {
                        ui.add(egui::Slider::new(&mut state.bootloader_timeout, 0..=60)
                            .text(t!("bootloader_timeout_suffix")));
                    });
                    ui.add_space(6.0);

                    // GRUB şifresi
                    form_label(ui, &t!("bootloader_password_label"));
                    ui.horizontal(|ui| {
                        let show_pwd = &mut self.show_password;
                        form_input_password(ui, &mut state.bootloader_password, show_pwd);
                    });
                    ui.add_space(6.0);

                    // Kernel parametreleri
                    form_label(ui, &t!("bootloader_kernel_label"));
                    ui.horizontal(|ui| {
                        form_input_text(ui, &mut state.kernel_options);
                    });
                    ui.label(
                        egui::RichText::new(t!("bootloader_kernel_hint"))
                            .size(11.0)
                            .color(theme::c_text_dim()),
                    );

                });
            });
    }

    fn is_complete(&self, state: &GlobalState) -> bool {
        !state.bootloader_device.is_empty()
    }
}
