use uefi::{
    proto::media::file::{Directory, File, FileAttribute, FileMode},
    CStr16, Result, Status,
};

#[derive(Debug)]
pub struct Config {
    pub cmdline: [u8; 4096],
    pub cmdline_len: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            cmdline: [0; 4096],
            cmdline_len: 0,
        }
    }
}

impl Config {
    pub fn load(root: &mut Directory, path: &CStr16) -> Result<Self> {
        let file = match root.open(path, FileMode::Read, FileAttribute::empty()) {
            Ok(file) => file,
            Err(error) => {
                root.reset_entry_readout().ok();
                log::warn!("Couldn't open {path:?}: {error:?}");
                return Ok(Self::default());
            }
        };
        root.reset_entry_readout().ok();
        let mut this = Self::default();
        let mut file = file.into_regular_file().ok_or(Status::INVALID_PARAMETER)?;

        this.cmdline_len = file.read(&mut this.cmdline)?;

        Ok(this)
    }
}
