import FilterOptionList from "@/core/components/DataTable/filter/FilterOptionList";
import CustomDatePicker from "@/core/components/DataTable/filter/fieldsType/CustomDate/CustomDatePicker";
import CustomDatePickerRange from "@/core/components/DataTable/filter/fieldsType/CustomDate/CustomDatePickerRange";
import CustomSelect from "@/core/components/DataTable/filter/fieldsType/CustomSelect";
import CustomSelectMultiple from "@/core/components/DataTable/filter/fieldsType/CustomSelectMultiple";
import CustomTextField from "@/core/components/DataTable/filter/fieldsType/CustomTextField";
import CustomTextFieldRange from "@/core/components/DataTable/filter/fieldsType/CustomTextFieldRange";
import { useState } from "react";

const columnFilterModeOptionFa = {
  equals: "برابر",
  notEquals: "نابرابر",
  contains: "شامل",
  lessThan: "کوچکتر",
  greaterThan: "بزرگتر",
  fuzzy: "فازی",
  between: "مابین",
};

function FilterBodyField({
  column,
  filterParameters,
  handleChange,
  handleBlur,
  dependencyFieldValue,
  setFieldValue,
  resetForm,
  errors,
}) {
  const [anchorEl, setAnchorEl] = useState(null);
  const defaultFilterTranslation =
    columnFilterModeOptionFa[filterParameters.filterMode] ||
    filterParameters.filterMode;

  const handleOpenFilterBox = (event) => {
    setAnchorEl(event.currentTarget);
  };

  const renderField = () => {
    const commonProps = {
      column,
      filterParameters,
      defaultFilterTranslation,
      handleOpenFilterBox,
      dependencyFieldValue,
      setFieldValue,
      handleChange,
      handleBlur,
      errors,
    };

    switch (filterParameters.datatype) {
      case "numeric":
        if (filterParameters.filterMode === "between") {
          return <CustomTextFieldRange {...commonProps} />;
        }
        if (filterParameters.filterMode === "equals") {
          return column.ColumnSelectComponent ? (
            <column.ColumnSelectComponent {...commonProps} />
          ) : (
            <CustomSelect {...commonProps} />
          );
        }
        break;
      case "date":
        if (filterParameters.filterMode === "equals") {
          return <CustomDatePicker {...commonProps} />;
        }
        if (filterParameters.filterMode === "between") {
          return <CustomDatePickerRange {...commonProps} />;
        }
        break;

      case "array":
        if (filterParameters.filterMode === "equals") {
          return <CustomSelectMultiple {...commonProps} />;
        }
        break;

      default:
        return <CustomTextField {...commonProps} />;
    }
  };

  return (
    <>
      {renderField()}
      {Array.isArray(column.columnFilterModeOptions) && (
        <FilterOptionList
          column={column}
          anchorEl={anchorEl}
          setAnchorEl={setAnchorEl}
          filterParameters={filterParameters}
          filterType={filterParameters.filterMode}
          handleChange={handleChange}
          resetForm={resetForm}
          filterOption={column.columnFilterModeOptions}
          columnFilterModeOptionFa={columnFilterModeOptionFa}
        />
      )}
    </>
  );
}

export default FilterBodyField;
