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

import { Variant } from "./questions/Variant";
import { Images } from "./questions/Images";
import { Varimg } from "./questions/Varimg";
import { Emoji } from "./questions/Emoji";
import { Text } from "./questions/Text";
import { Select } from "./questions/Select";
import { Date } from "./questions/Date";
import { Number } from "./questions/Number";
import { File } from "./questions/File";
import { Page } from "./questions/Page";
import { Rating } from "./questions/Rating";
import { Footer } from "./Footer";

import { useState, type FC, useEffect } from "react";
import type { QuestionType } from "../../model/question/question";
import type { AnyTypedQuizQuestion } from "../../model/questionTypes/shared";
import { useCurrentQuiz } from "@root/quizes/hooks";
import { getQuestionByContentId } from "@root/questions/actions";

type QuestionProps = {
  questions: AnyTypedQuizQuestion[];
};

const QUESTIONS_MAP: any = {
  variant: Variant,
  images: Images,
  varimg: Varimg,
  emoji: Emoji,
  text: Text,
  select: Select,
  date: Date,
  number: Number,
  file: File,
  page: Page,
  rating: Rating,
};

export const Question = ({ questions }: QuestionProps) => {
  const quiz = useCurrentQuiz();
  const [currentQuestion, setCurrentQuestion] =
    useState<AnyTypedQuizQuestion>();

  useEffect(() => {
    const nextQuestion = getQuestionByContentId(quiz?.config.haveRoot || "");

    if (nextQuestion?.type) {
      setCurrentQuestion(nextQuestion);

      return;
    }

    setCurrentQuestion(questions[0]);
  }, []);

  if (!currentQuestion) return <>не смог отобразить вопрос</>;

  const QuestionComponent =
    QUESTIONS_MAP[currentQuestion.type as Exclude<QuestionType, "nonselected">];

  return (
    <Box>
      <Box
        sx={{
          minHeight: "calc(100vh - 75px)",
          width: "100%",
          maxWidth: "1440px",
          padding: "40px 25px 20px",
          margin: "0 auto",
        }}
      >
        <QuestionComponent currentQuestion={currentQuestion} />
      </Box>
      <Footer
        questions={questions}
        question={currentQuestion}
        setCurrentQuestion={setCurrentQuestion}
      />
    </Box>
  );
};
