use crate::query::{
	play_animations, play_directional_animations, play_directional_override_animation,
	play_override_animation, play_simple_animation, sync_child_animation,
};

use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::entity::Entity;
use bevy_ecs::event::EntityEvent;
use bevy_ecs::message::Message;
use bevy_ecs::schedule::{IntoScheduleConfigs, SystemSet};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
pub enum AnimationSystems {
	TickAnimations,
	SyncAnimations,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Message, EntityEvent)]
pub struct AnimationCompleted {
	pub entity: Entity,
	pub user_data: u128,
}

pub struct AnimationSystemsPlugin;

impl Plugin for AnimationSystemsPlugin {
	fn build(&self, app: &mut App) {
		app.add_message::<AnimationCompleted>();

		app.configure_sets(
			PostUpdate,
			(AnimationSystems::TickAnimations.before(AnimationSystems::SyncAnimations),),
		);

		app.add_systems(
			PostUpdate,
			(
				play_animations,
				play_override_animation,
				play_directional_animations,
				play_directional_override_animation,
				play_simple_animation,
			)
				.in_set(AnimationSystems::TickAnimations),
		)
		.add_systems(
			PostUpdate,
			sync_child_animation.in_set(AnimationSystems::SyncAnimations),
		);
	}
}
