use clap::Parser;

use dsa::args::DsaArgs;
use dsa::ui::run_app;
use emulator_core::{DsaImplementation, EmulatorController, processor::Emulator};

fn main() {
    let args = DsaArgs::parse();

    let mut emulator = Emulator::new();
    emulator.apply_memory_map(args.get_memory_map());

    let controller = EmulatorController::new(Box::new(emulator));

    //
    // if let Some(bin) = args.get_binary() {
    //     for (i, ch) in bin.chunks(4).enumerate() {
    //         let instruction = Instruction(u32::from_le_bytes(ch.try_into().unwrap()));
    //         let hex = ch
    //             .iter()
    //             .map(|b| format!("{:02X}", b))
    //             .collect::<Vec<_>>()
    //             .join(" ");
    //         let chars = ch
    //             .iter()
    //             .map(|b| {
    //                 if b.is_ascii_graphic() {
    //                     *b as char
    //                 } else {
    //                     '.'
    //                 }
    //             })
    //             .collect::<String>();
    //         println!("{:04X}: {:<12} {:4} {:?}", i * 4, hex, chars, instruction);
    //     }
    //
    //     let program: Vec<Page> = Page::paginate(&bin).collect();
    //
    //     for (i, page) in program.iter().enumerate() {
    //         emulator.memory_mut().write_page(i as u32 * 4096, &page);
    //     }
    // }
    
    if args.headless() {
        run_server(&args);
    } else {
        run_app(controller).unwrap()
    }
}

fn run_server(args: &DsaArgs) {
    if args.headless() {
        println!("Running in headless mode");
    } else {
        println!("Running in GUI mode");
    }
}
