use imgui::{Condition, ImColor32, Ui};
use emmaemu::chip8::system_memory::Chip8SystemMemory;
use emmaemu::constants::{CHIP8_VIDEO_HEIGHT, CHIP8_VIDEO_WIDTH};

pub struct EmmaGui {}
const CELL_WIDTH: i32 = 5i32;
const CELL_HEIGHT: i32 = 5i32;

impl EmmaGui {
    pub fn hex_memory_display(bytes: Box<Vec<u8>>, rows: i32, cols: i32, ui: &Ui) {
        // display a block of data
        for current_row in 0..rows {
            for current_column in 0..cols {
                let data_offset = current_row * cols + current_column;
                ui.text(format!("{:02x}", bytes[data_offset as usize]));
            }
        }
    }

   pub fn system_memory_render(memory: Chip8SystemMemory, ui: &Ui) {
       ui.window("System Memory")
           .size([300.0, 100.0], Condition::FirstUseEver)
           .build(|| {
               ui.text("Rendering System Memory Here");
               let draw_list = ui.get_foreground_draw_list();
               let mut idx = 0;
               for row in 0..CHIP8_VIDEO_HEIGHT {
                   for column in 0..CHIP8_VIDEO_WIDTH {
                       let x_offset = column * CELL_WIDTH;
                       let y_offset = row * CELL_WIDTH;
                       let start_point = [x_offset as f32, y_offset as f32];
                       let end_point = [(x_offset + CELL_WIDTH) as f32,
                           (y_offset + CELL_HEIGHT) as f32
                       ];
                       let memory_offset = (row * CHIP8_VIDEO_WIDTH) + column;
                       let target_color = if memory.peek(memory_offset as u16) == 0 {
                           ImColor32::BLACK
                       } else {
                           ImColor32::WHITE
                       };
                       draw_list.add_rect([x_offset as f32, y_offset as f32],
                                          [(x_offset + CELL_WIDTH) as f32, (y_offset + CELL_HEIGHT) as f32],
                                          target_color).build();
                       idx += 1;
                   }
               }

           });
    }
}