import React from "react";
import { SPACING } from "../../../../data/spacing";
import { Grid } from "../../../../components/grid/Grid";
import { NavLink } from "../../../../components/nav-link/NavLink";
import LinkItem from "../../../../components/link-item/LinkItem";
import { VscBroadcast, VscPerson } from "react-icons/vsc";
import {
  ROUTE_ADMINX_DELEGATES_MANAGEMENT,
  ROUTE_ADMINX_DISPENSERS_INVENTORY,
  ROUTE_ADMINX_DISPENSERS_KILLHOUSES,
  ROUTE_ADMINX_DISPENSERS_MANAGEMENT,
  ROUTE_ADMINX_DISPENSERS_MANAGEMENT_V2,
  ROUTE_ADMINX_DISPENSERS_STEWARDS,
  ROUTE_ADMINX_DISPENSERS_STOCK,
  ROUTE_ADMINX_SALE_DESTRIBUTION_DETAILS,
  ROUTE_ADMINX_TRANSACTIONS,
  ROUTE_CITY_DISPENSERS_INVENTORY,
  ROUTE_CITY_REQUEST_DISTRIBUTION,
  ROUTE_CITY_REQUEST_TRANSACTIONS,
  ROUTE_PROVINCE_DISPENSERS_INVENTORY,
  ROUTE_PROVINCE_DISPENSERS_KILLHOUSES,
  ROUTE_PROVINCE_DISPENSERS_MANAGEMENT,
  ROUTE_PROVINCE_DISPENSERS_STEWARDS,
  ROUTE_PROVINCE_FINANCIAL_TRANSACTIONS,
  ROUTE_PROVINCE_SALE_DESTRIBUTION_DETAILS,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_INVENTORY,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_STOCK,
  ROUTE_PROVINCE_SUPERVISOR_REQUEST_DISTRIBUTION,
  ROUTE_PROVINCE_SUPERVISOR_REQUEST_TRANSACTIONS,
  ROUTE_PROVINCE_TRANSACTIONS,
  ROUTE_SLAUGHTER_DISPENSERS_KILLHOUSES,
  ROUTE_SLAUGHTER_DISPENSERS_MANAGEMENT,
  ROUTE_SLAUGHTER_DISPENSERS_STEWARDS,
  ROUTE_SUPER_ADMIN_DESTRIBUTION_DETAILS,
  ROUTE_SUPER_ADMIN_DISPENSERS_INVENTORY,
  ROUTE_SUPER_ADMIN_DISPENSERS_KILLHOUSES,
  ROUTE_SUPER_ADMIN_DISPENSERS_MANAGEMENT,
  ROUTE_SUPER_ADMIN_DISPENSERS_STEWARDS,
  ROUTE_SUPER_ADMIN_DISPENSERS_STOCK,
  ROUTE_SUPER_ADMIN_TRANSACTIONS,
} from "../../../../routes/routes";
import { useLocation } from "react-router-dom";
import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";
import { FaMoneyBill, FaStore, FaWarehouse } from "react-icons/fa";
import BadgeIcon from "@mui/icons-material/Badge";

const ROUTE_MAP = {
  management: {
    KillHouse: ROUTE_SLAUGHTER_DISPENSERS_MANAGEMENT,
    AdminX: ROUTE_ADMINX_DISPENSERS_MANAGEMENT,
    SuperAdmin: ROUTE_SUPER_ADMIN_DISPENSERS_MANAGEMENT,
    AdminXX: ROUTE_ADMINX_DISPENSERS_MANAGEMENT_V2,
    AdminXXX: ROUTE_ADMINX_DELEGATES_MANAGEMENT,
    default: ROUTE_PROVINCE_DISPENSERS_MANAGEMENT,
  },
  killhouses: {
    KillHouse: ROUTE_SLAUGHTER_DISPENSERS_KILLHOUSES,
    AdminX: ROUTE_ADMINX_DISPENSERS_KILLHOUSES,
    SuperAdmin: ROUTE_SUPER_ADMIN_DISPENSERS_KILLHOUSES,
    default: ROUTE_PROVINCE_DISPENSERS_KILLHOUSES,
  },
  stewards: {
    KillHouse: ROUTE_SLAUGHTER_DISPENSERS_STEWARDS,
    AdminX: ROUTE_ADMINX_DISPENSERS_STEWARDS,
    SuperAdmin: ROUTE_SUPER_ADMIN_DISPENSERS_STEWARDS,
    default: ROUTE_PROVINCE_DISPENSERS_STEWARDS,
  },
  inventory: {
    AdminX: ROUTE_ADMINX_DISPENSERS_INVENTORY,
    SuperAdmin: ROUTE_SUPER_ADMIN_DISPENSERS_INVENTORY,
    CityPoultry: ROUTE_CITY_DISPENSERS_INVENTORY,
    ProvinceSupervisor: ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_INVENTORY,
    default: ROUTE_PROVINCE_DISPENSERS_INVENTORY,
  },
  distribution: {
    AdminX: ROUTE_ADMINX_SALE_DESTRIBUTION_DETAILS,
    SuperAdmin: ROUTE_SUPER_ADMIN_DESTRIBUTION_DETAILS,
    ProvinceOperator: ROUTE_PROVINCE_SALE_DESTRIBUTION_DETAILS,
    CityPoultry: ROUTE_CITY_REQUEST_DISTRIBUTION,
    ProvinceSupervisor: ROUTE_PROVINCE_SUPERVISOR_REQUEST_DISTRIBUTION,
    default: ROUTE_PROVINCE_TRANSACTIONS,
  },
  transactions: {
    AdminX: ROUTE_ADMINX_TRANSACTIONS,
    SuperAdmin: ROUTE_SUPER_ADMIN_TRANSACTIONS,
    ProvinceFinancial: ROUTE_PROVINCE_FINANCIAL_TRANSACTIONS,
    CityPoultry: ROUTE_CITY_REQUEST_TRANSACTIONS,
    ProvinceSupervisor: ROUTE_PROVINCE_SUPERVISOR_REQUEST_TRANSACTIONS,
    default: ROUTE_PROVINCE_TRANSACTIONS,
  },
  stock: {
    AdminX: ROUTE_ADMINX_DISPENSERS_STOCK,
    SuperAdmin: ROUTE_SUPER_ADMIN_DISPENSERS_STOCK,
    ProvinceSupervisor: ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_STOCK,
  },
};

