use crate::tui::ecs::world::World;
use freecs::Entity;

pub struct EntityGroup {
    entities: Vec<Entity>,
}

impl EntityGroup {
    pub fn new() -> Self {
        Self {
            entities: Vec::new(),
        }
    }

    pub fn add(&mut self, entity: Entity) {
        self.entities.push(entity);
    }

    pub fn spawn_one(&mut self, world: &mut World, mask: u64) -> Entity {
        let entity = world.spawn_entities(mask, 1)[0];
        self.entities.push(entity);
        entity
    }

    pub fn despawn_all(&mut self, world: &mut World) {
        if !self.entities.is_empty() {
            world.despawn_entities(&self.entities);
            self.entities.clear();
        }
    }

    pub fn entities(&self) -> &[Entity] {
        &self.entities
    }

    pub fn len(&self) -> usize {
        self.entities.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entities.is_empty()
    }

    pub fn retain(&mut self, predicate: impl Fn(&Entity) -> bool) {
        self.entities.retain(predicate);
    }
}

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