use std::{cell::RefCell, collections::BTreeMap, rc::Rc};

use crate::entity::Entity;

use super::region::Region;

#[derive(Default)]
pub struct Grid {
    entity_map: BTreeMap<u32, Rc<RefCell<dyn Entity>>>,
    region_vec: Vec<Region>,
}

impl Grid {
    #[allow(dead_code)]
    pub fn has_entity(&self) -> bool {
        self.entity_map.len() != 0
    }

    pub fn add_entity(&mut self, entity: Rc<RefCell<dyn Entity>>) {
        let entity_id = entity.borrow().entity_id();
        self.entity_map.insert(entity_id, entity);
    }

    pub fn del_entity(&mut self, entity: &Rc<RefCell<dyn Entity>>) {
        let entity_id = entity.borrow().entity_id();
        self.entity_map.remove(&entity_id);
    }

    pub fn add_region(&mut self, region: Region) {
        self.region_vec.push(region);
    }

    pub fn get_all_entity(&self) -> Vec<Rc<RefCell<dyn Entity>>> {
        self.entity_map.values().map(|a| a.clone()).collect()
    }
}
