import React from 'react';
import {withNamespaces} from "react-i18next";
import {MdSettings, MdShoppingBasket, MdReceipt} from "react-icons/md";

import {withTheme, injectSheet} from '../../lib/theme';
import RouteCascaded from '../../lib/RouteCascaded';

import Nav from "../../component/Nav/Nav";
import Sidebar from "../../layout/ControlPanel/Sidebar";
import StyleGrid from "../../lib/StyleGrid";
import {needsPermission} from "../../feature/AuthPermission";
import {FEATURE_SHOP_GROUP, FEATURE_SHOP_ORDER, FEATURE_SHOP_SETTING, PERM_ADMIN} from "../../permissions";

const theming = (theme) => ({
    sidebarTop: {
        margin: 'auto ' + theme.gutter.base + 'px',
        [StyleGrid.smUp]: {
            margin: '0 0 ' + theme.gutter.base * 2 + 'px 0',
        }
    }
});

class NavShop extends React.Component {

    render() {
        const {t} = this.props;

        const navContent = (match) => {
            let content = [];

            content.push({
                path: match.url + '/settings',
                lbl: {icon: (p) => <MdSettings className={p.className} size={p.notxt ? '1.5em' : '1.125em'}/>, txt: t('nav.settings')},
                wrap: needsPermission([PERM_ADMIN, FEATURE_SHOP_SETTING])((props) => props.children)
            });

            content.push({
                path: match.url + '/products',
                lbl: {icon: (p) => <MdShoppingBasket className={p.className} size={p.notxt ? '1.5em' : '1.125em'}/>, txt: t('nav.products')},
                wrap: needsPermission([PERM_ADMIN, FEATURE_SHOP_GROUP])((props) => props.children)
            });

            content.push({
                path: match.url + '/orders',
                lbl: {icon: (p) => <MdReceipt className={p.className} size={p.notxt ? '1.5em' : '1.125em'}/>, txt: t('nav.orders')},
                wrap: needsPermission([PERM_ADMIN, FEATURE_SHOP_ORDER])((props) => props.children)
            });

            return content;
        };

        //const SideBarTop = p => (<p className={classes.sidebarTop}>{t('sidebar.shop-nav')}</p>);

        return (
            <React.Fragment>
                <Sidebar
                    center={(props => <Nav path='/shop' content={navContent} notxt={props.notxt}/>)}
                    textToggle={true}
                />

                {this.props.routes ? this.props.routes.map((route, i) => (
                    route.sidebar ?
                        <RouteCascaded key={i} {...route} component={route.sidebar}/> :
                        null
                )) : null}
            </React.Fragment>
        )
    };
}

export default withNamespaces('page--shop')(withTheme(injectSheet(theming)(NavShop)));