use nox_planet::RampDirection;
use nox_raws::RAWS;

pub fn add_ramp_geometry(
    vb: &mut Vec<f32>,
    element_count: &mut u32,
    direction: RampDirection,
    x: f32,
    y: f32,
    z: f32,
    material_index: (usize, bool),
) {
    let rlock = RAWS.read();
    let mi = *rlock.matmap.get(material_index.0) as f32;
    let tex = if material_index.1 {
        rlock.materials.material_list.as_ref().unwrap()[material_index.0].constructed
    } else {
        rlock.materials.material_list.as_ref().unwrap()[material_index.0].base
    };

    match direction {
        RampDirection::NorthSouth => north_south(vb, element_count, x, y, z, mi, tex),
        RampDirection::SouthNorth => south_north(vb, element_count, x, y, z, mi, tex),
        RampDirection::EastWest => east_west(vb, element_count, x, y, z, mi, tex),
        RampDirection::WestEast => west_east(vb, element_count, x, y, z, mi, tex), //_ => {}
    }
}

fn north_south(
    vb: &mut Vec<f32>,
    element_count: &mut u32,
    x: f32,
    y: f32,
    z: f32,
    mi: f32,
    tex: f32,
) {
    let w = 1.0;
    let h = 1.0;
    let d = 1.0;

    let x0 = x;
    let x1 = x0 + w;
    let y0 = z;
    let y1 = y0 + d;
    let z0 = y;
    let z1 = z0 + h;

    let t0 = 0.0f32;
    let tw = w;
    let th = h;

    #[rustfmt::skip]
    let cube_geometry = [
        x0, y0, z0, t0, t0,   1.0, mi, tex,
        x1, y1, z0, tw, th,   1.0, mi, tex,
        x1, y0, z0, tw, t0,   1.0, mi, tex,
        x1, y1, z0, tw, th,   1.0, mi, tex,
        x0, y0, z0, t0, t0,   1.0, mi, tex,
        x0, y1, z0, t0, th,   1.0, mi, tex,

        // Left side
        x0, y0, z0, tw, th,  5.0,  mi, tex,
        x0, y1, z0, tw, t0,  5.0,  mi, tex,
        x0, y0, z1, t0, t0,  5.0,  mi, tex,


        // Right side
        x1, y0, z0,  tw, th,  3.0,  mi, tex,
        x1, y1, z0,  tw, t0,  3.0,  mi, tex,
        x1, y0, z1,  t0, t0,  3.0,  mi, tex,

        // Base - unchanged
        x0, y0, z0, tw, th,  5.0,  mi, tex,
        x1, y0, z0, tw, t0,  5.0,  mi, tex,
        x1, y0, z1, t0, t0,  5.0,  mi, tex,
        x1, y0, z1, t0, t0,  5.0,  mi, tex,
        x0, y0, z1, t0, th,  5.0,  mi, tex,
        x0, y0, z0, tw, th,  5.0,  mi, tex,

        // Top - needs to slope
        x1, y0, z1, tw, th,  6.0,    mi, tex,
        x1, y1, z0, tw, t0,  6.0,    mi, tex,
        x0, y1, z0, t0, t0,  6.0,    mi, tex,
        x0, y1, z0, t0, t0,  6.0,    mi, tex,
        x0, y0, z1, t0, th,  6.0,    mi, tex,
        x1, y0, z1, tw, th,  6.0,    mi, tex,
    ];
    vb.extend_from_slice(&cube_geometry);
    *element_count += 8;
}

