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

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)"
      })
    });
    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 />
    });
  }

  if (bodyshop.messagingservicesid) {
    // Add Consent Settings tab
    items.push({
      key: "consent",
      label: t("bodyshop.labels.consent_settings"),
      children: <ShopInfoConsentComponent bodyshop={bodyshop} />
    });
  }

  return (
    <RbacWrapper action="shop:config">
      <Tabs activeKey={search.tab} onChange={(key) => history({ search: `?tab=${key}` })} items={items} />
    </RbacWrapper>
  );
}

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