use crate::{consts, core::event_bus::EventBus};
use godot::{
    classes::{IProgressBar, ProgressBar},
    prelude::*,
};

#[derive(GodotClass)]
#[class(base=ProgressBar)]
pub struct LiftProgressBar {
    event_bus: Option<Gd<EventBus>>,
    base: Base<ProgressBar>,
}

#[godot_api]
impl IProgressBar for LiftProgressBar {
    fn init(base: Base<ProgressBar>) -> Self {
        Self {
            event_bus: None,
            base,
        }
    }

    fn ready(&mut self) {
        let mut bus = self.base().get_node_as::<EventBus>(consts::EVENT_BUS_PATH);

        bus.connect(
            consts::LIFT_PROGRESS,
            &self.base().callable("on_lift_progress"),
        );

        self.event_bus = Some(bus);

        self.base_mut().set_min(0.0);
        self.base_mut().set_max(1.0);
        self.base_mut().set_value(0.0);
    }
}

#[godot_api]
impl LiftProgressBar {
    #[func]
    pub fn on_lift_progress(&mut self, progress: f32) {
        self.base_mut().set_value(progress as f64);
    }
}
