use bevy::{gltf::GltfMesh, math::Vec3A, prelude::*, render::mesh::MeshAabb};
use bevy_rapier3d::prelude::{Collider, RigidBody};

use crate::{GameState, asset_loading::GltfAssets};

pub fn plugin(app: &mut App) {
    app.add_systems(OnEnter(GameState::Playing), spawn);
}

#[derive(Component)]
pub struct Interact;

fn spawn(mut commands: Commands, models: Res<Assets<Gltf>>, gltf_assets: Res<GltfAssets>) {
    let hammer = gltf_assets
        .library
        .get("meshes/library/hammer.glb")
        .unwrap();

    let hammer = models.get(hammer).unwrap();
    let asset = hammer.default_scene.as_ref().unwrap();
    // hammer
    commands
        .spawn((
            Transform::from_xyz(0.0, 100.0, 0.0).with_scale(Vec3::splat(0.1)),
            Interact,
            RigidBody::Dynamic,
            SceneRoot(asset.clone()),
        ))
        .with_children(|parent| {
            parent
                .spawn(Collider::cuboid(0.8, 10f32, 0.8))
                .insert(Transform::from_xyz(0.0, -5.0, 0.0));
            parent
                .spawn(Collider::cuboid(1.0, 1.0, 4.5))
                // Position the collider relative to the rigid-body.
                .insert(Transform::from_xyz(0.0, 4.2, 1.0));
        });

    //tools
}
