import {
  Box,
  Typography,
  RadioGroup,
  FormControlLabel,
  Radio,
  useTheme,
  useMediaQuery,
} from "@mui/material";

import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView";
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";

import type { QuizQuestionImages } from "../../../model/questionTypes/images";

type ImagesProps = {
  currentQuestion: QuizQuestionImages;
};

export const Images = ({ currentQuestion }: ImagesProps) => {
  const { answers } = useQuizViewStore();
  const theme = useTheme();
  const { answer } =
    answers.find(
      ({ questionId }) => questionId === currentQuestion.content.id
    ) ?? {};
  const isTablet = useMediaQuery(theme.breakpoints.down(1000));
  const isMobile = useMediaQuery(theme.breakpoints.down(500));

  return (
    <Box>
      <Typography variant="h5">{currentQuestion.title}</Typography>
      <RadioGroup
        name={currentQuestion.id}
        value={currentQuestion.content.variants.findIndex(
          ({ id }) => answer === id
        )}
        sx={{
          display: "flex",
          flexWrap: "wrap",
          flexDirection: "row",
          justifyContent: "space-between",
          marginTop: "20px",
        }}
      >
        <Box
          sx={{
            display: "grid",
            gap: "15px",
            gridTemplateColumns: isTablet
              ? isMobile
                ? "repeat(1, 1fr)"
                : "repeat(2, 1fr)"
              : "repeat(3, 1fr)",
            width: "100%",
          }}
        >
          {currentQuestion.content.variants.map((variant, index) => (
            <Box
              key={index}
              sx={{
                cursor: "pointer",
                borderRadius: "5px",
                border: `1px solid ${theme.palette.grey2.main}`,
              }}
              onClick={(event) => {
                event.preventDefault();

                updateAnswer(
                  currentQuestion.content.id,
                  currentQuestion.content.variants[index].id
                );

                if (answer === currentQuestion.content.variants[index].id) {
                  deleteAnswer(currentQuestion.content.id);
                }
              }}
            >
              <Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
                <Box sx={{ width: "100%", height: "300px" }}>
                  {variant.extendedText && (
                    <img
                      src={variant.extendedText}
                      alt=""
                      style={{
                        display: "block",
                        width: "100%",
                        height: "100%",
                        objectFit: "cover",
                      }}
                    />
                  )}
                </Box>
              </Box>
              <FormControlLabel
                key={variant.id}
                sx={{
                  display: "block",
                  textAlign: "center",
                  color: theme.palette.grey2.main,
                  marginTop: "10px",
                }}
                value={index}
                control={
                  <Radio checkedIcon={<RadioCheck />} icon={<RadioIcon />} />
                }
                label={variant.answer}
              />
            </Box>
          ))}
        </Box>
      </RadioGroup>
    </Box>
  );
};
