import { Box } from "@mui/material";
import React, { useState, useEffect } from "react";
import { Grid } from "../components/grid/Grid";
import { SPACING } from "../data/spacing";
import { useLocation } from "react-router-dom";
import {
  ROUTE_COOPERATIVE_PRODUCT_DISTRIBUTION,
  ROUTE_COOPERATIVE_PRODUCT_DISTRIBUTION_ALLOCATION,
  ROUTE_COOPERATIVE_PRODUCT_DISTRIBUTION_POLICY,
  ROUTE_COOPERATIVE_PRODUCT_TRANSACTIONS,
  ROUTE_COOPERATIVE_HERDS,
  ROUTE_COOPERATIVE_RANCHERS,
  ROUTE_COOPERATIVE_USERS,
  ROUTE_PROVINCE_JAHAD_PRODUCT_DISTRIBUTION,
  ROUTE_PROVINCE_JAHAD_PRODUCT_DISTRIBUTION_ALLOCATION,
  ROUTE_PROVINCE_JAHAD_PRODUCT_DISTRIBUTION_POLICY,
  ROUTE_PROVINCE_JAHAD_PRODUCT_SHARES,
  ROUTE_PROVINCE_JAHAD_PRODUCT_TRANSACTIONS,
  ROUTE_PROVINCE_JAHAD_COOPERATIVES,
  ROUTE_PROVINCE_JAHAD_HERDS,
  ROUTE_PROVINCE_JAHAD_RANCHERS,
  ROUTE_PROVINCE_JAHAD_SELL_REPORT,
  ROUTE_PROVINCE_JAHAD_UNIONS,
  ROUTE_PROVINCE_JAHAD_USERS,
  ROUTE_UNION_PRODUCT_DISTRIBUTION,
  ROUTE_UNION_PRODUCT_DISTRIBUTION_ALLOCATION,
  ROUTE_UNION_PRODUCT_DISTRIBUTION_POLICY,
  ROUTE_UNION_PRODUCT_TRANSACTIONS,
  ROUTE_UNION_COOPERATIVES,
  ROUTE_UNION_HERDS,
  ROUTE_UNION_RANCHERS,
  ROUTE_UNION_SELL_REPORT,
  ROUTE_UNION_USERS,
} from "../routes/routes";
import { ProvinceJahadUnions } from "../features/province-jahad/components/province-jahad-unions/ProvinceJahadUnions";
import { ProvinceJahadCooperatives } from "../features/province-jahad/components/province-jahad-cooperatives/ProvinceJahadCooperatives";
import { ProvinceJahadHerds } from "../features/province-jahad/components/province-jahad-herds/ProvinceJahadHerds";
import { ProvinceJahadRanchers } from "../features/province-jahad/components/province-jahad-ranchers/ProvinceJahadRanchers";
import { ProvinceJahadUsers } from "../features/province-jahad/components/province-jahad-users/ProvinceJahadUsers";
import { ProvinceJahadBranDistributions } from "../features/province-jahad/components/province-jahad-bran-distributions/ProvinceJahadBranDistributions";
import { BackButton } from "../components/back-button/BackButton";
import { ProvinceJahadBranDistributionsAllocation } from "../features/province-jahad/components/province-jahad-bran-distributions-allocation/ProvinceJahadBranDistributionsAllocation";
import { ProvinceJahadBranDistributionsPolicy } from "../features/province-jahad/components/province-jahad-bran-distributions-policy/ProvinceJahadBranDistributionsPolicy";
import { ProvinceJahadShares } from "../features/province-jahad/components/province-jahad-shares/ProvinceJahadShares";
import { ProvinceJahadTransactions } from "../features/province-jahad/components/province-jahad-transactions/ProvinceJahadTransactions";
import { ProvinceJahadSellReport } from "../features/province-jahad/components/province-jahad-sell-report/ProvinceJahadSellReport";
import ProductSelector from "../features/province-jahad/components/product-selector/ProductSelector";

const LOCAL_STORAGE_KEY = "selectedProduct";

