use crate::locales::init_locales;
use crate::pages::home_page::HomePage;
use crate::pages::settings::Settings;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use crate::components::admin_portal::AdminPortal;
use crate::components::header::Header;
use crate::components::user_menu::MenuOpener;
use crate::pages::login::Login;
use crate::pages::public::Public;

#[derive(Clone, Copy)]
pub struct MenuHelper {
    opened: RwSignal<MenuOpener>
}

impl MenuHelper {
    pub fn new() -> Self {
        let opened = create_rw_signal(MenuOpener::new());
        Self {
            opened
        }
    }

    pub fn set_opened(&self, opened: MenuOpener) {
        self.opened.set(opened);
    }

    pub fn close(&self) {
        self.opened.get().close();
    }

    pub fn reset(&self) {
        self.opened.set(MenuOpener::new());
    }
}

#[component]
pub fn App() -> impl IntoView {
    // Provides context that manages stylesheets, titles, meta tags, etc.
    provide_meta_context();
    init_locales();
    provide_context(MenuHelper::new());

    view! {
        <Header/>

        <Router>
            <main>
                <Routes>
                    <Route path="" view=|| view! { <Public/> }/>
                    <Route path="login" view=|| view! { <Login/> }/>
                    <Route path="admin" view=|| view! {
                            <AdminPortal>
                                <HomePage/>
                            </AdminPortal>
                    }/>
                    <Route path="admin/settings" view=|| view! {
                            <AdminPortal>
                                <Settings/>
                            </AdminPortal>
                    }/>
                </Routes>
            </main>
        </Router>
    }
}
