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

#[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) {
        ui.heading(t!("bootloader_title"));
        ui.label(t!("bootloader_description"));
        ui.add_space(12.0);

        egui::Grid::new("bootloader_grid")
            .num_columns(2)
            .spacing([12.0, 10.0])
            .show(ui, |ui| {
                // Hedef aygıt
                ui.label(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(egui::Color32::from_rgb(120, 120, 140)),
                    );
                });
                ui.end_row();

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

                // GRUB şifresi
                ui.label(t!("bootloader_password_label"));
                ui.horizontal(|ui| {
                    if self.show_password {
                        ui.text_edit_singleline(&mut state.bootloader_password);
                    } else {
                        ui.add(egui::TextEdit::singleline(&mut state.bootloader_password).password(true));
                    }
                    if ui.small_button(if self.show_password { "🙈" } else { "👁" }).clicked() {
                        self.show_password = !self.show_password;
                    }
                    if !state.bootloader_password.is_empty() {
                        ui.colored_label(egui::Color32::from_rgb(200, 180, 40), "⚠");
                    }
                });
                ui.end_row();

                // Kernel parametreleri
                ui.label(t!("bootloader_kernel_label"));
                ui.horizontal(|ui| {
                    ui.text_edit_singleline(&mut state.kernel_options);
                    ui.label(
                        egui::RichText::new(t!("bootloader_kernel_hint"))
                            .size(11.0)
                            .color(egui::Color32::from_rgb(120, 120, 140)),
                    );
                });
                ui.end_row();
            });
    }

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