import React, { useState } from "react";
import {
  FormControl,
  InputLabel,
  MenuItem,
  Select,
  Typography,
  Box,
  Divider,
} from "@mui/material";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { SlaughterAllocateToGuild } from "../slaughter-allocate-to-guild/SlaughterAllocateToGuild";
import { SlaughterAllocateForFreezing } from "../slaughter-allocate-for-freezing/SlaughterAllocateForFreezing";
import { SlaughterSellCarcassOutProvinceSellSubmitSell } from "../slaughter-sell-carcass-out-province-submit-sell/SlaughterSellCarcassOutProvinceSellSubmitSell";
import { StewardSegmentSubmitOperation } from "../../../guild/components/StewardSegmentSubmitOperation";

const operations = [
  {
    value: "inProvinceSale",
    label: "توزیع/ فروش درون استان",
    description: "ثبت توزیع و فروش لاشه داخل استان",
  },
  {
    value: "coldStorage",
    label: "انتقال به سردخانه",
    description: "ثبت انتقال لاشه به سردخانه",
  },
  {
    value: "outProvinceSale",
    label: "فروش خارج استان",
    description: "ثبت فروش لاشه به خارج از استان",
  },
  {
    value: "segmentation",
    label: "قطعه بندی",
    description: "ثبت قطعه بندی لاشه",
  },
];

export const SlaughterSubmitOperations = ({
  updateTable,
  priceInfo,
  slaughterProducts,
  fetchApiData,
}) => {
  const [selectedOperation, setSelectedOperation] = useState("");

  const handleOperationChange = (event) => {
    setSelectedOperation(event.target.value);
  };

  const getOperationContent = () => {
    const commonProps = {
      updateTable,
      priceInfo,
      remainWeight: slaughterProducts?.[0]?.totalRemainWeight,
      fetchApiData,
    };

    switch (selectedOperation) {
      case "inProvinceSale":
        return (
          <SlaughterAllocateToGuild
            {...commonProps}
            sellerType="KillHouse"
            sellType="exclusive"
            killHouseAllocation={true}
          />
        );
      case "coldStorage":
        return (
          <SlaughterAllocateForFreezing
            {...commonProps}
            sellerType="KillHouse"
            sellType="free"
            killHouseAllocation={false}
          />
        );
      case "outProvinceSale":
        return (
          <SlaughterSellCarcassOutProvinceSellSubmitSell
            updateTable={updateTable}
          />
        );
      case "segmentation":
        return (
          <StewardSegmentSubmitOperation
            updateTable={updateTable}
            productKey={slaughterProducts?.[0]?.key}
          />
        );
      default:
        return null;
    }
  };

  const selectedOperationData = operations.find(
    (op) => op.value === selectedOperation
  );

  return (
    <Grid container direction="column" gap={SPACING.MEDIUM} width="100%">
      <FormControl fullWidth>
        <InputLabel id="operation-select-label">انتخاب عملیات</InputLabel>
        <Select
          labelId="operation-select-label"
          id="operation-select"
          value={selectedOperation}
          label="انتخاب عملیات"
          onChange={handleOperationChange}
        >
          {operations.map((operation) => (
            <MenuItem key={operation.value} value={operation.value}>
              {operation.label}
            </MenuItem>
          ))}
        </Select>
      </FormControl>

      {selectedOperationData && (
        <>
          <Divider />
          <Grid container direction="column" gap={SPACING.TINY}>
            <Typography variant="h6" color="primary">
              {selectedOperationData.label}
            </Typography>
            <Typography variant="body2" color="textSecondary">
              {selectedOperationData.description}
            </Typography>
          </Grid>
        </>
      )}

      <Box sx={{ width: "100%" }}>{getOperationContent()}</Box>
    </Grid>
  );
};
