import { Box, Typography } from "@mui/material";

import CustomTextField from "@ui_kit/CustomTextField";

import { useQuizViewStore, updateAnswer } from "@root/quizView/store";

import type { QuizQuestionText } from "../../../model/questionTypes/text";
import { enqueueSnackbar } from "notistack";
import { useQuestionsStore } from "@root/quizData/store"
import { sendAnswer } from "@api/quizRelase";

type TextProps = {
  currentQuestion: QuizQuestionText;
};

export const Text = ({ currentQuestion }: TextProps) => {
  const { settings } = useQuestionsStore()
  const { answers } = useQuizViewStore();
  const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};

  return (
    <Box>
      <Typography variant="h5">{currentQuestion.title}</Typography>
      <Box
        sx={{
          display: "flex",
          flexDirection: "column",
          width: "100%",
          marginTop: "20px",
        }}
      >
        <CustomTextField
          placeholder={currentQuestion.content.placeholder}
          //@ts-ignore
          value={answer || ""}
          onChange={async ({ target }) => {
            try {

              await sendAnswer({
                questionId: currentQuestion.id,
                body: target.value,
                //@ts-ignore
                qid: settings.qid
              })

              updateAnswer(currentQuestion.id, target.value)

            } catch (e) {
              console.log(e)
              enqueueSnackbar("ответ не был засчитан")
            }
          }
          }
        />
      </Box>
    </Box>
  );
};
