import React, {
  ChangeEventHandler,
  useCallback,
  useEffect,
  useRef,
  useState,
} from "react";
import { Box, Input, SxProps, TableCell } from "@mui/material";
import palette from "@/theme/devias-material-kit/palette";

interface Props<T> {
  value: T;
  onChange: (newValue: T) => void;
  renderValue?: (value: T) => string;
  convertValue: (inputValue: string) => T;
  cellSx?: SxProps;
  inputSx?: SxProps;
  error?: Boolean;
}

const TableCellEdit = <T,>({
  value,
  renderValue = (val) => `${val}`,
  convertValue,
  onChange,
  cellSx,
  inputSx,
  error,
}: Props<T>) => {
  const [editMode, setEditMode] = useState(false);
  // const [afterEdit, setAfterEdit] = useState(false);
  const [input, setInput] = useState<string>("");
  const inputRef = useRef<HTMLInputElement>(null);

  const onClick = useCallback(() => {
    setEditMode(true);
    setInput(`${value}`);
  }, [value]);

  const onInputChange = useCallback<ChangeEventHandler<HTMLInputElement>>(
    (e) => setInput(e.target.value),
    [],
  );

  const onBlur = useCallback(() => {
    setEditMode(false);
    // setAfterEdit(true)
    onChange(convertValue(input));
    setInput("");
  }, [convertValue, input, onChange]);

  useEffect(() => {
    if (editMode && inputRef.current) {
      inputRef.current?.focus();
    }
  }, [editMode]);

  return (
    <TableCell
      sx={{
        outline: editMode && !error ? "1px solid" : error ? "1px solid" : undefined,
        outlineColor: editMode && !error ? "primary.main" : error ? "error.main" : undefined,
        ...cellSx,
      }}
    >
      <Input
        sx={{
          display: editMode ? "block" : "none",
          "::after": { display: "none " },
          "::before": { display: "none " },
          height: "inherit",
          width: "inherit",
          padding: 0,
          fontWeight: "inherit",
          lineHeight: "inherit",
          ...inputSx,
        }}
        inputRef={inputRef}
        value={input}
        onChange={onInputChange}
        onBlur={onBlur}
        type={typeof value === "number" ? "number" : "text"}
      />
      <Box sx={{ display: editMode ? "none" : "block"  }} onClick={onClick}>
        {renderValue(value)}
      </Box>
    </TableCell>
  );
};

export default TableCellEdit;
