use std::time::Instant;
use crate::support::gemma_egui_support::{EGuiFileList, GemmaEguiSupport};
use crate::support::gemma_egui_state::GemmaEGuiState;
use eframe::egui;
use egui::Ui;
use gemma::chip8::computer::Chip8Computer;
use gemma::chip8::computer_manager::Chip8ComputerManager;

mod support;

fn main() -> eframe::Result {
    println!("Taxation is Theft");

    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
        ..Default::default()
    };

  //  let mut state = GemmaEGuiState::default();
    let mut computer = Chip8ComputerManager::new();
    let mut cps_counter = 0;
    let mut last_counter_update = Instant::now();
    let cps_refresh = 5;

    let mut last_frame_start = Instant::now();

    eframe::run_simple_native("EGUI Emma", options, move |ctx, _frame| {
        egui::CentralPanel::default().show(ctx, |ui| {
            let local_computer = computer.state();
            //if state.display_video {
                GemmaEguiSupport::video_view(local_computer, ui);
           // }

            ui.heading("EGUI Gemma");

          //  if state.display_memory {
                GemmaEguiSupport::memory_view(&local_computer, ui);
          //  }

          //  if state.display_registers {
                GemmaEguiSupport::registers_view(&local_computer, ui);
          //  }
            computer.tick();
            ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
                if ui.button("Start").clicked() {
                    computer.core_should_run = true;
                }
                if ui.button("Step").clicked() {
                    computer.one_step = true;
                }

                if ui.button("Stop").clicked() {
                    computer.core_should_run = false;
                    //     state.is_running = false;
                }
                if ui.button("Reset").clicked() {
                    computer.reset();
                    //   state.is_running = false;
                }
            });
            //
            //       if ui.button(format!("Load {}", state.selected_rom_filename)).clicked() {
            // // load the bin...
            //         let read_bin = std::fs::read(PathBuf::from(format!("resources/roms/{}", state.selected_rom_filename))).unwrap();
            // // ...then feed the system.
            //         system.load_bytes_to_memory(0x200, &read_bin);
            //         println!("Loaded {}", state.selected_rom_filename);
            //     }
            //      EGuiFileList::display_path(PathBuf::from("resources/roms"), &mut state.selected_rom_filename, ui);

        });
    })
}
