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

import gag from "./gag.png"

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

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

import type { QuizQuestionVarImg } from "../../../model/questionTypes/varimg";

type VarimgProps = {
  currentQuestion: QuizQuestionVarImg;
};

export const Varimg = ({ currentQuestion }: VarimgProps) => {
  const { answers } = useQuizViewStore();
  const theme = useTheme();
  const { answer } =
    answers.find(
      ({ questionId }) => questionId === currentQuestion.content.id
    ) ?? {};
  const variant = currentQuestion.content.variants.find(
    ({ id }) => answer === id
  );

  return (
    <Box>
      <Typography variant="h5">{currentQuestion.title}</Typography>
      <Box sx={{ display: "flex", marginTop: "20px" }}>
        <RadioGroup
          name={currentQuestion.id}
          value={currentQuestion.content.variants.findIndex(
            ({ id }) => answer === id
          )}
          sx={{
            display: "flex",
            flexWrap: "wrap",
            flexDirection: "row",
            justifyContent: "space-between",
            flexBasis: "100%",
          }}
        >
          <Box sx={{ display: "flex", flexDirection: "column", width: "100%" }}>
            {currentQuestion.content.variants.map((variant, index) => (
              <FormControlLabel
                key={variant.id}
                sx={{
                  marginBottom: "15px",
                  borderRadius: "5px",
                  padding: "15px",
                  color: "#4D4D4D",
                  border: `1px solid #9A9AAF`,
                  display: "flex",
                }}
                value={index}
                onClick={(event) => {
                  event.preventDefault();

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

                  if (answer === currentQuestion.content.variants[index].id) {
                    deleteAnswer(currentQuestion.content.id);
                  }
                }}
                control={
                  <Radio checkedIcon={<RadioCheck />} icon={<RadioIcon />} />
                }
                label={variant.answer}
              />
            ))}
          </Box>
        </RadioGroup>
        {/* {(variant?.extendedText || currentQuestion.content.back) && ( */}
          <Box
            sx={{
              maxWidth: "450px",
              width: "100%",
              height: "450px",
              border: "1px solid #9A9AAF",
              borderRadius: "12px",
              overflow: "hidden",
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              backgroundColor: "#9A9AAF12",
              color: "#9A9AAF"
            }}
          >
            {
              answer ?
              <img
              src={
                variant?.extendedText || gag
              }
              style={{ width: "100%", height: "100%", objectFit: "cover" }}
              alt=""
            />
              :
              (variant?.extendedText || "Выберите вариант ответа слева")
            }
            
          </Box>
         {/* )} */}
      </Box>
    </Box>
  );
};
