extern crate log;
extern crate sdl2;

use env_logger::Builder;
use log::{debug, error, info, warn, LevelFilter};

mod application;
mod doomlevel;
mod wadfile;

use application::Application;
use doomlevel::DoomLevel;
use wadfile::WADFile;

fn find_sdl_gl_driver() -> Option<u32> {
    for (index, item) in sdl2::render::drivers().enumerate() {
        if item.name == "opengl" {
            return Some(index as u32);
        }
    }
    None
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wad_file = WADFile::from_path("WADs/doom1.wad")?;
    let level = DoomLevel::load_level(&wad_file, wad_file.get_index_from_name("e1m1").unwrap());

    println!(
        "WAD Path: {}
        
Header:
    Identifier: {}
    Lump Count: {}
    Initial Offset: {}
    Number of Found Lumps: {}",
        wad_file.wad_path,
        wad_file.identifier,
        wad_file.num_lumps,
        wad_file.init_offset,
        wad_file.get_directory_len()
    );

    println!("First 15 Directory Entries:");

    for i in 0..15 {
        println!(
            "\t{}\tName {}\tPosition {}\tSize {}",
            i,
            wad_file.get_lump_name(i),
            wad_file.get_lump_offset(i),
            wad_file.get_lump_size(i)
        );
    }

    println!("\nFirst 15 Vertex Entries for {}:", level.name);

    for i in 0..15 {
        println!(
            "\t{} - ({}, {})",
            i, level.vertexes[i].x, level.vertexes[i].y
        );
    }

    let test_linedef = 247;

    println!("\nLinedef Entry {} for {}:", test_linedef, level.name);

    println!(
        "Start Vertex: {}
End Vertex: {}
Flags: {}
Special Type: {}
Sector Tag: {}
Front Sidedef: {}
Back Sidedef: {}

Flags:
\tBlocks Players: {}
\tBlocks Monsters: {}
\tTwo Sided: {}
\tUpper Texture Unpegged: {}
\tLower Texture Unpegged: {}
\tSecret: {}
\tBlocks Sound: {}
\tNever Shows on Automap: {}
\tAlways Shows on Automap: {}",
        level.linedefs[test_linedef].start_vertex,
        level.linedefs[test_linedef].end_vertex,
        level.linedefs[test_linedef].flags,
        level.linedefs[test_linedef].special_type,
        level.linedefs[test_linedef].sector_tag,
        level.linedefs[test_linedef].front_sidedef,
        level.linedefs[test_linedef].back_sidedef,
        level.linedefs[test_linedef].blocks_players(),
        level.linedefs[test_linedef].blocks_monsters(),
        level.linedefs[test_linedef].is_two_sided(),
        level.linedefs[test_linedef].is_upper_unpegged(),
        level.linedefs[test_linedef].is_lower_unpegged(),
        level.linedefs[test_linedef].is_secret(),
        level.linedefs[test_linedef].blocks_sound(),
        level.linedefs[test_linedef].never_automap(),
        level.linedefs[test_linedef].always_automap()
    );

    Builder::new().filter_level(LevelFilter::Debug).init();

    debug!("Test of the DEBUG log!");
    info!("Test of INFO log!");
    warn!("Test of WARN log!");
    error!("Test of ERROR log!");

    let mut app = Application::new()?;

    app.run()?;

    Ok(())
}
