import { useState } from "react";
import { useParams } from "react-router-dom";
import { Box, Typography, Link, useTheme, useMediaQuery } from "@mui/material";
import { AnswerDraggableList } from "../AnswerDraggableList";

import { questionStore, updateQuestionsList } from "@root/questions";

import EnterIcon from "../../../assets/icons/questionsPage/enterIcon";
import SwitchDropDown from "./switchDropDown";
import ButtonsOptions from "../ButtonsOptions";

import type { QuizQuestionSelect } from "../../../model/questionTypes/select";

interface Props {
  totalIndex: number;
}

export default function DropDown({ totalIndex }: Props) {
  const [switchState, setSwitchState] = useState("setting");
  const quizId = Number(useParams().quizId);
  const { listQuestions } = questionStore();
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down(790));
  const question = listQuestions[quizId][totalIndex] as QuizQuestionSelect;

  const SSHC = (data: string) => {
    setSwitchState(data);
  };

  const addNewAnswer = () => {
    const answerNew = question.content.variants.slice();
    answerNew.push({ answer: "", hints: "", extendedText: "" });

    updateQuestionsList(quizId, totalIndex, {
      content: { ...question.content, variants: answerNew },
    });
  };

  return (
    <>
      <Box
        sx={{
          padding: isMobile ? "15px 20px 20px 20px" : "20px 20px 20px 20px ",
        }}
      >
        {question.content.variants.length === 0 ? (
          <Typography
            sx={{
              padding: "0 0 33px 80px",
              fontWeight: 400,
              fontSize: "18px",
              lineHeight: "21.33px",
              color: theme.palette.grey2.main,
            }}
          >
            Добавьте ответ
          </Typography>
        ) : (
          <AnswerDraggableList
            variants={question.content.variants}
            totalIndex={totalIndex}
          />
        )}
        <Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
          <Link
            component="button"
            variant="body2"
            sx={{ color: theme.palette.brightPurple.main }}
            onClick={addNewAnswer}
          >
            Добавьте ответ
          </Link>
          {isMobile ? null : (
            <>
              <Typography
                sx={{
                  fontWeight: 400,
                  fontSize: "18px",
                  lineHeight: "21.33px",
                  color: theme.palette.grey2.main,
                }}
              >
                или нажмите Enter
              </Typography>
              <EnterIcon />
            </>
          )}
        </Box>
      </Box>
      <ButtonsOptions
        switchState={switchState}
        SSHC={SSHC}
        totalIndex={totalIndex}
      />
      <SwitchDropDown switchState={switchState} totalIndex={totalIndex} />
    </>
  );
}
