use std::f32::consts::PI;

use bevy::prelude::*;

use crate::{
	common::{animation::SpriteAnimation, path_follow::PathFollow2D, velocity::Velocity},
	level::Unit,
};

pub struct EnemyPlugin;

impl Plugin for EnemyPlugin {
	fn build(&self, app: &mut App) {
		// app.add_systems(Startup, setup);
		app.add_systems(Update, debug_direction);
		app.add_systems(FixedUpdate, outside_bounds);
	}
}

#[derive(Component)]
#[require(Unit, SpriteAnimation)]
pub struct Enemy;

fn debug_direction(query: Query<&Transform, With<Enemy>>, gizmos: Gizmos) {
	// for enemy in &query {
	// 	let start = enemy.translation.truncate();
	// 	let end = start + Vec2::new(10.0, 0.0);
	// 	gizmos.arrow_2d(start, end, RED);
	// }
}

fn outside_bounds(
	enemies: Query<(Entity, &Transform), With<Enemy>>,
	window: Query<&Window>,
	mut commands: Commands,
) {
	let window = window.single();
	let screen_size: Vec3 = window.physical_size().as_vec2().extend(1.0);

	for (entity, transform) in &enemies {
		let pos = transform.translation.truncate();
		if pos.x < -60.0
			|| pos.y < -60.0
			|| pos.x > screen_size.x + 60.0
			|| pos.y > screen_size.y + 60.0
		{
			commands.entity(entity).despawn();
		}
	}
}
