import { Box, Typography } from "@mui/material";
import React from "react";
import { Grid } from "../components/grid/Grid";
import { useLocation, useParams } from "react-router-dom";
import {
  ROUTE_ADMINX_DELEGATES_MANAGEMENT,
  ROUTE_ADMINX_DISPENSERS,
  ROUTE_ADMINX_DISPENSERS_INVENTORY,
  ROUTE_ADMINX_DISPENSERS_KILLHOUSES,
  ROUTE_ADMINX_DISPENSERS_MANAGEMENT,
  ROUTE_ADMINX_DISPENSERS_MANAGEMENT_V2,
  ROUTE_ADMINX_DISPENSERS_SELL_CARCASS,
  ROUTE_ADMINX_DISPENSERS_STEWARDS,
  ROUTE_ADMINX_DISPENSER_DETAILS,
  ROUTE_ADMINX_SALE_DESTRIBUTION_DETAILS,
  ROUTE_CITY_DISPENSERS,
  ROUTE_CITY_DISPENSERS_INVENTORY,
  ROUTE_CITY_JIHAD_DISPENSERS,
  ROUTE_CITY_JIHAD_DISPENSERS_INVENTORY,
  ROUTE_CITY_JIHAD_DISPENSERS_KILLHOUSES,
  ROUTE_CITY_JIHAD_DISPENSERS_MANAGEMENT,
  ROUTE_CITY_JIHAD_DISPENSERS_SELL_CARCASS,
  ROUTE_CITY_JIHAD_DISPENSERS_STEWARDS,
  ROUTE_CITY_JIHAD_DISPENSER_DETAILS_VIEW,
  ROUTE_CITY_JIHAD_SALE_DESTRIBUTION_DETAILS,
  ROUTE_CITY_REQUEST_DISTRIBUTION,
  // ROUTE_ADMINX_SALE_DESTRIBUTION_DETAILS,
  ROUTE_PROVINCE_DISPENSERS,
  ROUTE_PROVINCE_DISPENSERS_INVENTORY,
  ROUTE_PROVINCE_DISPENSERS_KILLHOUSES,
  ROUTE_PROVINCE_DISPENSERS_MANAGEMENT,
  ROUTE_PROVINCE_DISPENSERS_SELL_CARCASS,
  ROUTE_PROVINCE_DISPENSERS_STEWARDS,
  ROUTE_PROVINCE_DISPENSER_DETAILS,
  ROUTE_PROVINCE_SALE_DESTRIBUTION_DETAILS,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_INVENTORY,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_KILLHOUSES,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_MANAGEMENT,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_SELL_CARCASS,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_STEWARDS,
  ROUTE_PROVINCE_SUPERVISOR_DISPENSER_DETAILS_VIEW,
  ROUTE_PROVINCE_SUPERVISOR_REQUEST_DISTRIBUTION,
  ROUTE_PROVINCE_SUPERVISOR_SALE_DESTRIBUTION_DETAILS,
  // ROUTE_PROVINCE_SALE_DESTRIBUTION_DETAILS,
  ROUTE_SLAUGHTER_DISPENSERS,
  ROUTE_SLAUGHTER_DISPENSERS_KILLHOUSES,
  ROUTE_SLAUGHTER_DISPENSERS_MANAGEMENT,
  ROUTE_SLAUGHTER_DISPENSERS_STEWARDS,
  ROUTE_SLAUGHTER_DISPENSER_DETAILS,
  ROUTE_SUPER_ADMIN_DESTRIBUTION_DETAILS,
  // ROUTE_SUPER_ADMIN_DESTRIBUTION_DETAILS,
  ROUTE_SUPER_ADMIN_DISPENSERS,
  ROUTE_SUPER_ADMIN_DISPENSERS_INVENTORY,
  ROUTE_SUPER_ADMIN_DISPENSERS_KILLHOUSES,
  ROUTE_SUPER_ADMIN_DISPENSERS_MANAGEMENT,
  ROUTE_SUPER_ADMIN_DISPENSERS_SELL_CARCASS,
  ROUTE_SUPER_ADMIN_DISPENSERS_STEWARDS,
  ROUTE_SUPER_ADMIN_DISPENSER_DETAILS,
} from "../routes/routes";
import { SPACING } from "../data/spacing";
import { SlaughterHouseDispensersOperations } from "../features/slaughter-house/components/slaughter-house-dispensers-operations/SlaughterHouseDispensersOperations";
import { BackButton } from "../components/back-button/BackButton";
import { SlaughterHouseDispensers } from "../features/slaughter-house/components/slaughter-house-dispensers/SlaughterHouseDispensers";
import { SlaughterHouseDispenserDetails } from "../features/slaughter-house/components/slaughter-house-dispenser-details/SlaughterHouseDispenserDetails";
import { ProvinceDispenserStewards } from "../features/province/components/province-dispenser-stewards/ProvinceDispenserStewards";
import { ProvinceDispenserKillhouses } from "../features/province/components/province-dispenser-killhouses/ProvinceDispenserKillhouses";
import { ProvinceDispensersSellCarcass } from "../features/province/components/province-dispensers-sell-carcass/ProvinceDispensersSellCarcass";
import { ProvinceDispensersKillhouses } from "../features/province/components/province-dispensers-killhouses/ProvinceDispensersKillhouses";
import { ProvinceDispensersViewKillHouse } from "../features/province/components/province-dispensers-view-killhouse/ProvinceDispensersViewKillHouse";
import { ProvinceDispencerSaleDistribution } from "../features/province/components/province-dispencer-sale-distribution/ProvinceDispencerSaleDistribution";
import { SlaughterHouseDispensersV2 } from "../features/slaughter-house/components/slaughter-house-dispensers-v2/SlaughterHouseDispensers";
import { SlaughterHouseDelegates } from "../features/slaughter-house/components/slaughter-house-delegates/SlaughterHouseDelegates";
// import { ProvinceDispencerSaleDistribution } from "../features/province/components/province-dispencer-sale-distribution/ProvinceDispencerSaleDistribution";

