use downcast_rs::Downcast;

use std::any::Any;
use crate::prelude::App;
/// A collection of Bevy app logic and configuration.
///
/// Plugins configure an [`App`]. When an [`App`] registers a plugin,
/// the plugin's [`Plugin::build`] function is run. By default, a plugin
/// can only be added once to an [`App`].
///
/// If the plugin may need to be added twice or more, the function [`is_unique()`](Self::is_unique)
/// should be overridden to return `false`. Plugins are considered duplicate if they have the same
/// [`name()`](Self::name). The default `name()` implementation returns the type name, which means
/// generic plugins with different type parameters will not be considered duplicates.
///
/// ## Lifecycle of a plugin
///
/// When adding a plugin to an [`App`]:
/// * the app calls [`Plugin::build`] immediately, and register the plugin
/// * once the app started, it will wait for all registered [`Plugin::ready`] to return `true`
/// * it will then call all registered [`Plugin::finish`]
/// * and call all registered [`Plugin::cleanup`]
pub trait Plugin: Downcast + Any + Send + Sync {
    
    /// Configures the [`App`] to which this plugin is added.
    fn build(&self, app: &mut App);

    /// Has the plugin finished it's setup? This can be useful for plugins that needs something
    /// asynchronous to happen before they can finish their setup, like renderer initialization.
    /// Once the plugin is ready, [`finish`](Plugin::finish) should be called.
    fn ready(&self, _app: &App) -> bool {
        true
    }

    /// Finish adding this plugin to the [`App`], once all plugins registered are ready. This can
    /// be useful for plugins that depends on another plugin asynchronous setup, like the renderer.
    fn finish(&self, _app: &mut App) {
        // do nothing
    }

    /// Runs after all plugins are built and finished, but before the app schedule is executed.
    /// This can be useful if you have some resource that other plugins need during their build step,
    /// but after build you want to remove it and send it to another thread.
    fn cleanup(&self, _app: &mut App) {
        // do nothing
    }

    /// Configures a name for the [`Plugin`] which is primarily used for checking plugin
    /// uniqueness and debugging.
    fn name(&self) -> &str {
        std::any::type_name::<Self>()
    }

    /// If the plugin can be meaningfully instantiated several times in an [`App`](crate::App),
    /// override this method to return `false`.
    fn is_unique(&self) -> bool {
        true
    }
}

// impl_downcast!(Plugin);

/// A type representing an unsafe function that returns a mutable pointer to a [`Plugin`].
/// It is used for dynamically loading plugins.
///
/// See `bevy_dynamic_plugin/src/loader.rs#dynamically_load_plugin`.
// pub type CreatePlugin = unsafe fn() -> *mut dyn Plugin;

/// Types that represent a set of [`Plugin`]s.
///
/// This is implemented for all types which implement [`Plugin`],
/// [`PluginGroup`](super::PluginGroup), and tuples over [`Plugins`].
pub trait Plugins<Marker>: sealed::Plugins<Marker> {}

impl<Marker, T> Plugins<Marker> for T where T: sealed::Plugins<Marker> {}

mod sealed {

    // use bevy_ecs::all_tuples;

    // use crate::{App, AppError, Plugin, PluginGroup};
    // use pi_world_extend_macro::all_tuples;
    use crate::plugin_group::WorldPluginExtent;
    use crate::prelude::App;
    use crate::plugin::Plugin;
    pub trait Plugins<Marker> {

        fn add_to_app(self, app: &mut App);
    }

    pub struct PluginMarker;

    impl<P: Plugin> Plugins<PluginMarker> for P {
        fn add_to_app(self, app: &mut App) {
            app.add_boxed_plugin(Box::new(self));

        }
    }

    // impl<P: PluginGroup> Plugins<PluginGroupMarker> for P {
    //     fn add_to_app(self, app: &mut MultiThreadApp) {
    //         self.build().finish(app);
    //     }
    // }

    // macro_rules! impl_plugins_tuples {
    //     ($(($param: ident, $plugins: ident)),*) => {
    //         impl<$($param, $plugins),*> Plugins<(PluginsTupleMarker, $($param,)*)> for ($($plugins,)*)
    //         where
    //             $($plugins: Plugins<$param>),*
    //         {
    //             #[allow(non_snake_case, unused_variables)]
    //             fn add_to_app(self, app: &mut MultiThreadApp) {
    //                 let ($($plugins,)*) = self;
    //                 $($plugins.add_to_app(app);)*
    //             }
    //         }
    //     }
    // }

    // all_tuples!(impl_plugins_tuples, 0, 15, P, S);
}

// Dummy plugin used to temporary hold the place in the plugin registry
// struct PlaceholderPlugin;
// impl Plugin for PlaceholderPlugin {
//     fn build(&self, _app: &mut App) {}
// }