import { useState, ChangeEvent } from "react";
import {
  Box,
  FormControl,
  FormControlLabel,
  FormLabel,
  Radio,
  RadioGroup,
  Tooltip,
  Typography,
} from "@mui/material";

import InfoIcon from "@icons/InfoIcon";

import type { QuizQuestionEmoji } from "model/questionTypes/emoji";

interface Props {
  question: QuizQuestionEmoji;
}

export default function Emoji({ question }: Props) {
  const [value, setValue] = useState<string | null>(null);

  const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
    setValue((event.target as HTMLInputElement).value);
  };

  return (
    <FormControl fullWidth>
      <FormLabel id="quiz-question-radio-group" data-cy="question-title">{question.title}</FormLabel>
      <RadioGroup
        aria-labelledby="quiz-question-radio-group"
        value={value}
        onChange={handleChange}
      >
        {question.content.variants
          .filter(({ answer }) => answer)
          .map((variant, index) => (
            <FormControlLabel
              key={index}
              value={variant.answer}
              control={
                <Radio
                  inputProps={{
                    "data-cy": "variant-radio",
                  }}
                />
              }
              label={
                <Box sx={{ display: "flex", alignItems: "center", gap: 2 }} data-cy="variant-answer">
                  <Typography>{`${variant.extendedText} ${variant.answer}`}</Typography>
                  {variant.hints && (
                    <Tooltip title="Подсказка" placement="right">
                      <Box>
                        <InfoIcon />
                      </Box>
                    </Tooltip>
                  )}
                </Box>
              }
            />
          ))}
      </RadioGroup>
    </FormControl>
  );
}