const DashboardTitle = () => (
  <Grid justifyContent="center" my={SPACING.MEDIUM}>
    <Grid>
      <Typography variant="h6">داشبورد مدیریت توزیع</Typography>
    </Grid>
  </Grid>
);

const renderComponentBasedOnPath = (pathname) => {
  const managementRoutes = [
    ROUTE_SLAUGHTER_DISPENSERS_MANAGEMENT,
    ROUTE_ADMINX_DISPENSERS_MANAGEMENT,
    ROUTE_PROVINCE_DISPENSERS_MANAGEMENT,
    ROUTE_SUPER_ADMIN_DISPENSERS_MANAGEMENT,
    ROUTE_CITY_JIHAD_DISPENSERS_MANAGEMENT,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_MANAGEMENT,
  ];

  const detailsRoutes = [
    ROUTE_ADMINX_DISPENSER_DETAILS,
    ROUTE_SLAUGHTER_DISPENSER_DETAILS,
    ROUTE_PROVINCE_DISPENSER_DETAILS,
    ROUTE_SUPER_ADMIN_DISPENSER_DETAILS,
    ROUTE_CITY_JIHAD_DISPENSER_DETAILS_VIEW,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSER_DETAILS_VIEW,
  ];

  const stewardsRoutes = [
    ROUTE_SLAUGHTER_DISPENSERS_STEWARDS,
    ROUTE_ADMINX_DISPENSERS_STEWARDS,
    ROUTE_SUPER_ADMIN_DISPENSERS_STEWARDS,
    ROUTE_PROVINCE_DISPENSERS_STEWARDS,
    ROUTE_CITY_JIHAD_DISPENSERS_STEWARDS,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_STEWARDS,
  ];

  const killhousesRoutes = [
    ROUTE_SLAUGHTER_DISPENSERS_KILLHOUSES,
    ROUTE_ADMINX_DISPENSERS_KILLHOUSES,
    ROUTE_SUPER_ADMIN_DISPENSERS_KILLHOUSES,
    ROUTE_PROVINCE_DISPENSERS_KILLHOUSES,
    ROUTE_CITY_JIHAD_DISPENSERS_KILLHOUSES,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_KILLHOUSES,
  ];

  const sellCarcassRoutes = [
    ROUTE_PROVINCE_DISPENSERS_SELL_CARCASS,
    ROUTE_SUPER_ADMIN_DISPENSERS_SELL_CARCASS,
    ROUTE_ADMINX_DISPENSERS_SELL_CARCASS,
    ROUTE_CITY_JIHAD_DISPENSERS_SELL_CARCASS,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_SELL_CARCASS,
  ];

  const inventoryRoutes = [
    ROUTE_ADMINX_DISPENSERS_INVENTORY,
    ROUTE_SUPER_ADMIN_DISPENSERS_INVENTORY,
    ROUTE_PROVINCE_DISPENSERS_INVENTORY,
    ROUTE_CITY_DISPENSERS_INVENTORY,
    ROUTE_CITY_JIHAD_DISPENSERS_INVENTORY,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSERS_INVENTORY,
  ];

  const distribution = [
    ROUTE_ADMINX_SALE_DESTRIBUTION_DETAILS,
    ROUTE_SUPER_ADMIN_DESTRIBUTION_DETAILS,
    ROUTE_PROVINCE_SALE_DESTRIBUTION_DETAILS,
    ROUTE_CITY_REQUEST_DISTRIBUTION,
    ROUTE_CITY_JIHAD_SALE_DESTRIBUTION_DETAILS,
    ROUTE_PROVINCE_SUPERVISOR_SALE_DESTRIBUTION_DETAILS,
    ROUTE_PROVINCE_SUPERVISOR_REQUEST_DISTRIBUTION,
  ];

  const managementRoutesV2 = [ROUTE_ADMINX_DISPENSERS_MANAGEMENT_V2];

  const managementRoutesDelegates = [ROUTE_ADMINX_DELEGATES_MANAGEMENT];

  if (managementRoutes.includes(pathname)) return <SlaughterHouseDispensers />;
  if (managementRoutesV2.includes(pathname))
    return <SlaughterHouseDispensersV2 />;
  if (managementRoutesDelegates.includes(pathname))
    return <SlaughterHouseDelegates />;
  if (distribution.includes(pathname))
    return <ProvinceDispencerSaleDistribution />;
  if (detailsRoutes.some((route) => pathname.includes(route)))
    return <SlaughterHouseDispenserDetails />;
  if (stewardsRoutes.some((route) => pathname.includes(route)))
    return <ProvinceDispenserStewards />;
  if (killhousesRoutes.some((route) => pathname.includes(route)))
    return <ProvinceDispenserKillhouses />;
  if (sellCarcassRoutes.some((route) => pathname.includes(route)))
    return <ProvinceDispensersSellCarcass />;
  if (inventoryRoutes.some((route) => pathname.includes(route)))
    return <ProvinceDispensersKillhouses />;

  return null;
};

const SlaughterHouseDispenserDashboard = () => {
  const { pathname } = useLocation();
  const { key, name } = useParams();

  const isDashboardRoute = [
    ROUTE_SLAUGHTER_DISPENSERS,
    ROUTE_ADMINX_DISPENSERS,
    ROUTE_SUPER_ADMIN_DISPENSERS,
    ROUTE_PROVINCE_DISPENSERS,
    ROUTE_CITY_DISPENSERS,
    ROUTE_CITY_JIHAD_DISPENSERS,
    ROUTE_PROVINCE_SUPERVISOR_DISPENSERS,
  ].includes(pathname);

  return (
    <Box display="flex" justifyContent="center">
      <Grid
        container
        direction="column"
        justifyContent="center"
        xs={12}
        sm={12}
        md={10}
        lg={10}
      >
        {isDashboardRoute ? (
          <>
            <DashboardTitle />
            <SlaughterHouseDispensersOperations />
          </>
        ) : (
          <BackButton />
        )}

        {!key && renderComponentBasedOnPath(pathname)}
        {key && <ProvinceDispensersViewKillHouse key={key} name={name} />}
      </Grid>
    </Box>
  );
};

export default React.memo(SlaughterHouseDispenserDashboard);
