import { Space, TableColumnsType, Tooltip } from "antd";
import { useModalState } from "../../../zustand/Modal";
import { ModalEnum } from "../../../enums/Model";
import { MdOutlineEdit } from "react-icons/md";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { FaEye } from "react-icons/fa";
import ColumnsImage from "../../../Components/Columns/ColumnsImage";
import { ImageBaseURL } from "../../../api/config";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import { useNavigate } from "react-router-dom";
import useModalHandler from "../../../utils/useModalHandler";
import { useTranslation } from "react-i18next";
import {
  ABILITIES_ENUM,
  ABILITIES_VALUES_ENUM,
} from "../../../enums/abilities";
import { hasAbility } from "../../../utils/hasAbility";
import { canDeleteSubject, canEditSubject } from "../../../utils/hasAbilityFn";

export const useColumns = () => {
  const navigate = useNavigate();
  const { setObjectToEdit } = useObjectToEdit((state) => state);
  const { handel_open_model } = useModalHandler();

  const handelDeleteSubject = (data: any) => {
    setObjectToEdit(data);
    handel_open_model(ModalEnum?.SUBJECT_DELETE);
  };

  const handleEdit = (record: any) => {
    setObjectToEdit(record);
    handel_open_model(ModalEnum?.SUBJECT_EDIT);
  };

  const [t] = useTranslation();
  const columns: TableColumnsType<any> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "start",
    },
    {
      title: t("columns.subject_name"),
      dataIndex: "name",
      key: "name",
      align: "center",
    },
    {
      title: t("columns.image"),
      dataIndex: "image",
      key: "image",
      align: "center",
      render: (row: any) => {
        let str = row;
        // console.log(row);

        return <ColumnsImage src={str} />;
      },
    },

    {
      title: t("columns.card"),
      key: "name",
      align: "center",
      className: "SeeMoreEyeColumn",
      render: (text, record, index) => {
        const handelnavigate = () => {
          navigate(`${record?.id}`);
        };
        return (
          <div onClick={() => handelnavigate()} className="SeeMoreEye">
            <FaEye />
          </div>
        );
      },
    },

    {
      title: "",
      key: "actions",
      align: "end",
      width: "25vw",
      render: (text, record, index) => {
        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

        return (
          <Space size="middle" className={className}>
            {canEditSubject && (
              <Tooltip
                placement="top"
                title={t("practical.edit")}
                color="#E0E0E0"
              >
                <span onClick={() => handleEdit(record)}>
                  <MdOutlineEdit size={22} style={{ color: "#A098AE" }} />
                </span>
              </Tooltip>
            )}
            {canDeleteSubject && (
              <RiDeleteBin6Fill
                onClick={() => handelDeleteSubject(record)}
                size={22}
                style={{ color: "#C11313" }}
              />
            )}
          </Space>
        );
      },
    },
  ];

  return columns;
};
