use crate::solar_system::{SolarSystem, SystemId};

pub struct CurrentSystemWidget<'frame>
{
    star_systems: &'frame [SolarSystem],
    current_system: &'frame mut Option<SystemId>,
}

impl<'frame> CurrentSystemWidget<'frame> {
    pub fn new(
        star_systems: &'frame [SolarSystem],
        current_system: &'frame mut Option<SystemId>)
    -> Self {
        Self {
            star_systems,
            current_system
        }
    }
}

impl<'frame> egui::Widget for CurrentSystemWidget<'frame>
{
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        let selected_label = match self.current_system {
            Some(id) => &self.star_systems[*id].name(),
            None => "Click to open"
        };

        ui.vertical(|vertical| {
            vertical.label("Current System");
            let resp = egui::ComboBox::new("current_system", egui::WidgetText::default())
                .selected_text(selected_label)
                .show_ui(vertical, 
            |combo_box| {
                for system in self.star_systems.iter() {
                let entry_value = Some(system.id());
                combo_box.selectable_value(
                    self.current_system, 
                    entry_value,
                    system.name());
                }
            });

            resp.response
        }).inner
    }
}