const ProvinceJahadRequests = () => {
  const { pathname } = useLocation();

  const saved = localStorage.getItem(LOCAL_STORAGE_KEY);
  let initialProduct;
  try {
    initialProduct = saved ? JSON.parse(saved) : { key: "bran", label: "سبوس" };
  } catch {
    initialProduct = { key: "bran", label: "سبوس" };
  }

  const [selectedProduct, setSelectedProduct] = useState(initialProduct);

  useEffect(() => {
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(selectedProduct));
  }, [selectedProduct]);

  return (
    <Box display="flex" justifyContent="center">
      <Grid
        container
        direction="column"
        justifyContent="center"
        xs={12}
        sm={12}
        md={10}
        lg={10}
        my={SPACING.MEDIUM}
      >
        <BackButton />

        {pathname === ROUTE_PROVINCE_JAHAD_UNIONS && <ProvinceJahadUnions />}

        {(pathname === ROUTE_PROVINCE_JAHAD_COOPERATIVES ||
          pathname === ROUTE_UNION_COOPERATIVES) && (
          <ProvinceJahadCooperatives />
        )}

        {(pathname === ROUTE_PROVINCE_JAHAD_RANCHERS ||
          pathname === ROUTE_UNION_RANCHERS ||
          pathname === ROUTE_COOPERATIVE_RANCHERS) && <ProvinceJahadRanchers />}

        {(pathname === ROUTE_PROVINCE_JAHAD_HERDS ||
          pathname === ROUTE_UNION_HERDS ||
          pathname === ROUTE_COOPERATIVE_HERDS) && <ProvinceJahadHerds />}

        {(pathname === ROUTE_PROVINCE_JAHAD_USERS ||
          pathname === ROUTE_UNION_USERS ||
          pathname === ROUTE_COOPERATIVE_USERS) && <ProvinceJahadUsers />}

        {(pathname === ROUTE_PROVINCE_JAHAD_PRODUCT_DISTRIBUTION ||
          pathname === ROUTE_UNION_PRODUCT_DISTRIBUTION ||
          pathname === ROUTE_COOPERATIVE_PRODUCT_DISTRIBUTION) && (
          <>
            <ProductSelector onSelect={(p) => setSelectedProduct(p)} />
            <ProvinceJahadBranDistributions product={selectedProduct} />
          </>
        )}

        {(pathname === ROUTE_PROVINCE_JAHAD_PRODUCT_DISTRIBUTION_ALLOCATION ||
          pathname === ROUTE_UNION_PRODUCT_DISTRIBUTION_ALLOCATION ||
          pathname === ROUTE_COOPERATIVE_PRODUCT_DISTRIBUTION_ALLOCATION) && (
          <ProvinceJahadBranDistributionsAllocation product={selectedProduct} />
        )}

        {(pathname === ROUTE_PROVINCE_JAHAD_PRODUCT_DISTRIBUTION_POLICY ||
          pathname === ROUTE_UNION_PRODUCT_DISTRIBUTION_POLICY ||
          pathname === ROUTE_COOPERATIVE_PRODUCT_DISTRIBUTION_POLICY) && (
          <ProvinceJahadBranDistributionsPolicy product={selectedProduct} />
        )}

        {(pathname === ROUTE_PROVINCE_JAHAD_PRODUCT_TRANSACTIONS ||
          pathname === ROUTE_UNION_PRODUCT_TRANSACTIONS ||
          pathname === ROUTE_COOPERATIVE_PRODUCT_TRANSACTIONS) && (
          <ProvinceJahadTransactions product={selectedProduct} />
        )}

        {pathname === ROUTE_PROVINCE_JAHAD_PRODUCT_SHARES && (
          <ProvinceJahadShares product={selectedProduct} />
        )}

        {(pathname === ROUTE_UNION_SELL_REPORT ||
          pathname === ROUTE_PROVINCE_JAHAD_SELL_REPORT) && (
          <ProvinceJahadSellReport product={selectedProduct} />
        )}
      </Grid>
    </Box>
  );
};

export default ProvinceJahadRequests;
