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

import { useQuizViewStore, updateAnswer } from "@root/quizView";

import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";

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

type ImagesProps = {
  stepNumber: number;
  question: QuizQuestionImages;
};

export const Images = ({ stepNumber, question }: ImagesProps) => {
  const { answers } = useQuizViewStore();
  const theme = useTheme();
  const { answer } = answers.find(({ step }) => step === stepNumber) ?? {};
  const isTablet = useMediaQuery(theme.breakpoints.down(1000));
  const isMobile = useMediaQuery(theme.breakpoints.down(500));

  useEffect(() => {
    if (!answer) {
      updateAnswer(stepNumber, question.content.variants[0].id);
    }
  }, []);

  return (
    <Box>
      <Typography variant="h5">{question.title}</Typography>
      <RadioGroup
        name={question.id}
        value={question.content.variants.findIndex(({ id }) => answer === id)}
        onChange={({ target }) =>
          updateAnswer(
            stepNumber,
            question.content.variants[Number(target.value)].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%",
          }}
        >
          {question.content.variants.map(
            ({ id, answer, extendedText }, index) => (
              <Box
                key={index}
                sx={{
                  borderRadius: "5px",
                  border: `1px solid ${theme.palette.grey2.main}`,
                }}
              >
                <Box
                  sx={{ display: "flex", alignItems: "center", gap: "10px" }}
                >
                  <Box sx={{ width: "100%", height: "300px" }}>
                    {extendedText && (
                      <img
                        src={extendedText}
                        alt=""
                        style={{
                          display: "block",
                          width: "100%",
                          height: "100%",
                          objectFit: "cover",
                        }}
                      />
                    )}
                  </Box>
                </Box>
                <FormControlLabel
                  key={id}
                  sx={{
                    display: "block",
                    textAlign: "center",
                    color: theme.palette.grey2.main,
                    marginTop: "10px",
                  }}
                  value={index}
                  control={
                    <Radio checkedIcon={<RadioCheck />} icon={<RadioIcon />} />
                  }
                  label={answer}
                />
              </Box>
            )
          )}
        </Box>
      </RadioGroup>
    </Box>
  );
};
