use rapier_testbed3d::TestbedViewer;
use rapier3d::prelude::*;

fn create_tower_circle(
    bodies: &mut RigidBodySet,
    colliders: &mut ColliderSet,
    offset: Vec3,
    stack_height: usize,
    nsubdivs: usize,
    half_extents: Vec3,
) {
    let ang_step = std::f32::consts::PI * 2.0 / nsubdivs as f32;
    let radius = 1.3 * nsubdivs as f32 * half_extents.x / std::f32::consts::PI;

    let shift = half_extents * 2.0;
    for i in 0usize..stack_height {
        for j in 0..nsubdivs {
            let fj = j as f32;
            let fi = i as f32;
            let y = fi * shift.y;
            let pos = Pose3::new(offset, Vec3::Y * (fi / 2.0 + fj) * ang_step)
                .prepend_translation(Vec3::new(0.0, y, radius));

            // Build the rigid body.
            let rigid_body = RigidBodyBuilder::dynamic().pose(pos);
            let handle = bodies.insert(rigid_body);
            let collider = ColliderBuilder::cuboid(half_extents.x, half_extents.y, half_extents.z);
            colliders.insert_with_parent(collider, handle, bodies);
        }
    }
}

fn create_wall(
    bodies: &mut RigidBodySet,
    colliders: &mut ColliderSet,
    offset: Vec3,
    stack_height: usize,
    half_extents: Vec3,
) {
    let shift = half_extents * 2.0;
    for i in 0usize..stack_height {
        for j in i..stack_height {
            let fj = j as f32;
            let fi = i as f32;
            let x = offset.x;
            let y = fi * shift.y + offset.y;
            let z = (fi * shift.z / 2.0) + (fj - fi) * shift.z + offset.z
                - stack_height as f32 * half_extents.z;

            // Build the rigid body.
            let rigid_body = RigidBodyBuilder::dynamic().translation(Vec3::new(x, y, z));
            let handle = bodies.insert(rigid_body);
            let collider = ColliderBuilder::cuboid(half_extents.x, half_extents.y, half_extents.z);
            colliders.insert_with_parent(collider, handle, bodies);
        }
    }
}

fn create_pyramid(
    bodies: &mut RigidBodySet,
    colliders: &mut ColliderSet,
    offset: Vec3,
    stack_height: usize,
    half_extents: Vec3,
) {
    let shift = half_extents * 2.0;

    for i in 0usize..stack_height {
        for j in i..stack_height {
            for k in i..stack_height {
                let fi = i as f32;
                let fj = j as f32;
                let fk = k as f32;
                let x = (fi * shift.x / 2.0) + (fk - fi) * shift.x + offset.x
                    - stack_height as f32 * half_extents.x;
                let y = fi * shift.y + offset.y;
                let z = (fi * shift.z / 2.0) + (fj - fi) * shift.z + offset.z
                    - stack_height as f32 * half_extents.z;

                // Build the rigid body.
                let rigid_body = RigidBodyBuilder::dynamic().translation(Vec3::new(x, y, z));
                let handle = bodies.insert(rigid_body);
                let collider =
                    ColliderBuilder::cuboid(half_extents.x, half_extents.y, half_extents.z);
                colliders.insert_with_parent(collider, handle, bodies);
            }
        }
    }
}

pub async fn run(viewer: &mut TestbedViewer) -> anyhow::Result<()> {
    /*
     * World
     */
    let mut world = PhysicsWorld::new();

    /*
     * Ground
     */
    let ground_size = 200.0;
    let ground_height = 0.1;

    let rigid_body = RigidBodyBuilder::fixed().translation(Vec3::new(0.0, -ground_height, 0.0));
    let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size);
    let _ = world.insert(rigid_body, collider);

    /*
     * Create the cubes
     */
    let cube_size = 1.0;
    let hext = Vec3::splat(cube_size);
    let bottomy = cube_size * 50.0;
    create_pyramid(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(-110.0, bottomy, 0.0),
        12,
        hext,
    );
    create_pyramid(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(-80.0, bottomy, 0.0),
        12,
        hext,
    );
    create_pyramid(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(-50.0, bottomy, 0.0),
        12,
        hext,
    );
    create_pyramid(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(-20.0, bottomy, 0.0),
        12,
        hext,
    );
    create_wall(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(-2.0, bottomy, 0.0),
        12,
        hext,
    );
    create_wall(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(4.0, bottomy, 0.0),
        12,
        hext,
    );
    create_wall(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(10.0, bottomy, 0.0),
        12,
        hext,
    );
    create_tower_circle(
        &mut world.bodies,
        &mut world.colliders,
        Vec3::new(25.0, bottomy, 0.0),
        8,
        24,
        hext,
    );

    /*
     * Set up the viewer.
     */
    viewer.set_world(&mut world);
    viewer.look_at(Vec3::new(100.0, 100.0, 100.0), Vec3::ZERO);

    while viewer.render_frame(&mut world).await {
        if viewer.simulating() {
            world.step();
        }
    }
    Ok(())
}
