use std::{fs::File, io::Read};

pub struct WADFile {
    pub(super) wad_path: String,
    pub(super) identifier: String,
    pub(super) num_lumps: u32,
    pub(super) init_offset: u32,
}

impl WADFile {
    pub fn from_path(path: &str) -> Self {
        let header_data = get_header_data(path);

        Self {
            wad_path: path.to_string(),
            identifier: header_data.0,
            num_lumps: header_data.1,
            init_offset: header_data.2,
        }
    }
}

fn get_header_data(path: &str) -> (String, u32, u32) {
    let mut wad_data = File::open(&path).unwrap();

    let mut header = [0; 12];

    wad_data.read(&mut header).unwrap();

    let mut wad_type = String::with_capacity(4);

    wad_type.push(header[0] as char);
    wad_type.push(header[1] as char);
    wad_type.push(header[2] as char);
    wad_type.push(header[3] as char);

    let lump_bytes: [u8; 4] = [header[4], header[5], header[6], header[7]];
    let offset_bytes: [u8; 4] = [header[8], header[9], header[10], header[11]];

    (
        wad_type,
        u32::from_le_bytes(lump_bytes),
        u32::from_le_bytes(offset_bytes),
    )
}
