#![feature(iter_array_chunks)]

/// Example to illustrate selecting objects in 3d space
///
use bevy::{
    prelude::*,
    render::mesh::MeshVertexAttribute,
    render::render_resource::{Extent3d, TextureDimension, TextureFormat},
    render::{mesh::VertexAttributeValues, render_resource::VertexFormat},
    window::PrimaryWindow,
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, startup)
        .add_systems(Update, select)
        .run();
}

fn startup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut images: ResMut<Assets<Image>>,
) {
    let debug_material = materials.add(StandardMaterial {
        base_color_texture: Some(images.add(uv_debug_texture())),
        ..default()
    });

    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
        ..default()
    });

    commands.spawn(PointLightBundle {
        transform: Transform::from_xyz(0.0, 0.0, 5.0),
        ..default()
    });

    commands.spawn(PbrBundle {
        mesh: meshes.add(shape::Cube::default().into()),
        material: debug_material.clone(),
        transform: Transform::from_xyz(0.0, 0.0, 0.0),
        ..default()
    });

    commands.spawn(PbrBundle {
        mesh: meshes.add(shape::Torus::default().into()),
        material: debug_material.clone(),
        transform: Transform::from_xyz(3.0, 0.0, 0.0),
        ..default()
    });

    commands.spawn(PbrBundle {
        mesh: meshes.add(shape::Icosphere::default().try_into().unwrap()),
        material: debug_material.clone(),
        transform: Transform::from_xyz(0.0, 3.0, 0.0),
        ..default()
    });
}

/// Creates a colorful test pattern
fn uv_debug_texture() -> Image {
    const TEXTURE_SIZE: usize = 8;

    let mut palette: [u8; 32] = [
        255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255,
        198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255,
    ];

    let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
    for y in 0..TEXTURE_SIZE {
        let offset = TEXTURE_SIZE * y * 4;
        texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
        palette.rotate_right(4);
    }

    Image::new_fill(
        Extent3d {
            width: TEXTURE_SIZE as u32,
            height: TEXTURE_SIZE as u32,
            depth_or_array_layers: 1,
        },
        TextureDimension::D2,
        &texture_data,
        TextureFormat::Rgba8UnormSrgb,
    )
}

fn select(
    query: Query<(Entity, &Handle<Mesh>, &GlobalTransform)>,
    meshes: Res<Assets<Mesh>>,
    cameras: Query<(&Camera, &GlobalTransform)>,
    windows: Query<&Window, With<PrimaryWindow>>,
) {
    // TODO:
    // When mouse moves
    // Ray trace to find object being selected
    windows.iter().for_each(|window| {
        if let Some(pos) = window.cursor_position() {
            cameras.iter().for_each(|(camera, gt)| {
                if let Some(ray) = camera.viewport_to_world(gt, pos) {
                    query
                        .iter()
                        .filter_map(|(entity, handle, gt)| {
                            meshes.get(handle).map(|mesh| (entity, mesh, gt))
                        })
                        .for_each(|(entity, mesh, gt)| {
                            let hit = intersects(&ray, &gt, mesh);
                            if hit.is_some() {
                                info!("We got a hit on {:?} at {:?}", entity, hit);
                            }
                        });
                }
            });
        }
    });
}

#[derive(Debug)]
struct Triangle {
    v0: Vec3,
    v1: Vec3,
    v2: Vec3,
}

impl Triangle {
    fn normal(&self) -> Vec3 {
        (self.edge_a()).cross(self.edge_b())
    }

    fn edge_a(&self) -> Vec3 {
        self.v1 - self.v0
    }

    fn edge_b(&self) -> Vec3 {
        self.v2 - self.v0
    }

    fn edge0(&self) -> Vec3 {
        self.v1 - self.v0
    }

    fn edge1(&self) -> Vec3 {
        self.v2 - self.v1
    }

    fn edge2(&self) -> Vec3 {
        self.v0 - self.v2
    }
}

#[derive(Debug)]
struct Hit {
    distance: f32,
    point: Vec3,
}

/// Heavily synthesized by these two things:
/// * Textbook: https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/ray-triangle-intersection-geometric-solution.html
/// * Example: https://github.com/aevyrie/bevy_mod_raycast/blob/435d8ef100738797161ac3a9b910ea346a4ed6e6/src/raycast.rs#L43
fn intersects(ray: &Ray, gt: &GlobalTransform, mesh: &Mesh) -> Option<Hit> {
    let attr = MeshVertexAttribute::new("Vertex_Position", 0, VertexFormat::Float32x3);
    if let Some(verts) = mesh.attribute(attr) {
        if let Some(idxs) = mesh.indices() {
            match verts {
                VertexAttributeValues::Float32x3(vals) => {
                    idxs.iter()
                        .array_chunks::<3>()
                        // Convert arrays to vec3
                        .map(|[x, y, z]| {
                            [
                                Vec3::from_array(vals[x]),
                                Vec3::from_array(vals[y]),
                                Vec3::from_array(vals[z]),
                            ]
                        })
                        // Transform each point by the global transform
                        .map(|[a, b, c]| {
                            [
                                gt.transform_point(a),
                                gt.transform_point(b),
                                gt.transform_point(c),
                            ]
                        })
                        // Collect everything into a triangle for easy operations
                        .map(|[v0, v1, v2]| Triangle { v0, v1, v2 })
                        .filter_map(|triangle| {
                            // Calculate the distance this ray hits the plane normal to the tri
                            if let Some(d) = ray.intersect_plane(triangle.v0, triangle.normal()) {
                                // Calculate the point on that plane which intersects
                                let p = ray.get_point(d);
                                // Inside out test
                                let hit = {
                                    // Determine if p is w/in edge0
                                    let c0 = triangle.edge0().cross(p - triangle.v0);
                                    // Determine if p is w/in edge1
                                    let c1 = triangle.edge1().cross(p - triangle.v1);
                                    // Determine if p is w/in edge2
                                    let c2 = triangle.edge2().cross(p - triangle.v2);

                                    // Check all three at once
                                    triangle.normal().dot(c0) > 0.0
                                        && triangle.normal().dot(c1) > 0.0
                                        && triangle.normal().dot(c2) > 0.0
                                };
                                hit.then_some(Hit {
                                    distance: d,
                                    point: p,
                                })
                            } else {
                                None
                            }
                        })
                        .min_by(|a, b| a.distance.total_cmp(&b.distance))
                }
                _ => None,
            }
        } else {
            None
        }
    } else {
        None
    }
}
