use super::TriggerArrow;
use crate::{
    GameState, billboards::Billboard, global_observer, heads_database::HeadsDatabase,
    hitpoints::Hit, loading_assets::GameAssets, physics_layers::GameLayer, sounds::PlaySound,
    utils::sprite_3d_animation::AnimationTimer,
};
use avian3d::prelude::*;
use bevy::{pbr::NotShadowCaster, prelude::*};
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
use std::f32::consts::PI;

#[derive(Component)]
struct ArrowProjectile {
    damage: u32,
}

#[derive(Resource)]
struct ShotAssets {
    image: Handle<Image>,
    layout: Handle<TextureAtlasLayout>,
}

pub fn plugin(app: &mut App) {
    app.add_systems(OnEnter(GameState::Playing), setup);
    app.add_systems(Update, update.run_if(in_state(GameState::Playing)));

    global_observer!(app, on_trigger_arrow);
}

fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Sprite3dParams) {
    let layout = TextureAtlasLayout::from_grid(UVec2::splat(256), 7, 6, None, None);
    let texture_atlas_layout = sprite_params.atlas_layouts.add(layout);

    commands.insert_resource(ShotAssets {
        image: assets.impact_atlas.clone(),
        layout: texture_atlas_layout,
    });
}

fn on_trigger_arrow(
    trigger: Trigger<TriggerArrow>,
    mut commands: Commands,
    query_transform: Query<&Transform>,
    heads_db: Res<HeadsDatabase>,
) {
    let state = trigger.0;

    commands.trigger(PlaySound::Crossbow);

    let rotation = if let Some(target) = state.target {
        let t = query_transform
            .get(target)
            .expect("target must have transform");
        Transform::from_translation(state.pos)
            .looking_at(t.translation, Vec3::Y)
            .rotation
    } else {
        state.rot.mul_quat(Quat::from_rotation_y(PI))
    };

    let mut t = Transform::from_translation(state.pos).with_rotation(rotation);
    t.translation += t.forward().as_vec3() * 2.;

    let damage = heads_db.head_stats(state.head).damage;
    commands.spawn((Name::new("projectile-arrow"), ArrowProjectile { damage }, t));
}

fn update(
    mut cmds: Commands,
    query: Query<(Entity, &Transform, &ArrowProjectile)>,
    spatial_query: SpatialQuery,
    assets: Res<ShotAssets>,
    mut sprite_params: Sprite3dParams,
) {
    for (e, t, arrow) in query.iter() {
        let filter = SpatialQueryFilter::from_mask(LayerMask(
            GameLayer::Level.to_bits() | GameLayer::Npc.to_bits(),
        ));

        if let Some(first_hit) = spatial_query.cast_shape(
            &Collider::sphere(0.5),
            t.translation,
            t.rotation,
            t.forward(),
            &ShapeCastConfig::from_max_distance(150.),
            &filter,
        ) {
            cmds.entity(first_hit.entity).trigger(Hit {
                damage: arrow.damage,
            });

            cmds.spawn(
                Sprite3dBuilder {
                    image: assets.image.clone(),
                    pixels_per_metre: 128.,
                    alpha_mode: AlphaMode::Blend,
                    unlit: true,
                    ..default()
                }
                .bundle_with_atlas(
                    &mut sprite_params,
                    TextureAtlas {
                        layout: assets.layout.clone(),
                        index: 0,
                    },
                ),
            )
            .insert((
                Billboard,
                Transform::from_translation(first_hit.point1),
                NotShadowCaster,
                AnimationTimer::new(Timer::from_seconds(0.005, TimerMode::Repeating)),
            ));
        }

        cmds.entity(e).despawn();
    }
}
