import { useParams } from "react-router-dom";
import {
  Box,
  Typography,
  Tooltip,
  useMediaQuery,
  useTheme,
} from "@mui/material";
import CustomCheckbox from "@ui_kit/CustomCheckbox";
import CustomTextField from "@ui_kit/CustomTextField";
import { useDebouncedCallback } from "use-debounce";

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

import InfoIcon from "../../../assets/icons/InfoIcon";

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

type SettingDropDownProps = {
  totalIndex: number;
};

export default function SettingDropDown({ totalIndex }: SettingDropDownProps) {
  const quizId = Number(useParams().quizId);
  const { listQuestions } = questionStore();

  const theme = useTheme();
  const isTablet = useMediaQuery(theme.breakpoints.down(1000));
  const isFigmaTablte = useMediaQuery(theme.breakpoints.down(990));

  const isMobile = useMediaQuery(theme.breakpoints.down(790));
  const question = listQuestions[quizId][totalIndex] as QuizQuestionSelect;
  const debounced = useDebouncedCallback((value) => {
    updateQuestionsList<QuizQuestionSelect>(quizId, totalIndex, {
      content: { ...question.content, innerName: value },
    });
  }, 1000);
  const debounceAnswer = useDebouncedCallback((value) => {
    updateQuestionsList<QuizQuestionSelect>(quizId, totalIndex, {
      content: { ...question.content, default: value },
    });
  }, 1000);

  return (
    <>
      <Box
        sx={{
          position: "relative",
          display: "flex",
          gap: "20px",
          width: "100%",
          justifyContent: "space-between",
          flexDirection: isMobile ? "column" : null,
        }}
      >
        <Box
          sx={{
            pt: isMobile ? "25px" : "20px",
            pb: isMobile ? "25px" : "20px",
            pl: "20px",
            display: "flex",
            flexDirection: "column",
            gap: "14px",
            width: "100%",
            maxWidth: isFigmaTablte ? "297px" : "360px",
          }}
        >
          <Typography
            sx={{
              height: isMobile ? "18px" : "auto",
              fontWeight: "500",
              fontSize: "18px",
              color: " #4D4D4D",
            }}
          >
            Настройки ответов
          </Typography>
          <CustomCheckbox
            label={"Можно несколько"}
            checked={question.content.multi}
            dataCy="multiple-answers-checkbox"
            handleChange={({ target }) =>
              updateQuestionsList<QuizQuestionSelect>(quizId, totalIndex, {
                content: { ...question.content, multi: target.checked },
              })
            }
          />
          <Box
            sx={{
              display: isMobile ? "none" : "block",
              mt: isMobile ? "11px" : "6px",
            }}
          >
            <Typography
              sx={{
                height: isMobile ? "18px" : "auto",
                fontWeight: "500",
                fontSize: "18px",
                color: " #4D4D4D",
                mb: "14px",
              }}
            >
              Текст в выпадающем списке
            </Typography>
            <CustomTextField
              placeholder={"Выберите вариант"}
              text={question.content.default}
              onChange={({ target }) => debounceAnswer(target.value)}
            />
          </Box>
        </Box>
        <Box
          sx={{
            pt: isMobile ? "0px" : "20px",
            pb: "20px",
            pl: isFigmaTablte ? (isMobile ? "20px" : "30px") : "0px",
            pr: isFigmaTablte ? "19px" : "20px",
            display: "flex",
            flexDirection: "column",
            gap: "14px",
            width: isMobile ? "auto" : "100%",
          }}
        >
          <Typography
            sx={{
              height: isMobile ? "18px" : "auto",
              fontWeight: "500",
              fontSize: "18px",
              color: " #4D4D4D",
            }}
          >
            Настройки вопросов
          </Typography>
          <CustomCheckbox
            label={"Необязательный вопрос"}
            checked={!question.required}
            handleChange={(e) => {
              updateQuestionsList<QuizQuestionSelect>(quizId, totalIndex, {
                required: !e.target.checked,
              });
            }}
          />
          <Box sx={{ position: "relative", display: "flex", alignItems: "flex-start" }}>
            <CustomCheckbox
              sx={{ height: isMobile ? "100%" : "26px", alignItems: isMobile ? "flex-start" : "center" }}
              label={"Внутреннее название вопроса"}
              checked={question.content.innerNameCheck}
              handleChange={({ target }) => {
                updateQuestionsList<QuizQuestionSelect>(quizId, totalIndex, {
                  content: {
                    ...question.content,
                    innerNameCheck: target.checked,
                    innerName: target.checked ? question.content.innerName : "",
                  },
                });
              }}
            />
            <Tooltip
              title="Будет отображаться как заголовок вопроса в приходящих заявках."
              placement="top"
            >
              <Box>
                <InfoIcon />
              </Box>
            </Tooltip>
          </Box>
          <Box
            sx={{
              width: "100%",
              pt: "20px",
              display: isMobile ? "block" : "none",
            }}
          >
            <Typography
              sx={{
                height: isMobile ? "18px" : "auto",
                fontWeight: "500",
                fontSize: "18px",
                color: " #4D4D4D",
                mb: "10px",
              }}
            >
              Текст в выпадающем списке
            </Typography>
            <CustomTextField
              placeholder={"Выберите вариант"}
              text={question.content.default}
              onChange={({ target }) => debounceAnswer(target.value)}
            />
          </Box>
          {question.content.innerNameCheck && (
            <CustomTextField
              placeholder={"Развёрнутое описание вопроса"}
              text={question.content.innerName}
              onChange={({ target }) => debounced(target.value)}
            />
          )}
        </Box>
      </Box>
    </>
  );
}
