use ratatui::{
    style::{Style, Stylize},
    widgets::Widget,
};

pub struct Keypad {
    keys: [bool; 0x10],
}

impl Keypad {
    pub fn push_key(&mut self, key_index: u8) {
        self.keys[key_index as usize] = true;
    }

    pub fn release_key(&mut self, key_index: u8) {
        self.keys[key_index as usize] = false;
    }

    pub fn key_state(&self, key_index: u8) -> bool {
        self.keys[key_index as usize]
    }
}

impl Widget for Keypad {
    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
    where
        Self: Sized,
    {
        let mut working_string = String::new();
        for i in 0..16 {
            working_string += if self.key_state(i) { "X" } else { "O" }
        }

        let style = Style::new().cyan();
        buf.set_string(0, 0, working_string, style);
    }
}
