import { Col, Row } from "reactstrap";
import React, { useEffect } from "react";
import ValidationField from "../../../Components/ValidationField/ValidationField";
import { useFormikContext } from "formik";
import ChoiceFields from "./Field/ChoiceFields";
import { FaCirclePlus } from "react-icons/fa6";
import { Choice } from "../../../types/Item";
import { useTranslation } from "react-i18next";
import DynamicTags from "./Tags/DynamicTags";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import MathInput from "./MathInput";
const Form = () => {
  const formik = useFormikContext<any>();
  const { setSuccess, Success, setSavedQuestionData } = useObjectToEdit();

  useEffect(() => {
    if (Success) {
      formik.setErrors({});
      formik.resetForm({ values: {} });
      setSuccess(false);
    }
  }, [Success]);

  useEffect(() => {
    setSavedQuestionData(formik.values);
  }, [formik?.values]);

  const handleAddChoice = () => {
    console.log(formik?.values?.QuestionOptions?.length);

    formik.setFieldValue("QuestionOptions", [
      ...((formik?.values as any)?.QuestionOptions as Choice[]),

      {
        answer: null,
        answer_image: null,
        isCorrect: 0,
      },
    ]);
  };
  const [t] = useTranslation();

  return (
    <Row className="w-100">
      <div className="exercise_form">
        {/* <MathInput/> */}
        <ValidationField
          className="textarea_exercise"
          name="content"
          label="details"
          type="TextArea"
        />
        <ValidationField
          className="file_exercise"
          name="image"
          label="attachment"
          type="File"
        />

        <div></div>
      </div>
      <div className=" flex "></div>
      {(((formik?.values as any)?.QuestionOptions as Choice[]) || []).map(
        (item: Choice, index: number) => {
          return <ChoiceFields key={index} index={index} data={item} />;
        },
      )}
      {formik?.values?.QuestionOptions?.length < 5 && (
        <p className="add_new_button">
          <FaCirclePlus onClick={handleAddChoice} size={23} />{" "}
          {t("header.add_new_choice")}
        </p>
      )}

      <DynamicTags />
    </Row>
  );
};

export default Form;
