#![allow(dead_code)]

use std::mem::take;
use std::time::Instant;

use specs::{System, Write};

use crate::resources::Global;

pub struct Process {
    last_frame: Instant,
    time: f32,
    count: usize,
}

impl Default for Process {
    fn default() -> Self {
        Self {
            last_frame: Instant::now(),
            time: 0.0,
            count: 0,
        }
    }
}

impl<'a> System<'a> for Process {
    type SystemData = Write<'a, Global>;

    fn run(&mut self, mut global: Self::SystemData) {
        let now = Instant::now();

        global.delta = (now - self.last_frame).as_secs_f32();
        self.last_frame = now;
        self.time += global.delta;
        self.count += 1;

        if self.time >= 2.0 {
            self.time = 0.0;
            self.count = 0;

            global.fps = 0;
        } else if self.time >= 1.0 {
            self.time -= 1.0;

            global.fps = take(&mut self.count);
        }
    }
}
