import { sendAnswer } from "@/api/quizRelase";
import { RealTypedQuizQuestion } from "@/model/questionTypes/shared";
import { QuestionAnswer } from "@/stores/quizView";
import moment from "moment";
import { notReachable } from "./notReachable";

export function sendQuestionAnswer(
  quizId: string,
  question: RealTypedQuizQuestion,
  questionAnswer: QuestionAnswer | undefined
) {
  if (!questionAnswer) {
    return sendAnswer({
      questionId: question.id,
      body: "",
      qid: quizId,
    });
  }
  switch (question.type) {
    case "date": {
      if (!moment.isMoment(questionAnswer.answer)) throw new Error("Cannot send answer in date question");

      return sendAnswer({
        questionId: question.id,
        body: moment(questionAnswer.answer).format("YYYY.MM.DD"),
        qid: quizId,
      });
    }
    case "emoji": {
      const variant = question.content.variants.find((v) => v.id === questionAnswer.answer);
      if (!variant) throw new Error(`Cannot find variant with id ${questionAnswer.answer} in question ${question.id}`);

      return sendAnswer({
        questionId: question.id,
        body: variant.extendedText + " " + variant.answer,
        qid: quizId,
      });
    }
    case "file": {
      return;
    }
    case "images": {
      const variant = question.content.variants.find((v) => v.id === questionAnswer.answer);
      if (!variant) throw new Error(`Cannot find variant with id ${questionAnswer.answer} in question ${question.id}`);
      const body = {
        Image: variant.extendedText,
        Description: variant.answer,
      };
      if (!body) throw new Error(`Body of answer in question ${question.id} is undefined`);

      return sendAnswer({
        questionId: question.id,
        body: JSON.stringify(body),
        qid: quizId,
      });
    }
    case "number": {
      if (typeof questionAnswer.answer !== "string") throw new Error("Cannot send answer in select question");

      return sendAnswer({
        questionId: question.id,
        body: questionAnswer.answer,
        qid: quizId,
      });
    }
    case "page": {
      return;
    }
    case "rating": {
      if (typeof questionAnswer.answer !== "string") throw new Error("Cannot send answer in select question");

      return sendAnswer({
        questionId: question.id,
        body: String(questionAnswer.answer) + " из " + question.content.steps,
        qid: quizId,
      });
    }
    case "select": {
      if (typeof questionAnswer.answer !== "string") throw new Error("Cannot send answer in select question");

      const variant = question.content.variants[Number(questionAnswer.answer)];
      if (!variant) throw new Error(`Cannot find variant with id ${questionAnswer.answer} in question ${question.id}`);

      return sendAnswer({
        questionId: question.id,
        body: variant.answer,
        qid: quizId,
      });
    }
    case "text": {
      if (moment.isMoment(questionAnswer.answer)) throw new Error("Cannot send Moment in text question");

      return sendAnswer({
        questionId: question.id,
        body: questionAnswer.answer,
        qid: quizId,
      });
    }
    case "variant": {
      if (question.content.multi) {
        const answer = questionAnswer.answer;
        if (!Array.isArray(answer)) throw new Error("Cannot send answer in select question");

        const selectedVariants = question.content.variants.filter((v) => answer.includes(v.id));

        return sendAnswer({
          questionId: question.id,
          body: selectedVariants.map((v) => v.answer).join(", "),
          qid: quizId,
        });
      }

      const variant = question.content.variants.find((v) => v.id === questionAnswer.answer);
      if (!variant) throw new Error(`Cannot find variant with id ${questionAnswer.answer} in question ${question.id}`);

      return sendAnswer({
        questionId: question.id,
        body: variant.answer,
        qid: quizId,
      });
    }
    case "varimg": {
      const variant = question.content.variants.find((v) => v.id === questionAnswer.answer);
      if (!variant) throw new Error(`Cannot find variant with id ${questionAnswer.answer} in question ${question.id}`);
      const body = {
        Image: variant.extendedText,
        Description: variant.answer,
      };
      if (!body) throw new Error(`Body of answer in question ${question.id} is undefined`);

      return sendAnswer({
        questionId: question.id,
        body: JSON.stringify(body),
        qid: quizId,
      });
    }
    default:
      notReachable(question);
  }
}
