mod directory;

use directory::Directory;
use std::{
    fs::File,
    io::{Read, Seek, SeekFrom},
};

pub struct WADFile {
    pub wad_path: String,
    pub identifier: String,
    pub num_lumps: u32,
    pub init_offset: u32,
    lump_directory: Vec<Directory>,
}

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

        let mut lump_dir: Vec<Directory> = Vec::with_capacity(header_data.1 as usize);

        get_lump_dir(path, &header_data.2, &header_data.1, &mut lump_dir);

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

    pub fn get_lump_name(&self, index: u32) -> String {
        self.lump_directory[index as usize].lump_name.to_owned()
    }

    pub fn get_lump_offset(&self, index: u32) -> u32 {
        self.lump_directory[index as usize].lump_pos.to_owned()
    }

    pub fn get_lump_size(&self, index: u32) -> u32 {
        self.lump_directory[index as usize].lump_size.to_owned()
    }

    pub fn get_directory_len(&self) -> usize {
        self.lump_directory.len()
    }

    pub fn get_index_from_name(&self, lump_name: &str) -> Option<u32> {
        let mut search_lump = lump_name.to_ascii_uppercase();

        for _ in 0..8 - search_lump.len() {
            search_lump.push(0 as char);
        }

        for i in 0..self.num_lumps {
            if self.lump_directory[i as usize].lump_name == search_lump {
                return Some(i);
            }
        }

        None
    }
}

fn get_lump_dir(path: &str, dir_offset: &u32, num_lumps: &u32, lump_buffer: &mut Vec<Directory>) {
    let mut wad_data = File::open(&path).unwrap();
    wad_data
        .seek(SeekFrom::Start(dir_offset.to_owned() as u64))
        .unwrap();

    for _ in 0..num_lumps.to_owned() {
        let mut lump_pos = [0; 4];
        let mut lump_size = [0; 4];
        let mut lump_name = [0; 8];
        let mut name = String::with_capacity(8);

        wad_data.read(&mut lump_pos).unwrap();
        wad_data.read(&mut lump_size).unwrap();
        wad_data.read(&mut lump_name).unwrap();

        for letter in 0..lump_name.len() {
            name.push(lump_name[letter] as char);
        }

        lump_buffer.push(Directory {
            lump_pos: u32::from_le_bytes(lump_pos),
            lump_size: u32::from_le_bytes(lump_size),
            lump_name: name,
        })
    }
}

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),
    )
}
