import React from 'react';
import Loadable from 'react-loadable';
import Loading from './component/Loading';
import {FAIL_DISPLAY, needsPermission} from "./feature/AuthPermission";
import {FEATURE_CONTENT, FEATURE_MARKETING, FEATURE_MARKETING_NEWSLETTER, FEATURE_MEDIA, FEATURE_SHOP, FEATURE_SHOP_GROUP, FEATURE_SHOP_ORDER, FEATURE_SHOP_PRODUCT, FEATURE_SHOP_SETTING, FEATURE_USER, PERM_ADMIN} from "./permissions";
import {StoreSectionProvider} from "./feature/Content/Stores/Section";
import {StoreArticleProvider} from "./feature/Content/Stores/Article";
import {StoreSchemaProvider} from "./feature/Content/Stores/Schema";
import {StoreBlockProvider} from "./feature/Content/Stores/Block";
import {StorePageTypeProvider} from "./feature/Content/Stores/PageType";
import {StoreGroupProvider} from "./feature/Shop/Stores/Group";
import {StoreProductTypeProvider} from "./feature/Shop/Stores/ProductTypes";
import {StoreProductProvider} from "./feature/Shop/Stores/Product";
import {StoreOrderProvider} from "./feature/Shop/Stores/Order";

/**
 * Simple Content Pages with Header, Content and Footer
 */
const routesPage = (pages) => ({
    main: '/:lng/(auth|info)',
    routes: [
        {
            path: '/:lng/auth',
            component: Loadable({
                loader: () => import('./layout/Page/Auth'),
                loading: (props) => <Loading {...props} name='page/Auth'/>,
            }),
        }, {
            path: '/:lng/info',
            component: pages.info
        },
    ]
});

