import {Route, Routes} from "react-router-dom";
import {paths} from "../path";
import {useAuth} from "../hooks/useAuth";
import NotFoundPage from "./pages/notFound/NotFoundPage";
import {UserLayout} from "./layout/UserLayout";
import {HomeRedirect} from "./HomeRedirect";
import {PublicLayout} from "./layout/PublicLayout";
import QueuePage from "./pages/queue/QueuePage";
import LoginPage from "./pages/auth/sign-in/loginPage";
import {TelegramCode} from "./pages/auth/sign-in/telegram-code";
import {IngredientsPage} from "./pages/ingredients/IngredientsPage";
import {MenuPage} from "./pages/cocktails/MenuPage";
import {AllCocktailsPage} from "./pages/cocktails/AllCocktailsPage";
import {EditIngredientPage} from "./pages/ingredients/EditIngredientPage";
import {EditCocktailPage} from "./pages/cocktails/EditCocktailPage";
import {MyQueuePage} from "./pages/queue/MyQueuePage";
import {VisitorPage} from "./pages/VisitorPage";
import {CocktailMenuBarPage} from "./pages/cocktails/CocktailMenuBarPage";
import {MyBarPage} from "./pages/MyBarPage";
import {useEffect, useState} from "react";

export function NavigationRoutes() {
    const {auth} = useAuth();
    const [loadedRoutes, setLoadedRoutes] = useState(undefined);
    useEffect(() => {
        setLoadedRoutes(auth ? authPages : guestPages)
    }, [auth]);
    if (!loadedRoutes) {
        return null
    }
    return (
        <Routes>
            {loadedRoutes.map((page) => {
                return (
                    <Route
                        key={page.path + page.isPrivate + page.exact}
                        path={page.path}
                        exact={page.exact}
                        element={<ElementProvider isPrivate={page.isPrivate}>
                            {page.children}
                        </ElementProvider>}/>
                )
            })}
        </Routes>
    )
}

function ElementProvider({isPrivate, children}) {
    if (isPrivate) {
        return (<UserLayout>{children}</UserLayout>);
    } else {
        return (<PublicLayout>{children}</PublicLayout>);
    }
}

const authPages = [
    {
        children: (<HomeRedirect auth={true}/>),
        isPrivate: false,
        path: paths.home,
    },
    {
        path: paths.auth.signIn,
        children: (<LoginPage/>),
        isPrivate: false,
    },
    {
        path: paths.dashboard.overview,
        isPrivate: true,
        children: (<MenuPage/>),
        exact: true,
    },
    {
        path: paths.bar.cocktails,
        isPrivate: true,
        children: (<AllCocktailsPage/>)
    },
    {
        path: paths.bar.list,
        isPrivate: true,
        children: (<MyBarPage/>)
    },
    {
        path: paths.orders.my,
        isPrivate: true,
        children: (<MyQueuePage/>)
    },
    {
        path: paths.bar.ingredients,
        isPrivate: true,
        children: (<IngredientsPage/>)
    },
    {
        path: paths.bar.ordersQueue,
        isPrivate: true,
        children: (<QueuePage/>),
    },
    {
        path: paths.visitor.inBar,
        isPrivate: true,
        children: (<VisitorPage/>)
    },
    {
        path: paths.bar.ingredientEdit,
        isPrivate: true,
        forAdmin: true,
        children: (<EditIngredientPage/>)
    },
    {
        path: paths.bar.menu,
        isPrivate: true,
        children: (<CocktailMenuBarPage/>)
    },
    {
        path: paths.bar.cocktailEdit,
        isPrivate: true,
        forAdmin: true,
        children: (<EditCocktailPage/>)
    },
    {
        path: paths.notFound,
        isPrivate: false,
        children: (<NotFoundPage/>)
    },
]

const guestPages = [
    {
        path: paths.home,
        isPrivate: false,
        children: (<HomeRedirect auth={false}/>),
        exact: true,
    },
    {
        path: paths.auth.tg,
        isPrivate: false,
        children: (<TelegramCode/>),
        exact: false
    },
    {
        path: paths.auth.signIn,
        isPrivate:
            false,
        children: (<LoginPage/>),
    },
    {
        path: paths.notFound,
        isPrivate: false,
        children: (<NotFoundPage/>),
    },
]