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::all_reservations::Bookings;
use crate::pages::appearance_settings::Appearance;
use crate::pages::customers::Customers;
use crate::pages::login::Login;
use crate::pages::mail_settings::MailSettings;
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());
    }
}

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

impl DialogHelper {
    pub fn new() -> Self {
        let opened = create_rw_signal(false);
        Self {
            opened
        }
    }

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

    pub fn is_open(&self) -> bool {
        self.opened.get()
    }
}

#[component]
pub fn App() -> impl IntoView {
    // Provides context that manages stylesheets, titles, meta tags, etc.
    provide_meta_context();
    init_locales();
    provide_context(MenuHelper::new());     // Only one menu can be opened
    provide_context(MenuOpener::new());     // Drawer opener
    provide_context(DialogHelper::new());   // Gray dialog background

    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>
                    }/>
                    <Route path="admin/mail_settings" view=|| view! {
                            <AdminPortal>
                                <MailSettings/>
                            </AdminPortal>
                    }/>
                    <Route path="admin/bookings" view=|| view! {
                            <AdminPortal>
                                <Bookings/>
                            </AdminPortal>
                    }/>
                    <Route path="admin/customers" view=|| view! {
                            <AdminPortal>
                                <Customers/>
                            </AdminPortal>
                    }/>
                    <Route path="admin/appearance" view=|| view! {
                            <AdminPortal>
                                <Appearance/>
                            </AdminPortal>
                    }/>
                </Routes>
            </main>
        </Router>
    }
}
