dioxus_router/contexts/
router.rsuse std::{
    collections::HashSet,
    sync::{Arc, Mutex},
};
use dioxus_history::history;
use dioxus_lib::prelude::*;
use crate::{
    components::child_router::consume_child_route_mapping, navigation::NavigationTarget,
    prelude::SiteMapSegment, routable::Routable, router_cfg::RouterConfig,
};
#[derive(Clone, Copy)]
struct RootRouterContext(Signal<Option<RouterContext>>);
pub fn root_router() -> Option<RouterContext> {
    if let Some(ctx) = ScopeId::ROOT.consume_context::<RootRouterContext>() {
        ctx.0.cloned()
    } else {
        ScopeId::ROOT.provide_context(RootRouterContext(Signal::new_in_scope(None, ScopeId::ROOT)));
        None
    }
}
pub(crate) fn provide_router_context(ctx: RouterContext) {
    if root_router().is_none() {
        ScopeId::ROOT.provide_context(RootRouterContext(Signal::new_in_scope(
            Some(ctx),
            ScopeId::ROOT,
        )));
    }
    provide_context(ctx);
}
#[derive(Debug, Clone)]
pub struct ExternalNavigationFailure(pub String);
pub(crate) type RoutingCallback<R> =
    Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
pub(crate) type AnyRoutingCallback = Arc<dyn Fn(RouterContext) -> Option<NavigationTarget>>;
struct RouterContextInner {
    unresolved_error: Option<ExternalNavigationFailure>,
    subscribers: Arc<Mutex<HashSet<ReactiveContext>>>,
    routing_callback: Option<AnyRoutingCallback>,
    failure_external_navigation: fn() -> Element,
    internal_route: fn(&str) -> bool,
    site_map: &'static [SiteMapSegment],
}
impl RouterContextInner {
    fn update_subscribers(&self) {
        for &id in self.subscribers.lock().unwrap().iter() {
            id.mark_dirty();
        }
    }
    fn subscribe_to_current_context(&self) {
        if let Some(rc) = ReactiveContext::current() {
            rc.subscribe(self.subscribers.clone());
        }
    }
    fn external(&mut self, external: String) -> Option<ExternalNavigationFailure> {
        match history().external(external.clone()) {
            true => None,
            false => {
                let failure = ExternalNavigationFailure(external);
                self.unresolved_error = Some(failure.clone());
                self.update_subscribers();
                Some(failure)
            }
        }
    }
}
#[derive(Clone, Copy)]
pub struct RouterContext {
    inner: CopyValue<RouterContextInner>,
}
impl RouterContext {
    pub(crate) fn new<R: Routable + 'static>(cfg: RouterConfig<R>) -> Self
    where
        <R as std::str::FromStr>::Err: std::fmt::Display,
    {
        let subscribers = Arc::new(Mutex::new(HashSet::new()));
        let mapping = consume_child_route_mapping();
        let myself = RouterContextInner {
            unresolved_error: None,
            subscribers: subscribers.clone(),
            routing_callback: cfg.on_update.map(|update| {
                Arc::new(move |ctx| {
                    let ctx = GenericRouterContext {
                        inner: ctx,
                        _marker: std::marker::PhantomData,
                    };
                    update(ctx).map(|t| match t {
                        NavigationTarget::Internal(r) => match mapping.as_ref() {
                            Some(mapping) => {
                                NavigationTarget::Internal(mapping.format_route_as_root_route(r))
                            }
                            None => NavigationTarget::Internal(r.to_string()),
                        },
                        NavigationTarget::External(s) => NavigationTarget::External(s),
                    })
                }) as Arc<dyn Fn(RouterContext) -> Option<NavigationTarget>>
            }),
            failure_external_navigation: cfg.failure_external_navigation,
            internal_route: |route| R::from_str(route).is_ok(),
            site_map: R::SITE_MAP,
        };
        history().updater(Arc::new(move || {
            for &rc in subscribers.lock().unwrap().iter() {
                rc.mark_dirty();
            }
        }));
        Self {
            inner: CopyValue::new_in_scope(myself, ScopeId::ROOT),
        }
    }
    pub(crate) fn include_prevent_default(&self) -> bool {
        history().include_prevent_default()
    }
    #[must_use]
    pub fn can_go_back(&self) -> bool {
        history().can_go_back()
    }
    #[must_use]
    pub fn can_go_forward(&self) -> bool {
        history().can_go_forward()
    }
    pub fn go_back(&self) {
        history().go_back();
        self.change_route();
    }
    pub fn go_forward(&self) {
        history().go_forward();
        self.change_route();
    }
    pub(crate) fn push_any(&self, target: NavigationTarget) -> Option<ExternalNavigationFailure> {
        {
            let mut write = self.inner.write_unchecked();
            match target {
                NavigationTarget::Internal(p) => history().push(p),
                NavigationTarget::External(e) => return write.external(e),
            }
        }
        self.change_route()
    }
    pub fn push(&self, target: impl Into<NavigationTarget>) -> Option<ExternalNavigationFailure> {
        let target = target.into();
        {
            let mut write = self.inner.write_unchecked();
            match target {
                NavigationTarget::Internal(p) => {
                    let history = history();
                    history.push(p)
                }
                NavigationTarget::External(e) => return write.external(e),
            }
        }
        self.change_route()
    }
    pub fn replace(
        &self,
        target: impl Into<NavigationTarget>,
    ) -> Option<ExternalNavigationFailure> {
        let target = target.into();
        {
            let mut state = self.inner.write_unchecked();
            match target {
                NavigationTarget::Internal(p) => {
                    let history = history();
                    history.replace(p)
                }
                NavigationTarget::External(e) => return state.external(e),
            }
        }
        self.change_route()
    }
    pub fn current<R: Routable>(&self) -> R {
        let absolute_route = self.full_route_string();
        let mapping = consume_child_route_mapping::<R>();
        match mapping.as_ref() {
            Some(mapping) => mapping
                .parse_route_from_root_route(&absolute_route)
                .unwrap_or_else(|| {
                    panic!("route's display implementation must be parsable by FromStr")
                }),
            None => R::from_str(&absolute_route).unwrap_or_else(|_| {
                panic!("route's display implementation must be parsable by FromStr")
            }),
        }
    }
    pub fn full_route_string(&self) -> String {
        let inner = self.inner.read();
        inner.subscribe_to_current_context();
        let history = history();
        history.current_route()
    }
    pub fn prefix(&self) -> Option<String> {
        let history = history();
        history.current_prefix()
    }
    pub fn clear_error(&self) {
        let mut write_inner = self.inner.write_unchecked();
        write_inner.unresolved_error = None;
        write_inner.update_subscribers();
    }
    pub fn site_map(&self) -> &'static [SiteMapSegment] {
        self.inner.read().site_map
    }
    pub(crate) fn render_error(&self) -> Option<Element> {
        let inner_write = self.inner.write_unchecked();
        inner_write.subscribe_to_current_context();
        inner_write
            .unresolved_error
            .as_ref()
            .map(|_| (inner_write.failure_external_navigation)())
    }
    fn change_route(&self) -> Option<ExternalNavigationFailure> {
        let self_read = self.inner.read();
        if let Some(callback) = &self_read.routing_callback {
            let myself = *self;
            let callback = callback.clone();
            drop(self_read);
            if let Some(new) = callback(myself) {
                let mut self_write = self.inner.write_unchecked();
                match new {
                    NavigationTarget::Internal(p) => {
                        let history = history();
                        history.replace(p)
                    }
                    NavigationTarget::External(e) => return self_write.external(e),
                }
            }
        }
        self.inner.read().update_subscribers();
        None
    }
    pub(crate) fn internal_route(&self, route: &str) -> bool {
        (self.inner.read().internal_route)(route)
    }
}
pub struct GenericRouterContext<R> {
    inner: RouterContext,
    _marker: std::marker::PhantomData<R>,
}
impl<R> GenericRouterContext<R>
where
    R: Routable,
{
    #[must_use]
    pub fn can_go_back(&self) -> bool {
        self.inner.can_go_back()
    }
    #[must_use]
    pub fn can_go_forward(&self) -> bool {
        self.inner.can_go_forward()
    }
    pub fn go_back(&self) {
        self.inner.go_back();
    }
    pub fn go_forward(&self) {
        self.inner.go_forward();
    }
    pub fn push(
        &self,
        target: impl Into<NavigationTarget<R>>,
    ) -> Option<ExternalNavigationFailure> {
        self.inner.push(target.into())
    }
    pub fn replace(
        &self,
        target: impl Into<NavigationTarget<R>>,
    ) -> Option<ExternalNavigationFailure> {
        self.inner.replace(target.into())
    }
    pub fn current(&self) -> R
    where
        R: Clone,
    {
        self.inner.current()
    }
    pub fn prefix(&self) -> Option<String> {
        self.inner.prefix()
    }
    pub fn clear_error(&self) {
        self.inner.clear_error()
    }
}