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

const DM_OPTIONS: &[&str] = &["sddm", "lightdm", "gdm"];
const DE_OPTIONS: &[&str] = &["plasma", "xfce", "gnome", "cinnamon", "budgie", "lxde", "lxqt", "mate", "lumina"];

#[derive(Default)]
pub struct DisplayManagerStep;

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

    fn on_enter(&mut self, state: &mut GlobalState) {
        let desktop = crate::installer::detect_desktop_environment();
        if !desktop.is_empty() {
            state.desktop_environment = desktop;
        }
    }

    fn show(&mut self, ui: &mut egui::Ui, state: &mut GlobalState) {
        ui.heading(t!("display_manager_title"));
        ui.label(t!("display_manager_description"));
        ui.add_space(12.0);

        egui::Grid::new("dm_grid")
            .num_columns(2)
            .spacing([12.0, 10.0])
            .show(ui, |ui| {
                // Ekran yöneticisi
                ui.label(t!("display_manager_select"));
                let dm_current = state.display_manager.clone();
                theme::show_combo_box(ui, "dm_combo", dm_current.as_str(), ui.available_width(), |ui, w| {
                    for dm in DM_OPTIONS {
                        theme::selectable_value(ui, &mut state.display_manager, dm.to_string(), *dm, w);
                    }
                });
                ui.end_row();

                // Masaüstü ortamı (seçilebilir)
                ui.label(t!("display_manager_de"));
                let de_current = if state.desktop_environment.is_empty() || state.desktop_environment == "default" {
                    "plasma".to_string()
                } else {
                    state.desktop_environment.clone()
                };
                theme::show_combo_box(ui, "de_combo", de_current.as_str(), ui.available_width(), |ui, w| {
                    for de in DE_OPTIONS {
                        theme::selectable_value(ui, &mut state.desktop_environment, de.to_string(), *de, w);
                    }
                });
                ui.end_row();

                // Otomatik oturum açma
                ui.label(t!("display_manager_autologin"));
                ui.checkbox(&mut state.autologin, "");
                ui.end_row();

                if state.autologin {
                    ui.label(t!("display_manager_autologin_user"));
                    ui.horizontal(|ui| {
                        let name = if !state.username.is_empty() {
                            state.username.clone()
                        } else {
                            t!("display_manager_autologin_hint").to_string()
                        };
                        ui.colored_label(egui::Color32::from_rgb(120, 180, 120), name);
                    });
                    ui.end_row();
                }
            });

        ui.add_space(8.0);
        ui.label(egui::RichText::new(t!("display_manager_note")).size(11.0).color(egui::Color32::from_rgb(140, 140, 160)));
    }

    fn is_complete(&self, _state: &GlobalState) -> bool {
        true
    }
}
