import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
import { updateQuestion } from "@root/questions/actions";
import CustomCheckbox from "@ui_kit/CustomCheckbox";
import CustomTextField from "@ui_kit/CustomTextField";
import { memo } from "react";
import type { QuizQuestionSelect } from "@frontend/squzanswerer";

type SettingDropDownProps = {
  questionId: string;
  questionContentDefault: string;
  isRequired: boolean;
};

const SettingDropDown = memo<SettingDropDownProps>(function ({ questionId, questionContentDefault, isRequired }) {
  const theme = useTheme();
  const isFigmaTablte = useMediaQuery(theme.breakpoints.down(990));
  const isMobile = useMediaQuery(theme.breakpoints.down(790));

  const setContentDefault = (value: string) => {
    updateQuestion(questionId, (question) => {
      if (question.type !== "select") return;

      question.content.default = value;
    });
  };

  return (
    <>
      <Box
        sx={{
          position: "relative",
          display: "flex",
          gap: "20px",
          width: "100%",
          justifyContent: "space-between",
          flexDirection: isMobile ? "column" : null,
        }}
      >
        <Box
          sx={{
            pt: isMobile ? "0" : "20px",
            pb: isMobile ? "0" : "20px",
            pl: "20px",
            display: "flex",
            flexDirection: "column",
            gap: "14px",
            width: "100%",
            maxWidth: isFigmaTablte ? "297px" : "360px",
          }}
        >
          <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={"Выберите вариант"}
              value={questionContentDefault}
              maxLength={60}
              onChange={({ target }) => setContentDefault(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
            dataCy="checkbox-optional-question"
            label={"Необязательный вопрос"}
            checked={!isRequired}
            handleChange={(e) => {
              updateQuestion<QuizQuestionSelect>(questionId, (question) => {
                question.content.required = !e.target.checked;
              });
            }}
          />
          <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={"Выберите вариант"}
              value={questionContentDefault}
              onChange={({ target }) => setContentDefault(target.value)}
            />
          </Box>
        </Box>
      </Box>
    </>
  );
});

SettingDropDown.displayName = "SettingDropDown";

export default SettingDropDown;
