use super::Entity;

#[derive(Debug, Clone)]
pub enum SubentityList<E: Entity> {
    NotLoaded,
    Loaded(Vec<E>),
}

impl<E: Entity> SubentityList<E> {
    pub fn iter(&self) -> std::slice::Iter<'_, E> {
        return match self {
            SubentityList::NotLoaded => panic!("Entity list has not been loaded"),
            SubentityList::Loaded(entity_vec) => entity_vec.iter(),
        };
    }

    pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, E> {
        return match self {
            SubentityList::NotLoaded => panic!("Entity list has not been loaded"),
            SubentityList::Loaded(entity_vec) => entity_vec.iter_mut(),
        };
    }

    pub fn to_vec(&self) -> Vec<&E> {
        return self.iter().collect();
    }

    pub fn into_vec(self) -> Vec<E> {
        return match self {
            SubentityList::NotLoaded => panic!("Entity list has not been loaded"),
            SubentityList::Loaded(entity_vec) => entity_vec,
        };
    }
}

impl<'a, E: Entity> IntoIterator for &'a SubentityList<E> {
    type Item = &'a E;
    type IntoIter = std::slice::Iter<'a, E>;

    fn into_iter(self) -> Self::IntoIter {
        return self.iter();
    }
}

impl<'a, E: Entity> IntoIterator for &'a mut SubentityList<E> {
    type Item = &'a mut E;
    type IntoIter = std::slice::IterMut<'a, E>;

    fn into_iter(self) -> Self::IntoIter {
        return self.iter_mut();
    }
}

impl<E: Entity> IntoIterator for SubentityList<E> {
    type Item = E;
    type IntoIter = std::vec::IntoIter<E>;

    fn into_iter(self) -> Self::IntoIter {
        return match self {
            SubentityList::NotLoaded => panic!("Entity list has not been loaded"),
            SubentityList::Loaded(entity_vec) => entity_vec.into_iter(),
        };
    }
}
