use crate::stdlib::{boxed::Box, ops::AddAssign, vec::Vec};
use crate::{Engine, EvalAltResult, Module, ModuleResolver, Position, Shared};

/// Module resolution service that holds a collection of module resolves,
/// to be searched in sequential order.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Module};
/// use rhai::module_resolvers::{StaticModuleResolver, ModuleResolversCollection};
///
/// let mut collection = ModuleResolversCollection::new();
///
/// let resolver = StaticModuleResolver::new();
/// collection.push(resolver);
///
/// let mut engine = Engine::new();
/// engine.set_module_resolver(collection);
/// ```
#[derive(Default)]
pub struct ModuleResolversCollection(Vec<Box<dyn ModuleResolver>>);

impl ModuleResolversCollection {
    /// Create a new [`ModuleResolversCollection`].
    ///
    /// # Example
    ///
    /// ```
    /// use rhai::{Engine, Module};
    /// use rhai::module_resolvers::{StaticModuleResolver, ModuleResolversCollection};
    ///
    /// let mut collection = ModuleResolversCollection::new();
    ///
    /// let resolver = StaticModuleResolver::new();
    /// collection.push(resolver);
    ///
    /// let mut engine = Engine::new();
    /// engine.set_module_resolver(collection);
    /// ```
    #[inline(always)]
    pub fn new() -> Self {
        Default::default()
    }
    /// Append a module resolver to the end.
    #[inline(always)]
    pub fn push(&mut self, resolver: impl ModuleResolver + 'static) -> &mut Self {
        self.0.push(Box::new(resolver));
        self
    }
    /// Insert a module resolver to an offset index.
    ///
    /// # Panics
    ///
    /// Panics if the index is out of bounds.
    #[inline(always)]
    pub fn insert(&mut self, index: usize, resolver: impl ModuleResolver + 'static) -> &mut Self {
        self.0.insert(index, Box::new(resolver));
        self
    }
    /// Remove the last module resolver from the end, if any.
    #[inline(always)]
    pub fn pop(&mut self) -> Option<Box<dyn ModuleResolver>> {
        self.0.pop()
    }
    /// Remove a module resolver at an offset index.
    ///
    /// # Panics
    ///
    /// Panics if the index is out of bounds.
    #[inline(always)]
    pub fn remove(&mut self, index: usize) -> Box<dyn ModuleResolver> {
        self.0.remove(index)
    }
    /// Get an iterator of all the module resolvers.
    #[inline(always)]
    pub fn iter(&self) -> impl Iterator<Item = &dyn ModuleResolver> {
        self.0.iter().map(|v| v.as_ref())
    }
    /// Get a mutable iterator of all the modules.
    #[inline(always)]
    pub fn into_iter(self) -> impl Iterator<Item = Box<dyn ModuleResolver>> {
        self.0.into_iter()
    }
    /// Remove all module resolvers.
    #[inline(always)]
    pub fn clear(&mut self) -> &mut Self {
        self.0.clear();
        self
    }
    /// Is this [`ModuleResolversCollection`] empty?
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    /// Get the number of module resolvers in this [`ModuleResolversCollection`].
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.0.len()
    }
    /// Add another [`ModuleResolversCollection`] to the end of this collection.
    /// The other [`ModuleResolversCollection`] is consumed.
    #[inline(always)]
    pub fn append(&mut self, other: Self) -> &mut Self {
        self.0.extend(other.0.into_iter());
        self
    }
}

impl ModuleResolver for ModuleResolversCollection {
    fn resolve(
        &self,
        engine: &Engine,
        path: &str,
        pos: Position,
    ) -> Result<Shared<Module>, Box<EvalAltResult>> {
        for resolver in self.0.iter() {
            match resolver.resolve(engine, path, pos) {
                Ok(module) => return Ok(module),
                Err(err) => match *err {
                    EvalAltResult::ErrorModuleNotFound(_, _) => continue,
                    EvalAltResult::ErrorInModule(_, err, _) => return Err(err),
                    _ => panic!("ModuleResolver::resolve returns error that is not ErrorModuleNotFound or ErrorInModule"),
                },
            }
        }

        EvalAltResult::ErrorModuleNotFound(path.into(), pos).into()
    }
}

impl<M: ModuleResolver + 'static> AddAssign<M> for ModuleResolversCollection {
    #[inline(always)]
    fn add_assign(&mut self, rhs: M) {
        self.push(rhs);
    }
}
