import { Tabs } from "antd";
import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import queryString from "query-string";
import { useTranslation } from "react-i18next";
import ShopEmployeesContainer from "../../components/shop-employees/shop-employees.container";
import ShopInfoContainer from "../../components/shop-info/shop-info.container";
import ShopCsiConfig from "../../components/shop-csi-config/shop-csi-config.component";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
import InstanceRenderManager from "../../utils/instanceRenderMgr";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { setBreadcrumbs, setSelectedHeader } from "../../redux/application/application.actions";
import { selectBodyshop } from "../../redux/user/user.selectors";
import ShopInfoUsersComponent from "../../components/shop-users/shop-users.component";

import ShopTeamsContainer from "../../components/shop-teams/shop-teams.container";
import { HasFeatureAccess } from "../../components/feature-wrapper/feature-wrapper.component";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop
});
const mapDispatchToProps = (dispatch) => ({
  setSelectedHeader: (key) => dispatch(setSelectedHeader(key)),
  setBreadcrumbs: (breadcrumbs) => dispatch(setBreadcrumbs(breadcrumbs))
});

export function ShopPage({ bodyshop, setSelectedHeader, setBreadcrumbs }) {
  const { t } = useTranslation();
  const history = useNavigate();
  const search = queryString.parse(useLocation().search);

  useEffect(() => {
    document.title = t("titles.shop", {
      app: InstanceRenderManager({
        imex: "$t(titles.imexonline)",
        rome: "$t(titles.romeonline)",
        promanager: "$t(titles.promanager)"
      })
    });
    setSelectedHeader("shop");
    setBreadcrumbs([
      {
        link: "/manage/shop",
        label: t("titles.bc.shop", { shopname: bodyshop.shopname })
      }
    ]);
  }, [t, setSelectedHeader, setBreadcrumbs, bodyshop.shopname]);

  useEffect(() => {
    if (!search.tab) history({ search: "?tab=info" });
  }, [history, search]);

  const items = [
    {
      key: "info",
      label: t("bodyshop.labels.shopinfo"),
      children: <ShopInfoContainer />
    },
    {
      key: "employees",
      label: t("bodyshop.labels.employees"),
      children: <ShopEmployeesContainer />
    }
  ];

  if (bodyshop.md_tasks_presets.enable_tasks) {
    items.push({
      key: "teams",
      label: t("bodyshop.labels.employee_teams"),
      children: <ShopTeamsContainer />
    });
  }

  InstanceRenderManager({
    executeFunction: true,
    args: [],
    imex: () => {
      items.push({
        key: "licensing",
        label: t("bodyshop.labels.licensing"),
        children: <ShopInfoUsersComponent />
      });
    },
    rome: "USE_IMEX"
  });

  if (HasFeatureAccess({ featureName: "csi", bodyshop })) {
    items.push({
      key: "csiq",
      label: t("bodyshop.labels.csiq"),
      children: <ShopCsiConfig />
    });
  }
  return (
    <RbacWrapper action="shop:config">
      <Tabs activeKey={search.tab} onChange={(key) => history({ search: `?tab=${key}` })} items={items} />
    </RbacWrapper>
  );
}

export default connect(mapStateToProps, mapDispatchToProps)(ShopPage);