const routesControlPanel = (pages) => ({
    main: '/:lng/(dashboard|content|users|messages|shop|marketing|media|analyze|blocks|connections|settings)',
    routes: [
        {
            path: '/:lng/dashboard',
            content: needsPermission([PERM_ADMIN])(pages.dashboard)
        },
        {
            path: '/:lng/content',
            provider: (props) => (
                <StorePageTypeProvider>
                    <StoreBlockProvider>
                        <StoreSchemaProvider>
                            <StoreArticleProvider>
                                <StoreSectionProvider {...props}/>
                            </StoreArticleProvider>
                        </StoreSchemaProvider>
                    </StoreBlockProvider>
                </StorePageTypeProvider>
            ),
            sidebar: needsPermission([PERM_ADMIN, FEATURE_CONTENT])(Loadable({
                loader: () => import('./feature/Content/SectionList'),
                loading: (props) => <Loading {...props} name='Content/SectionList'/>,
            })),
            content: needsPermission([PERM_ADMIN, FEATURE_CONTENT], FAIL_DISPLAY)(Loadable({
                loader: () => import('./feature/Content/SectionOverview'),
                loading: (props) => <Loading {...props} name='Content/OverviewSection'/>,
            })),
            routes: [
                {
                    path: '/:lng/content/:section',
                    sidebar: needsPermission([PERM_ADMIN, FEATURE_CONTENT])(Loadable({
                        loader: () => import('./feature/Content/SectionArticleList'),
                        loading: (props) => <Loading {...props} name='Content/SectionArticleList'/>,
                    })),
                    content: needsPermission([PERM_ADMIN, FEATURE_CONTENT], FAIL_DISPLAY)(Loadable({
                        loader: () => import('./feature/Content/SectionOverview'),
                        loading: (props) => <Loading {...props} name='Content/OverviewSection'/>,
                    })),
                    routes: [
                        {
                            path: '/:lng/content/:section/:article',
                            content: needsPermission([PERM_ADMIN, FEATURE_CONTENT], FAIL_DISPLAY)(Loadable({
                                loader: () => import('./feature/Content/ArticleDetail'),
                                loading: (props) => <Loading {...props} name='Content/ArticleDetail'/>,
                            })),
                        }
                    ]
                }
            ]
        },
        {
            path: '/:lng/users',
            content: needsPermission([PERM_ADMIN, FEATURE_USER])(Loadable({
                loader: () => import('./feature/Users/Overview'),
                loading: (props) => <Loading {...props} name='Users/Overview'/>,
            }))
        },
        {
            path: '/:lng/shop',
            provider: (props) => (
                <StoreProductTypeProvider>
                    <StoreSchemaProvider>
                        <StoreOrderProvider>
                            <StoreGroupProvider>
                                <StoreProductProvider {...props}/>
                            </StoreGroupProvider>
                        </StoreOrderProvider>
                    </StoreSchemaProvider>
                </StoreProductTypeProvider>
            ),
            sidebar: needsPermission([PERM_ADMIN, FEATURE_SHOP])(Loadable({
                loader: () => import('./feature/Shop/NavShop'),
                loading: (props) => <Loading {...props} name='Shop/NavShop'/>,
            })),
            content: needsPermission([PERM_ADMIN, FEATURE_SHOP], FAIL_DISPLAY)(Loadable({
                loader: () => import('./feature/Shop/Overview'),
                loading: (props) => <Loading {...props} name='Shop/Overview'/>,
            })),
            routes: [
                {
                    path: '/:lng/shop/settings',
                    sidebar: needsPermission([PERM_ADMIN, FEATURE_SHOP_SETTING])(Loadable({
                        loader: () => import('./feature/Shop/NavShopSettings'),
                        loading: (props) => <Loading {...props} name='Shop/NavShopSettings'/>,
                    })),
                    content: needsPermission([PERM_ADMIN, FEATURE_SHOP_SETTING], FAIL_DISPLAY)(Loadable({
                        loader: () => import('./feature/Shop/Settings'),
                        loading: (props) => <Loading {...props} name='Shop/Settings'/>,
                    }))
                },
                {
                    path: '/:lng/shop/orders',
                    sidebar: needsPermission([PERM_ADMIN, FEATURE_SHOP_ORDER])(Loadable({
                        loader: () => import('./feature/Shop/NavShopOrders'),
                        loading: (props) => <Loading {...props} name='Shop/NavShopOrders'/>,
                    })),
                    content: needsPermission([PERM_ADMIN, FEATURE_SHOP_ORDER], FAIL_DISPLAY)(Loadable({
                        loader: () => import('./feature/Shop/Orders'),
                        loading: (props) => <Loading {...props} name='Shop/Orders'/>,
                    })),
                    routes: [
                        {
                            path: '/:lng/shop/orders/:state',
                            content: needsPermission([PERM_ADMIN, FEATURE_SHOP_ORDER], FAIL_DISPLAY)(Loadable({
                                loader: () => import('./feature/Shop/Orders'),
                                loading: (props) => <Loading {...props} name='Shop/Orders'/>,
                            })),
                        }
                    ]
                },
                {
                    path: '/:lng/shop/products',
                    sidebar: needsPermission([PERM_ADMIN, FEATURE_SHOP_GROUP])(Loadable({
                        loader: () => import('./feature/Shop/GroupList'),
                        loading: (props) => <Loading {...props} name='Shop/GroupList'/>,
                    })),
                    content: needsPermission([PERM_ADMIN, FEATURE_SHOP_GROUP], FAIL_DISPLAY)(Loadable({
                        loader: () => import('./feature/Shop/ObjectOverview'),
                        loading: (props) => <Loading {...props} name='Shop/ObjectOverview'/>,
                    })),
                    routes: [
                        {
                            path: '/:lng/shop/products/:group',
                            sidebar: needsPermission([PERM_ADMIN, FEATURE_SHOP_PRODUCT])(Loadable({
                                loader: () => import('./feature/Shop/ProductList'),
                                loading: (props) => <Loading {...props} name='Shop/ProductList'/>,
                            })),
                            content: needsPermission([PERM_ADMIN, FEATURE_SHOP_GROUP], FAIL_DISPLAY)(Loadable({
                                loader: () => import('./feature/Shop/GroupDetails'),
                                loading: (props) => <Loading {...props} name='Shop/GroupDetails'/>,
                            })),
                            routes: [
                                {
                                    path: '/:lng/shop/products/:group/:product',
                                    content: needsPermission([PERM_ADMIN, FEATURE_SHOP_PRODUCT], FAIL_DISPLAY)(Loadable({
                                        loader: () => import('./feature/Shop/ProductDetail'),
                                        loading: (props) => <Loading {...props} name='Shop/ProductDetail'/>,
                                    })),
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            path: '/:lng/marketing',
            sidebar: needsPermission([PERM_ADMIN, FEATURE_MARKETING])(Loadable({
                loader: () => import('./feature/Marketing/Nav'),
                loading: (props) => <Loading {...props} name='Marketing/Nav'/>,
            })),
            content: needsPermission([PERM_ADMIN, FEATURE_MARKETING], FAIL_DISPLAY)(Loadable({
                loader: () => import('./feature/Marketing/Overview'),
                loading: (props) => <Loading {...props} name='Marketing/Overview'/>,
            })),
            routes: [
                {
                    path: '/:lng/marketing/newsletter',
                    sidebar: needsPermission([PERM_ADMIN, FEATURE_MARKETING_NEWSLETTER])(Loadable({
                        loader: () => import('./feature/Marketing/NavNewsletter'),
                        loading: (props) => <Loading {...props} name='Marketing/NavNewsletter'/>,
                    })),
                    content: needsPermission([PERM_ADMIN, FEATURE_MARKETING_NEWSLETTER], FAIL_DISPLAY)(Loadable({
                        loader: () => import('./feature/Marketing/Overview'),
                        loading: (props) => <Loading {...props} name='Marketing/Overview'/>,
                    })),
                    routes: [
                        {
                            path: '/:lng/marketing/newsletter/user',
                            content: needsPermission([PERM_ADMIN, FEATURE_MARKETING_NEWSLETTER], FAIL_DISPLAY)(Loadable({
                                loader: () => import('./feature/Marketing/NewsletterUser'),
                                loading: (props) => <Loading {...props} name='Marketing/NewsletterUser'/>,
                            }))
                        },
                        {
                            path: '/:lng/marketing/newsletter/create',
                            content: needsPermission([PERM_ADMIN, FEATURE_MARKETING_NEWSLETTER], FAIL_DISPLAY)(Loadable({
                                loader: () => import('./feature/Marketing/NewsletterOverview'),
                                loading: (props) => <Loading {...props} name='Marketing/NewsletterCreate'/>,
                            }))
                        },
                        {
                            path: '/:lng/marketing/newsletter/history',
                            content: needsPermission([PERM_ADMIN, FEATURE_MARKETING_NEWSLETTER], FAIL_DISPLAY)(Loadable({
                                loader: () => import('./feature/Marketing/NewsletterOverview'),
                                loading: (props) => <Loading {...props} name='Marketing/NewsletterHistory'/>,
                            }))
                        },
                    ]
                }
            ]
        },
        {
            path: '/:lng/media',
            content: needsPermission([PERM_ADMIN, FEATURE_MEDIA], FAIL_DISPLAY)(Loadable({
                loader: () => import('./feature/Media/Overview'),
                loading: (props) => <Loading {...props} name='Media/Overview'/>,
            }))
        },
        {
            path: '/:lng/settings',
            content: Loadable({
                loader: () => import('./feature/Settings/Overview'),
                loading: (props) => <Loading {...props} name='Settings'/>,
            })
        },
        {
            path: '/:lng/(messages|analyze|blocks|connections)',
            content: needsPermission([PERM_ADMIN], FAIL_DISPLAY)(Loadable({
                loader: () => import('./feature/Demo/Overview'),
                loading: (props) => <Loading {...props} name='Demo/Overview'/>,
            }))
        },
    ]
});

export {routesPage, routesControlPanel};