// still needs work
fn south_north(
    vb: &mut Vec<f32>,
    element_count: &mut u32,
    x: f32,
    y: f32,
    z: f32,
    mi: f32,
    tex: f32,
) {
    let w = 1.0;
    let h = 1.0;
    let d = 1.0;

    let x0 = x;
    let x1 = x0 + w;
    let y0 = z;
    let y1 = y0 + d;
    let z0 = y;
    let z1 = z0 + h;

    let t0 = 0.0f32;
    let tw = w;
    let th = h;

    #[rustfmt::skip]
    let cube_geometry = [
        /*
        x0, y0, z0,    1.0,  t0, t0,
        x1, y1, z0,    1.0,  tw, th,
        x1, y0, z0,    1.0,  tw, t0,
        x1, y1, z0,    1.0,  tw, th,
        x0, y0, z0,    1.0,  t0, t0,
        x0, y1, z0,    1.0,  t0, th,*/

        x0, y0, z1, t0, t0,   2.0,  mi, tex,
        x1, y0, z1, tw, t0,   2.0,  mi, tex,
        x1, y1, z1, tw, th,   2.0,  mi, tex,
        x1, y1, z1, tw, th,   2.0,  mi, tex,
        x0, y1, z1, t0, th,   2.0,  mi, tex,
        x0, y0, z1, t0, t0,   2.0,  mi, tex,

        /*
        x0, y1, z1,   5.0,   tw, th,
        x0, y1, z0,   5.0,   tw, t0,
        x0, y0, z0,   5.0,   t0, t0,
        x0, y0, z0,   5.0,   t0, t0,
        x0, y0, z1,   5.0,   t0, th,
        x0, y1, z1,   5.0,   tw, th,
        */
        x0, y0, z0, tw, th,  5.0,  mi, tex,
        x0, y0, z1, tw, t0,  5.0,  mi, tex,
        x0, y1, z1, t0, t0,  5.0,  mi, tex,

        /*
        x1, y1, z1,    3.0,  tw, th,
        x1, y0, z0,    3.0,  t0, t0,
        x1, y1, z0,    3.0,  tw, t0,
        x1, y0, z0,    3.0,  t0, t0,
        x1, y1, z1,    3.0,  tw, th,
        x1, y0, z1,    3.0,  t0, th,
        */
        x1, y0, z0,  tw, th,  3.0, mi, tex,
        x1, y1, z1,  t0, t0,  3.0, mi, tex,
        x1, y0, z1,  tw, t0,  3.0, mi, tex,

        // Base
        x0, y0, z0, tw, th,  5.0, mi, tex,
        x1, y0, z0, tw, t0,  5.0, mi, tex,
        x1, y0, z1, t0, t0,  5.0, mi, tex,
        x1, y0, z1, t0, t0,  5.0, mi, tex,
        x0, y0, z1, t0, th,  5.0, mi, tex,
        x0, y0, z0, tw, th,  5.0, mi, tex,

        // Top
        x1, y1, z1, tw, th,  7.0,  mi, tex,
        x1, y0, z0, tw, t0,  7.0,  mi, tex,
        x0, y0, z0, t0, t0,  7.0,  mi, tex,
        x0, y0, z0, t0, t0,  7.0,  mi, tex,
        x0, y1, z1, t0, th,  7.0,  mi, tex,
        x1, y1, z1, tw, th,  7.0,  mi, tex,
    ];
    vb.extend_from_slice(&cube_geometry);
    *element_count += 8;
}

fn east_west(
    vb: &mut Vec<f32>,
    element_count: &mut u32,
    x: f32,
    y: f32,
    z: f32,
    mi: f32,
    tex: f32,
) {
    let w = 1.0;
    let h = 1.0;
    let d = 1.0;

    let x0 = x;
    let x1 = x0 + w;
    let y0 = z;
    let y1 = y0 + d;
    let z0 = y;
    let z1 = z0 + h;

    let t0 = 0.0f32;
    let tw = w;
    let th = h;

    #[rustfmt::skip]
    let cube_geometry = [
        /*
        x0, y0, z0,    1.0,  t0, t0,
        x1, y1, z0,    1.0,  tw, th,
        x1, y0, z0,    1.0,  tw, t0,
        x1, y1, z0,    1.0,  tw, th,
        x0, y0, z0,    1.0,  t0, t0,
        x0, y1, z0,    1.0,  t0, th,*/
        x1, y0, z0, t0, t0,   1.0, mi, tex,
        x0, y0, z0, tw, th,   1.0, mi, tex,
        x0, y1, z0, tw, t0,   1.0, mi, tex,

        /*
        x0, y0, z1,    2.0,   t0, t0,
        x1, y0, z1,    2.0,   tw, t0,
        x1, y1, z1,    2.0,   tw, th,
        x1, y1, z1,    2.0,   tw, th,
        x0, y1, z1,    2.0,   t0, th,
        x0, y0, z1,    2.0,   t0, t0,
        */
        x1, y0, z1,  t0, t0,  2.0, mi, tex,
        x0, y0, z1,  tw, t0,  2.0, mi, tex,
        x0, y1, z1,  tw, th,  2.0, mi, tex,

        x0, y1, z1, tw, th,  5.0, mi, tex,
        x0, y1, z0, tw, t0,  5.0, mi, tex,
        x0, y0, z0, t0, t0,  5.0, mi, tex,
        x0, y0, z0, t0, t0,  5.0, mi, tex,
        x0, y0, z1, t0, th,  5.0, mi, tex,
        x0, y1, z1, tw, th,  5.0, mi, tex,

        /*x1, y1, z1,    3.0,  tw, th,
        x1, y0, z0,    3.0,  t0, t0,
        x1, y1, z0,    3.0,  tw, t0,
        x1, y0, z0,    3.0,  t0, t0,
        x1, y1, z1,    3.0,  tw, th,
        x1, y0, z1,    3.0,  t0, th,*/

        // Base
        x0, y0, z0, tw, th,  5.0,  mi, tex,
        x1, y0, z0, tw, t0,  5.0,  mi, tex,
        x1, y0, z1, t0, t0,  5.0,  mi, tex,
        x1, y0, z1, t0, t0,  5.0,  mi, tex,
        x0, y0, z1, t0, th,  5.0,  mi, tex,
        x0, y0, z0, tw, th,  5.0,  mi, tex,

        // Top - slope me
        x1, y0, z1, tw, th,  8.0,  mi, tex,
        x1, y0, z0, tw, t0,  8.0,  mi, tex,
        x0, y1, z0, t0, t0,  8.0,  mi, tex,
        x0, y1, z0, t0, t0,  8.0,  mi, tex,
        x0, y1, z1, t0, th,  8.0,  mi, tex,
        x1, y0, z1, tw, th,  8.0,  mi, tex,
    ];
    vb.extend_from_slice(&cube_geometry);
    *element_count += 8;
}

