use avian3d::prelude::*;
use bevy::ecs::{component::ComponentId, world::DeferredWorld};
use bevy::math::*;
use bevy::prelude::*;
use bevy_trenchbroom::prelude::*;

use crate::cash::Cash;

#[derive(PointClass, Component, Reflect, Default)]
#[reflect(Component)]
#[require(Transform)]
#[component(on_add = Self::on_add)]
#[model({ "path": "models/spawn.glb" })]
pub struct SpawnPoint {}

impl SpawnPoint {
    fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) {
        let Some(asset_server) = world.get_resource::<AssetServer>() else {
            return;
        };

        let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/spawn.glb"));

        world.commands().entity(entity).insert((
            Name::new("spawn"),
            SceneRoot(mesh),
            RigidBody::Static,
            ColliderConstructorHierarchy::new(ColliderConstructor::TrimeshFromMesh),
        ));
    }
}

#[derive(SolidClass, Component, Reflect)]
#[reflect(Component)]
#[geometry(GeometryProvider::new().trimesh_collider().smooth_by_default_angle().render())]
pub struct Worldspawn;

#[derive(SolidClass, Component, Reflect, Default)]
#[reflect(Component)]
#[require(Transform)]
#[geometry(GeometryProvider::new().trimesh_collider().smooth_by_default_angle().render())]
pub struct NamedEntity {
    pub name: String,
}

#[derive(PointClass, Component, Reflect, Default)]
#[reflect(Component)]
#[require(Transform)]
#[component(on_add = Self::on_add)]
#[model({ "path": "models/alien_naked.glb" })]
pub struct EnemySpawn {}

impl EnemySpawn {
    fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) {
        let Some(asset_server) = world.get_resource::<AssetServer>() else {
            return;
        };

        let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/alien_naked.glb"));

        world
            .commands()
            .entity(entity)
            .insert((Name::from("Enemy"), Visibility::default()))
            .with_children(|parent| {
                parent
                    .spawn((
                        Visibility::default(),
                        RigidBody::Dynamic,
                        Collider::capsule(0.4, 2.),
                        LockedAxes::new().lock_rotation_z().lock_rotation_x(),
                        Transform::from_translation(Vec3::new(0., 1.0, 0.)),
                    ))
                    .with_child((
                        Visibility::default(),
                        Transform::from_translation(Vec3::new(0., -1.4, 0.))
                            .with_scale(Vec3::splat(1.5)),
                        SceneRoot(mesh),
                    ));
            });
    }
}

#[derive(PointClass, Component, Reflect, Default)]
#[reflect(Component)]
#[require(Transform)]
#[component(on_add = Self::on_add)]
#[model({ "path": "models/cash.glb" })]
pub struct CashSpawn {}

impl CashSpawn {
    fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) {
        let Some(asset_server) = world.get_resource::<AssetServer>() else {
            return;
        };

        let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/cash.glb"));

        world.commands().entity(entity).insert((
            Name::new("cash"),
            SceneRoot(mesh),
            Cash,
            Collider::cuboid(2., 3.0, 2.),
            Sensor,
        ));
    }
}
