use crate::ecs::resources::{AnimationClipAssets, SkeletonAssets};
use crate::ecs::systems::{BlendingSystem, JointUploadSystem, SamplingSystem, SkinningSystem};
use archetype_ecs::prelude::{App, Plugin};

/// Plugin for registering AnyMotion animation systems and resources
pub struct AnimationPlugin;

impl AnimationPlugin {
    pub fn new() -> Self {
        Self
    }
}

impl Plugin for AnimationPlugin {
    fn build(&self, app: &mut App) {
        // Register resources if they don't exist
        if !app.world.has_resource::<SkeletonAssets>() {
            app.world.insert_resource(SkeletonAssets::new());
        }
        if !app.world.has_resource::<AnimationClipAssets>() {
            app.world.insert_resource(AnimationClipAssets::new());
        }

        // Register systems in order
        // Note: Blending should run before sampling if we want cross-fading to override base animation
        app.add_system(Box::new(BlendingSystem));
        app.add_system(Box::new(SamplingSystem));
        app.add_system(Box::new(SkinningSystem));
        app.add_system(Box::new(JointUploadSystem));
    }
}

impl Default for AnimationPlugin {
    fn default() -> Self {
        Self::new()
    }
}
