use embassy_time::{Duration, Instant};

#[derive(Debug)]
pub struct IdleClock {
    duration: Duration,
    last_active: Instant,
    fired: bool
}

impl IdleClock {
    pub fn new(duration: Duration) -> Self {
        Self {
            last_active: Instant::now(),
            duration,
            fired: false
        }
    }

    pub fn wake(&mut self) -> bool {
        self.last_active = Instant::now();
        if self.fired {
            self.fired = false;
            true
        } else {
            false
        }
    }

    pub fn check(&mut self) -> bool {
        if !self.fired && Instant::now().duration_since(self.last_active) >= self.duration {
            self.fired = true;
            true
        } else {
            false
        }
    }
}