fn west_east(
    vb: &mut Vec<f32>,
    element_count: &mut u32,
    x: f32,
    y: f32,
    z: f32,
    mi: f32,
    tex: f32,
) {
    let w = 1.0;
    let h = 1.0;
    let d = 1.0;

    let x0 = x;
    let x1 = x0 + w;
    let y0 = z;
    let y1 = y0 + d;
    let z0 = y;
    let z1 = z0 + h;

    let t0 = 0.0f32;
    let tw = w;
    let th = h;

    #[rustfmt::skip]
    let cube_geometry = [
        /*
        x0, y0, z0,    1.0,  t0, t0,
        x1, y1, z0,    1.0,  tw, th,
        x1, y0, z0,    1.0,  tw, t0,
        x1, y1, z0,    1.0,  tw, th,
        x0, y0, z0,    1.0,  t0, t0,
        x0, y1, z0,    1.0,  t0, th,
        */
        x0, y0, z0, t0, t0,   1.0,  mi, tex,
        x1, y1, z0, tw, th,   1.0,  mi, tex,
        x1, y0, z0, tw, t0,   1.0,  mi, tex,

        /*
        x0, y0, z1,    2.0,   t0, t0,
        x1, y0, z1,    2.0,   tw, t0,
        x1, y1, z1,    2.0,   tw, th,
        x1, y1, z1,    2.0,   tw, th,
        x0, y1, z1,    2.0,   t0, th,
        x0, y0, z1,    2.0,   t0, t0,
        */
        x0, y0, z1,  t0, t0,  2.0,  mi, tex,
        x1, y0, z1,  tw, t0,  2.0,  mi, tex,
        x1, y1, z1,  tw, th,  2.0,  mi, tex,

        x1, y1, z1,  tw, th,  3.0, mi, tex,
        x1, y0, z0,  t0, t0,  3.0, mi, tex,
        x1, y1, z0,  tw, t0,  3.0, mi, tex,
        x1, y0, z0,  t0, t0,  3.0, mi, tex,
        x1, y1, z1,  tw, th,  3.0, mi, tex,
        x1, y0, z1,  t0, th,  3.0, mi, tex,

        // Base - unchanged
        x0, y0, z0, tw, th,  5.0, mi, tex,
        x1, y0, z0, tw, t0,  5.0, mi, tex,
        x1, y0, z1, t0, t0,  5.0, mi, tex,
        x1, y0, z1, t0, t0,  5.0, mi, tex,
        x0, y0, z1, t0, th,  5.0, mi, tex,
        x0, y0, z0, tw, th,  5.0, mi, tex,

        // Top - needs to slope
        x1, y1, z1, tw, th,  9.0, mi, tex,
        x1, y1, z0, tw, t0,  9.0, mi, tex,
        x0, y0, z0, t0, t0,  9.0, mi, tex,
        x0, y0, z0, t0, t0,  9.0, mi, tex,
        x0, y0, z1, t0, th,  9.0, mi, tex,
        x1, y1, z1, tw, th,  9.0, mi, tex,
    ];
    vb.extend_from_slice(&cube_geometry);
    *element_count += 8;
}
