import React, { useEffect } from "react";
import { Choice } from "../../../../../types/Item";
import ValidationField from "../../../../../Components/ValidationField/ValidationField";
import { useFormikContext } from "formik";
import { useTranslation } from "react-i18next";
import { getCharFromNumber } from "../../../../../utils/getCharFromNumber";
import TextField from "./TextField";
import File from "./File";
import { FaTrash } from "react-icons/fa";
import { useObjectToEdit } from "../../../../../zustand/ObjectToEditState";
import { toast } from "react-toastify";
import CheckboxField from "./CheckboxField";
import HintField from "./HintField";

const QuestionFIeld = ({ index, data }: { index: number; data: Choice }) => {
  const formik = useFormikContext<any>();
  const { setDeletedQuestions, DeletedQuestions } = useObjectToEdit();

  const [t] = useTranslation();
  useEffect(() => {
    setDeletedQuestions([]);
  }, [window?.location.pathname]);

  const handleDeleteQuestion = () => {
    const DeleteQuestionId = formik.values.Questions?.[index];
    if (DeleteQuestionId?.id) {
      setDeletedQuestions([...DeletedQuestions, DeleteQuestionId]);
    }
    const updatedAnswers = formik.values.Questions.filter(
      (_: any, i: any) => i !== index,
    );
    formik.setFieldValue(`Questions`, updatedAnswers);
  };

  return (
  <div className="d-c">
    <div className="ChoiceFields">
      <TextField
        className="textarea_exercise"
        placeholder={"choice"}
        label2={t(`input.question`) + ` ` + `${index + 1}`}
        name={index}
        type="TextArea"
      />
      <File
        className="file_exercise"
        label={"attachment"}
        name={index}
        type="File"
      />
      <CheckboxField
        className="canAnswersBeShuffled"
        label={"canAnswersBeShuffled"}
        name={index}
      />
      
      

      <p className="delete_question_options">
        <FaTrash onClick={handleDeleteQuestion} size={17} />
      </p>
    </div>

    <div>
        <HintField placeholder={"hint"} name={index} label="hint" id={`hint`} />
      </div>
  
  </div>
  );
};

export default QuestionFIeld;
