#[derive(Clone, Copy)]
pub struct DelayTimer {
    counter: i32
}

impl DelayTimer {
    pub fn current(&self) -> i32 {
        self.counter
    }

    pub fn new() -> Self {
        DelayTimer {
            counter: 0xff
        }
    }

    pub fn reset(&mut self) {
        self.counter = 0xff;
    }

    pub fn set_timer(&mut self, new_value: i32) {
        self.counter = new_value
    }

    pub fn tick(&mut self) {
        if self.counter > 0 {
            self.counter -= 1;
        }
    }
}

#[cfg(test)]
mod test {
    use crate::chip8::sound_timer::SoundTimer;
    use super::*;

    #[test]
    fn smoke() {
        assert!(true)
    }

    #[test]
    fn ticks_reduce_time() {
        let mut st = DelayTimer::new();
        st.set_timer(100);
        st.tick();
        st.tick();
        st.tick();
        assert_eq!(st.current(), 97);
    }

    #[test]
    fn out_of_ticks_works() {
        let mut st = DelayTimer::new();
        st.set_timer(0);
        st.tick();
        st.tick();
        st.tick();
        assert_eq!(st.current(), 0);
    }
}