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

pub const PACKAGE_GROUPS: &[(&str, &str, &[&str])] = &[
    ("office",      "Office Suite",     &["libreoffice", "calligra"]),
    ("multimedia",  "Multimedia",       &["vlc", "audacity", "gimp", "inkscape"]),
    ("development", "Development Tools",&["gcc", "make", "cmake", "git", "python3", "rust"]),
    ("network",     "Network Tools",    &["firefox", "chromium", "thunderbird", "filezilla"]),
    ("graphics",    "Graphics & Design",&["blender", "krita", "darktable"]),
    ("games",       "Games",            &["steam", "lutris", "supertuxkart"]),
    ("vm",          "Virtualization",   &["virtualbox", "qemu", "docker"]),
    ("server",      "Server",           &["nginx", "apache", "mariadb", "postgresql"]),
];

#[derive(Default)]
#[allow(dead_code)]
pub struct NetinstallStep;

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

    fn on_enter(&mut self, state: &mut GlobalState) {
        if state.selected_package_groups.is_empty() {
            // Varsayılan ofis ve network
            state.selected_package_groups = vec!["office".to_string(), "network".to_string()];
        }
    }

    fn show(&mut self, ui: &mut egui::Ui, state: &mut GlobalState) {
        ui.heading(t!("netinstall_title"));
        ui.label(t!("netinstall_description"));
        ui.add_space(10.0);

        let screen_width = ui.available_width();
        let cols = if screen_width > 700.0 { 3 } else if screen_width > 500.0 { 2 } else { 1 };

        egui::Grid::new("netinstall_grid")
            .num_columns(cols)
            .spacing([8.0, 4.0])
            .show(ui, |ui| {
                for (key, label, _pkgs) in PACKAGE_GROUPS {
                    let mut selected = state.selected_package_groups.contains(&key.to_string());
                    ui.checkbox(&mut selected, *label);
                    if selected && !state.selected_package_groups.contains(&key.to_string()) {
                        state.selected_package_groups.push(key.to_string());
                    } else if !selected {
                        state.selected_package_groups.retain(|k| k != key);
                    }
                }
            });

        ui.add_space(12.0);
        if !state.selected_package_groups.is_empty() {
            let total: usize = PACKAGE_GROUPS.iter()
                .filter(|(k, _, _)| state.selected_package_groups.contains(&k.to_string()))
                .map(|(_, _, pkgs)| pkgs.len())
                .sum();
            ui.label(format!("{} {} {}", total, t!("netinstall_package_count"), t!("netinstall_selected")));
        }
    }

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