// Main entry to Render stuff
// i.e r_main c/h
mod r_data;
mod r_defs;
mod r_plane;


use crate::doomdef::SCREENWIDTH;
use crate::math::{fixed_t, FRACUNIT};
use crate::m_menu::{detailLevel, screenBlocks};
use r_data::R_InitData;
use r_defs::lighttable_t;
use r_plane::R_InitPlanes;

// Light constants
// Orignal source comment: Now why not 32 levels here?
pub const LIGHTLEVELS: i32 = 16;
pub const MAXLIGHTSCALE: i32 = 48;
pub const LIGHTSCALESHIFT: i32 = 12;
pub const MAXLIGHTZ: i32 = 128;
pub const LIGHTZSHIFT: i32 = 20;

#[allow(non_upper_case_globals)]
pub static mut scalelight: [[*mut lighttable_t; MAXLIGHTSCALE as usize]; LIGHTLEVELS as usize] = 
    [[std::ptr::null_mut(); MAXLIGHTSCALE as usize]; LIGHTLEVELS as usize];

#[allow(non_upper_case_globals)]
pub static mut scalelightfixed: [*mut lighttable_t; MAXLIGHTSCALE as usize] = 
    [std::ptr::null_mut(); MAXLIGHTSCALE as usize];

#[allow(non_upper_case_globals)]
pub static mut zlight: [[*mut lighttable_t; MAXLIGHTZ as usize]; LIGHTLEVELS as usize] = 
    [[std::ptr::null_mut(); MAXLIGHTZ as usize]; LIGHTLEVELS as usize];

/// R_InitLightTables
/// Only inits the zlight table,
///  because the scalelight table changes with view size.
pub const DISTMAP:i32 = 2;


/// Number of diminishing brightness levels.
/// There a 0-31, i.e. 32 LUT in the COLORMAP lump.
pub const NUMCOLORMAPS: i32 =  32;

#[allow(non_upper_case_globals)]
static mut viewcos: fixed_t = fixed_t{value: 0};

#[allow(non_upper_case_globals)]
static mut viewsin: fixed_t = fixed_t{value: 0};

#[allow(non_upper_case_globals)]
static mut setsizeneeded: bool = false;

#[allow(non_upper_case_globals)]
static mut setblocks: i32 = 0;

#[allow(non_upper_case_globals)]
static mut setdetail: i32 = 0;



#[allow(non_snake_case)]
fn R_InitPointToAngle() {
    // this is unused in the original source
    // we get this data from tables
}

#[allow(non_snake_case)]
fn R_InitTables() {
    // this is unused in the original source
    // we get this data from tables
}

#[allow(non_snake_case)]
fn R_InitLightTables() {
    let mut startmap: i32;
    let mut scale: fixed_t;
    let mut level: i32;
    for i in 0..(LIGHTLEVELS as usize) {
        startmap = ((LIGHTLEVELS - 1 - (i as i32)) * 2) * NUMCOLORMAPS/LIGHTLEVELS;
        for j in 0..(MAXLIGHTZ as usize) {
            scale = fixed_t::FixedDiv(fixed_t{value: (SCREENWIDTH/2*FRACUNIT)}, fixed_t{value: ((j as i32) + 1) << LIGHTZSHIFT});
            scale >>= LIGHTSCALESHIFT;

            level = startmap - scale.value/DISTMAP;
            level = level.clamp(0, NUMCOLORMAPS - 1);

            // zlight[i][j] = colormaps + level * 256; //TODO: Implement zlight
        }
    }
}



/* // TODO: Implement player_t
pub fn R_RenderPlayerView(player: *const player_t) {

} 
*/

#[allow(non_snake_case)]
pub fn R_Init() {
    R_InitData();
    println!("R_InitData");
    R_InitPointToAngle();
    println!("R_InitPointToAngle");
    R_InitTables();
    // viewwidth / viewheight / detailLevel are set by the defaults
    println!("R_InitTables");
    unsafe {R_SetViewSize(screenBlocks, detailLevel);}
    R_InitPlanes();
    println!("R_InitPlanes");
    R_InitLightTables();
    println!("R_InitLightTables");
    //R_InitSkyMap();
    println!("R_InitSkyMap");
    //R_InitTranslationTables();
    println!("R_InitTranslationTables")

    // framecount = 0;
}

#[allow(non_snake_case)]
pub fn R_SetViewSize(blocks: i32, detail: i32) {
    unsafe {
        setsizeneeded = true;
        setblocks = blocks;
        setdetail = detail;
    }
}