use super::calendar::SECONDSPERDAY;

pub const NEPTUNEDAYSECONDS: f64 = 57996.0;

pub struct TimeScale {
    pub simulationtimes: f64,
    pub realtimes: f64,
    pub speedmultiplier: f64,
    pub paused: bool,
}

impl TimeScale {
    pub fn new(speedmultiplier: f64) -> Self {
        Self {
            simulationtimes: 0.0,
            realtimes: 0.0,
            speedmultiplier,
            paused: false,
        }
    }

    pub fn realtime() -> Self {
        Self::new(1.0)
    }

    pub fn fastforward(factor: f64) -> Self {
        Self::new(factor)
    }

    pub fn slowmotion(factor: f64) -> Self {
        Self::new(1.0 / factor)
    }

    pub fn step(&mut self, realdts: f64) {
        self.realtimes += realdts;
        if !self.paused {
            self.simulationtimes += realdts * self.speedmultiplier;
        }
    }

    pub fn pause(&mut self) {
        self.paused = true;
    }

    pub fn resume(&mut self) {
        self.paused = false;
    }

    pub fn togglepause(&mut self) {
        self.paused = !self.paused;
    }

    pub fn setspeed(&mut self, multiplier: f64) {
        self.speedmultiplier = multiplier;
    }

    pub fn simulationdt(&self, realdts: f64) -> f64 {
        if self.paused {
            0.0
        } else {
            realdts * self.speedmultiplier
        }
    }

    pub fn simhours(&self) -> f64 {
        self.simulationtimes / 3600.0
    }

    pub fn simdays(&self) -> f64 {
        self.simulationtimes / SECONDSPERDAY
    }

    pub fn simneptunedays(&self) -> f64 {
        self.simulationtimes / NEPTUNEDAYSECONDS
    }

    pub fn simneptuneyears(&self) -> f64 {
        self.simulationtimes / (SECONDSPERDAY * 60182.0)
    }
}
