use crate::ecs::world::{EcsCommand, World};
use freecs::Entity;
use nalgebra_glm::Vec3;

use crate::prelude::*;
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EngineEntity(pub Entity);

pub fn sync_engine_translation<I>(pairs: I, world: &mut World)
where
    I: IntoIterator<Item = (Entity, Vec3)>,
{
    for (engine_entity, translation) in pairs {
        if let Some(transform) = world.core.get_local_transform_mut(engine_entity) {
            transform.translation = translation;
        }
        crate::ecs::transform::commands::mark_local_transform_dirty(world, engine_entity);
    }
}

pub fn sync_engine_transform<I>(pairs: I, world: &mut World)
where
    I: IntoIterator<Item = (Entity, crate::ecs::transform::components::LocalTransform)>,
{
    for (engine_entity, local_transform) in pairs {
        world
            .core
            .set_local_transform(engine_entity, local_transform);
        crate::ecs::transform::commands::mark_local_transform_dirty(world, engine_entity);
    }
}

pub fn despawn_linked(engine_entity: Entity, world: &mut World) {
    queue_ecs_command(
        world,
        EcsCommand::DespawnRecursive {
            entity: engine_entity,
        },
    );
}
