use std::time::{Instant, Duration};
use super::Figure;

/// This struct is responsible for logging the average frame duration to stdout
/// once a second.
pub struct FpsLog {
    last_second: Instant,
    avg_duration_ns: u64,
    ticks: u64,
}

impl FpsLog {
    pub fn new() -> FpsLog {
        FpsLog {
            last_second: Instant::now(),
            avg_duration_ns: 0,
            ticks: 0
        }
    }

    /// Dump the frame time to std out
    fn print(&self) {
        let frame_time_ms = self.avg_duration_ns / 1000000;
        println!("avg frame time: {}ns which is {}ms",
                 self.avg_duration_ns, frame_time_ms);
    }

    /// Reset internal state which is used to calculate frame duration
    fn reset(&mut self) {
        self.last_second = Instant::now();
        self.avg_duration_ns = 0;
        self.ticks = 0;
    }

    /// Update state for duration calculation
    fn add_frame_duration(&mut self, duration: Duration) {
        let scaled_avg = self.avg_duration_ns * self.ticks;
        let frame_ns = duration.subsec_nanos() as u64;

        self.ticks += 1;
        self.avg_duration_ns = (scaled_avg + frame_ns) / self.ticks;
    }
}

impl Figure for FpsLog {
    fn update(&mut self, duration: Duration) {
        if self.last_second.elapsed() > Duration::from_secs(1) 


        self.add_frame_duration(duration);
    }
}

{
            self.print();
            self.reset();
        }