const getRoute = (routeType, role) => {
  const routeMap = ROUTE_MAP[routeType];
  return routeMap[role] || routeMap.default || null;
};

const isActive = (pathname, route) => (pathname === route ? "true" : null);

export const SlaughterHouseDispensersOperations = () => {
  const { pathname } = useLocation();
  const role = getRoleFromUrl();
  const isKillHouse = role === "KillHouse";
  const isAdminX = role === "AdminX";
  const hasStockAccess = [
    "AdminX",
    "SuperAdmin",
    "ProvinceSupervisor",
  ].includes(role);

  const managementRoute = getRoute("management", role);
  const managementRouteV2 = getRoute("management", "AdminXX");
  const managementRouteDelegates = getRoute("management", "AdminXXX");
  const killhousesRoute = getRoute("killhouses", role);
  const stewardsRoute = getRoute("stewards", role);
  const inventoryRoute = getRoute("inventory", role);
  const distributionRoute = getRoute("distribution", role);
  const transactionsRoute = getRoute("transactions", role);
  const stockRoute = getRoute("stock", role);

  return (
    <Grid
      container
      gap={SPACING.SMALL}
      p={SPACING.SMALL}
      direction={{ xs: "column", md: "row" }}
      justifyContent="center"
      style={{ placeContent: "baseline" }}
    >
      {isAdminX && (
        <Grid container direction="column" style={{ width: "100%" }}>
          <Grid container gap={SPACING.SMALL} justifyContent="center">
            <NavLink
              to={managementRouteDelegates}
              active={isActive(pathname, managementRouteDelegates)}
            >
              <LinkItem
                icon={<BadgeIcon fontSize="large" sx={{ color: "#244CCC" }} />}
                title="مدیریت نمایندگان"
                description="مدیریت نمایندگان"
              />
            </NavLink>
            <NavLink
              to={managementRouteV2}
              active={isActive(pathname, managementRouteV2)}
            >
              <LinkItem
                icon={<VscPerson size={30} color="#244CCC" />}
                title="مدیریت توزیع کنندگان دوم"
                description="مدیریت توزیع کنندگان دوم"
              />
            </NavLink>
            <NavLink
              to={managementRoute}
              active={isActive(pathname, managementRoute)}
            >
              <LinkItem
                icon={<VscPerson size={30} color="#244CCC" />}
                title="مدیریت توزیع کنندگان"
                description="مدیریت توزیع کنندگان"
              />
            </NavLink>

            {!isKillHouse && (
              <NavLink
                to={killhousesRoute}
                active={isActive(pathname, killhousesRoute)}
              >
                <LinkItem
                  icon={<FaStore size={30} color="#244CCC" />}
                  title="مدیریت کشتارگاه ها"
                  description="مدیریت کشتارگاه ها"
                />
              </NavLink>
            )}

            {!isKillHouse && (
              <NavLink
                to={stewardsRoute}
                active={isActive(pathname, stewardsRoute)}
              >
                <LinkItem
                  icon={<VscBroadcast size={30} color="#244CCC" />}
                  title="مدیریت مباشرین"
                  description="مدیریت مباشرین"
                />
              </NavLink>
            )}
          </Grid>
        </Grid>
      )}

      <Grid container direction="column" style={{ width: "100%" }}>
        <Grid container gap={SPACING.SMALL} justifyContent="center">
          {!isKillHouse && inventoryRoute && (
            <NavLink
              to={inventoryRoute}
              active={isActive(pathname, inventoryRoute)}
            >
              <LinkItem
                icon={<FaStore size={30} color="#244CCC" />}
                title="توزیع/فروش کشتارگاه"
                description="توزیع/فروش کشتارگاه"
              />
            </NavLink>
          )}

          {!isKillHouse && distributionRoute && (
            <NavLink
              to={distributionRoute}
              active={isActive(pathname, distributionRoute)}
            >
              <LinkItem
                icon={<FaMoneyBill size={30} color="#244CCC" />}
                title=" توزیع/فروش مباشر"
                description=" توزیع/فروش مباشر"
              />
            </NavLink>
          )}

          {!isKillHouse && transactionsRoute && (
            <NavLink
              to={transactionsRoute}
              active={isActive(pathname, transactionsRoute)}
            >
              <LinkItem
                icon={<FaWarehouse size={30} color="#244CCC" />}
                title="مدیریت انبار صنوف"
                description="تراکنش ها"
              />
            </NavLink>
          )}

          {hasStockAccess && stockRoute && (
            <NavLink to={stockRoute} active={isActive(pathname, stockRoute)}>
              <LinkItem
                icon={<FaWarehouse size={30} color="#244CCC" />}
                title="مانده انبار"
                description="مانده انبار"
              />
            </NavLink>
          )}
        </Grid>
      </Grid>
    </Grid>
  );
};
