//! AbleOS Kernel Entrypoint

use {
    crate::{
        arch::hardware_random_u64,
        bootmodules::BootModules,
        //bootmodules::build_cmd,
        device_tree::DeviceTree,
        holeybytes::ExecThread,
        ipc::buffer::IpcBuffer,
    },
    hashbrown::HashMap,
    hbvm::mem::Address,
    limine::{Framebuffer, FramebufferRequest, NonNullPtr},
    log::{debug, info},
    spin::{Lazy, Mutex},
};

pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
    debug!("Entered kmain");

    // let kcmd = build_cmd("Kernel Command Line", cmdline);
    // trace!("Cmdline: {kcmd:?}");

    // for (i, bm) in boot_modules.iter().enumerate() {
    //     let name = format!("module-{}", i);
    //     let _bmcmd: XMLElement;
    //     if bm.cmd.len() >= 2 {
    //         // TODO: pass into the program
    //         // Pass CMDLine into an IPCBuffer and put the ptr to the IPCBuffer in r200
    //         _bmcmd = build_cmd(name, bm.cmd.clone());
    //         log::info!("{:?}", _bmcmd);
    //     }
    // }

    let dt = DEVICE_TREE.lock();

    // TODO(Able): This line causes a deadlock
    info!("Device Tree: {}", dt);

    info!("Boot complete. Moving to init_system");

    // TODO: schedule the disk driver from the initramfs
    // TODO: schedule the filesystem driver from the initramfs
    // TODO: Schedule the VFS from initramfs
    // TODO: schedule the init system from the initramfs

    drop(dt);

    let fb1: &NonNullPtr<Framebuffer> = &FB_REQ.get_response().get().unwrap().framebuffers()[0];

    {
        use crate::alloc::string::ToString;
        let mut dt = DEVICE_TREE.lock();
        let mut disp = xml::XMLElement::new("display_0");

        disp.set_attribute("width", fb1.width);
        disp.set_attribute("height", fb1.height);
        disp.set_attribute("bits per pixel", fb1.bpp);
        disp.set_attribute("pitch", fb1.pitch);
        dt.devices.insert("Displays".to_string(), alloc::vec![disp]);
    }
    log::info!("Graphics initialised");
    log::info!(
        "Graphics front ptr {:?}",
        fb1.address.as_ptr().unwrap() as *const u8
    );

    let mut executor = crate::task::Executor::new(256);
    let bm_take = boot_modules.len();
    unsafe {
        for module in boot_modules.into_iter().take(bm_take) {
            let mut cmd = module.cmd;
            if cmd.len() > 2 {
                // // Remove the quotes
                // cmd.remove(0);
                // cmd.pop();
                cmd = &cmd[1..cmd.len()]
            }
            let cmd_len = cmd.len() as u64;

            log::info!("Spawning {} with arguments \"{}\"", module.path, cmd);

            executor.spawn(async move {
                let mut thr = ExecThread::new(&module.bytes, Address::new(0));
                if cmd_len > 0 {
                    thr.set_arguments(cmd.as_bytes().as_ptr() as u64, cmd_len);
                }
                if let Err(e) = thr.await {
                    log::error!("{e:?}");
                }
            })
        }

        info!("Random number: {}", hardware_random_u64());

        executor.run();
    };

    crate::arch::spin_loop()
}

pub static DEVICE_TREE: Lazy<Mutex<DeviceTree>> = Lazy::new(|| {
    let dt = DeviceTree::new();
    Mutex::new(dt)
});
pub static FB_REQ: FramebufferRequest = FramebufferRequest::new(0);

pub type IpcBuffers<'a> = HashMap<u64, IpcBuffer<'a>>;
pub static IPC_BUFFERS: Lazy<Mutex<IpcBuffers>> = Lazy::new(|| {
    let mut bufs = HashMap::new();
    let log_buffer = IpcBuffer::new(false, 0);
    let file_buffer = IpcBuffer::new(false, 0);

    bufs.insert(1, log_buffer);
    bufs.insert(2, file_buffer);

    Mutex::new(bufs)
});

#[test_case]
fn trivial_assertion() {
    trace!("trivial assertion... ");
    assert_eq!(1, 1);
    info!("[ok]");
}