//! Contains time-keeping systems.

use std::time::Instant;

pub struct Metrics {
    previous: Instant,
    elapsed: f64,
    frame_time: f64,
}

impl Metrics {
    /// Gets the elapsed time since the last reset.
    pub fn elapsed(&self) -> f64 {
        self.elapsed
    }

    /// Returns the differnet between the last frame and the current one.
    pub fn frame_time(&self) -> f64 {
        self.frame_time
    }

    /// Updates the elapsed time.
    pub fn measure(&mut self) {
        self.frame_time = self.previous.elapsed().as_secs_f64();
        self.elapsed += self.frame_time;
        self.previous = Instant::now();
    }

    /// Resets the elapsed time which begins another recording.
    pub fn reset_elapsed(&mut self) {
        self.elapsed = 0.;
    }
}

impl Default for Metrics {
    fn default() -> Self {
        let previous = Instant::now();
        let elapsed = 0.;
        let frame_time = 0.;

        Self { previous, elapsed, frame_time }
    }
}
