//! AbleOS Kernel Entrypoint

// use crate::arch::sloop;
use {
    crate::{
        bootmodules::{build_cmd, BootModules},
        device_tree::DeviceTree,
        scheduler::Scheduler,
    },
    alloc::format,
    log::{debug, info, trace},
    spin::{Lazy, Mutex},
    xml::XMLElement,
};

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
            _bmcmd = build_cmd(name, bm.cmd.clone());
        }
    }

    let dt = DEVICE_TREE.lock();

    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 init system from the initramfs

    // capabilities::example();

    let mut sched = Scheduler::new();

    sched.new_process(boot_modules[0].bytes.clone());
    sched.new_process(boot_modules[1].bytes.clone());

    sched.run();
    // sloop();
}

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

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