//! Configuration Window for tuning the application's calculations and displays.

use egui;

use crate::app::OrbitSenseApp;
use crate::constants::DEFAULT_PASS_THRESHOLD_KM;

/// Renders the `File > Preferences` popup window to configure prediction thresholds.
pub fn render_preferences_window(app: &mut OrbitSenseApp, ctx: &egui::Context) {
    if !app.preferences_open {
        return;
    }

    let mut open = app.preferences_open;
    egui::Window::new("Preferences")
        .open(&mut open)
        .resizable(false)
        .collapsible(false)
        .show(ctx, |ui| {
            ui.heading("Display Options");

            ui.horizontal(|ui| {
                ui.label("Map Theme:");
                egui::ComboBox::from_id_salt("map_style")
                    .selected_text(match app.map_style {
                        crate::app::MapStyle::OpenStreetMap => "Light (OSM)",
                        crate::app::MapStyle::CartoDark => "Dark (CartoDB)",
                    })
                    .show_ui(ui, |ui| {
                        ui.selectable_value(
                            &mut app.map_style,
                            crate::app::MapStyle::OpenStreetMap,
                            "Light (OSM)",
                        );
                        ui.selectable_value(
                            &mut app.map_style,
                            crate::app::MapStyle::CartoDark,
                            "Dark (CartoDB)",
                        );
                    });
            });

            ui.checkbox(&mut app.show_orbital_trail, "Show Orbital Trail");

            ui.horizontal(|ui| {
                ui.label("Swath Footprint Color:");
                ui.color_edit_button_rgb(&mut app.swath_color);
            });
            ui.add(egui::Slider::new(&mut app.swath_opacity, 0.0..=1.0).text("Swath Opacity"));

            ui.separator();
            ui.heading("Prediction Settings");
            ui.horizontal(|ui| {
                ui.label("Overhead Pass Distance Threshold (km):");
                ui.add(
                    egui::DragValue::new(&mut app.pass_threshold_km)
                        .range(0.0..=10000.0)
                        .speed(10.0),
                );
            });
            if ui.button("Reset to Default").clicked() {
                app.pass_threshold_km = DEFAULT_PASS_THRESHOLD_KM;
            }
        });

    app.preferences_open = open;
}
