#![allow(missing_docs)]
use dioxus_core::{ScopeState, TaskId};
use std::{any::Any, cell::Cell, future::Future, rc::Rc, sync::Arc};
use crate::{use_state, UseState};
pub fn use_future<T, F, D>(
    cx: &ScopeState,
    dependencies: D,
    future: impl FnOnce(D::Out) -> F,
) -> &UseFuture<T>
where
    T: 'static,
    F: Future<Output = T> + 'static,
    D: UseFutureDep,
{
    let val = use_state(cx, || None);
    let state = cx.use_hook(move || UseFuture {
        update: cx.schedule_update(),
        needs_regen: Cell::new(true),
        state: val.clone(),
        task: Default::default(),
        dependencies: Vec::new(),
    });
    if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() {
        if let Some(task) = state.task.take() {
            cx.remove_future(task);
        }
        let fut = future(dependencies.out());
        let val = val.clone();
        let task = state.task.clone();
        state.task.set(Some(cx.push_future(async move {
            val.set(Some(fut.await));
            task.take();
        })));
        state.needs_regen.set(false);
    }
    state.state.current_val = val.current_val.clone();
    state
}
pub enum FutureState<'a, T> {
    Pending,
    Complete(&'a T),
    Regenerating(&'a T), }
pub struct UseFuture<T: 'static> {
    update: Arc<dyn Fn()>,
    needs_regen: Cell<bool>,
    task: Rc<Cell<Option<TaskId>>>,
    dependencies: Vec<Box<dyn Any>>,
    state: UseState<Option<T>>,
}
pub enum UseFutureState<'a, T> {
    Pending,
    Complete(&'a T),
    Reloading(&'a T),
}
impl<T> UseFuture<T> {
    pub fn restart(&self) {
        self.needs_regen.set(true);
        (self.update)();
    }
    pub fn cancel(&self, cx: &ScopeState) {
        if let Some(task) = self.task.take() {
            cx.remove_future(task);
        }
    }
    pub fn set(&self, new_value: T) {
        self.state.set(Some(new_value));
    }
    pub fn value(&self) -> Option<&T> {
        self.state.current_val.as_ref().as_ref()
    }
    pub fn task(&self) -> Option<TaskId> {
        self.task.get()
    }
    pub fn state(&self) -> UseFutureState<T> {
        match (&self.task.get(), &self.value()) {
            (Some(_), Some(val)) => UseFutureState::Reloading(val),
            (None, Some(val)) => UseFutureState::Complete(val),
            (None, None) => UseFutureState::Pending,
            (Some(_), None) => UseFutureState::Pending,
        }
    }
}
pub trait UseFutureDep: Sized + Clone {
    type Out;
    fn out(&self) -> Self::Out;
    fn apply(self, state: &mut Vec<Box<dyn Any>>) -> bool;
}
impl UseFutureDep for () {
    type Out = ();
    fn out(&self) -> Self::Out {}
    fn apply(self, _state: &mut Vec<Box<dyn Any>>) -> bool {
        false
    }
}
pub trait Dep: 'static + PartialEq + Clone {}
impl<T> Dep for T where T: 'static + PartialEq + Clone {}
impl<A: Dep> UseFutureDep for &A {
    type Out = A;
    fn out(&self) -> Self::Out {
        (*self).clone()
    }
    fn apply(self, state: &mut Vec<Box<dyn Any>>) -> bool {
        match state.get_mut(0).and_then(|f| f.downcast_mut::<A>()) {
            Some(val) => {
                if *val != *self {
                    *val = self.clone();
                    return true;
                }
            }
            None => {
                state.push(Box::new(self.clone()));
                return true;
            }
        }
        false
    }
}
macro_rules! impl_dep {
    (
        $($el:ident=$name:ident,)*
    ) => {
        impl< $($el),* > UseFutureDep for ($(&$el,)*)
        where
            $(
                $el: Dep
            ),*
        {
            type Out = ($($el,)*);
            fn out(&self) -> Self::Out {
                let ($($name,)*) = self;
                ($((*$name).clone(),)*)
            }
            #[allow(unused)]
            fn apply(self, state: &mut Vec<Box<dyn Any>>) -> bool {
                let ($($name,)*) = self;
                let mut idx = 0;
                let mut needs_regen = false;
                $(
                    match state.get_mut(idx).map(|f| f.downcast_mut::<$el>()).flatten() {
                        Some(val) => {
                            if *val != *$name {
                                *val = $name.clone();
                                needs_regen = true;
                            }
                        }
                        None => {
                            state.push(Box::new($name.clone()));
                            needs_regen = true;
                        }
                    }
                    idx += 1;
                )*
                needs_regen
            }
        }
    };
}
impl_dep!(A = a,);
impl_dep!(A = a, B = b,);
impl_dep!(A = a, B = b, C = c,);
impl_dep!(A = a, B = b, C = c, D = d,);
impl_dep!(A = a, B = b, C = c, D = d, E = e,);
impl_dep!(A = a, B = b, C = c, D = d, E = e, F = f,);
impl_dep!(A = a, B = b, C = c, D = d, E = e, F = f, G = g,);
impl_dep!(A = a, B = b, C = c, D = d, E = e, F = f, G = g, H = h,);
#[macro_export]
macro_rules! use_future {
    ($cx:ident, || $($rest:tt)*) => { use_future( $cx, (), |_| $($rest)* ) };
    ($cx:ident, | $($args:tt),* | $($rest:tt)*) => {
        use_future(
            $cx,
            ($($args),*),
            |($($args),*)| $($rest)*
        )
    };
}
#[cfg(test)]
mod tests {
    use super::*;
    #[allow(unused)]
    #[test]
    fn test_use_future() {
        use dioxus_core::prelude::*;
        struct MyProps {
            a: String,
            b: i32,
            c: i32,
            d: i32,
            e: i32,
        }
        async fn app(cx: Scope<'_, MyProps>) -> Element {
            let fut = use_future(cx, (), |_| async move {});
            let fut = use_future(cx, (&cx.props.a,), |(a,)| async move {});
            let fut = use_future(cx, (&cx.props.a, &cx.props.b), |(a, b)| async move { 123 });
            let a = use_future!(cx, || async move {
                });
            let b = &123;
            let c = &123;
            let a = use_future!(cx, |b, c| async move {
                let a = b + c;
                let blah = "asd";
            });
            todo!()
        }
    }
}