import { useState } from "react";
import { useParams } from "react-router-dom";
import {
  Box,
  Link,
  Typography,
  useMediaQuery,
  useTheme,
  Popover,
} from "@mui/material";
import { EnterIcon } from "../../../assets/icons/questionsPage/enterIcon";
import ButtonsOptions from "../ButtonsOptions";
import SwitchEmoji from "./switchEmoji";
import { AnswerDraggableList } from "../AnswerDraggableList";
import { EmojiPicker } from "@ui_kit/EmojiPicker";
import { EmojiIcons } from "@icons/EmojiIocns";
import AddEmoji from "@icons/questionsPage/addEmoji";
import PlusImage from "@icons/questionsPage/plus";

import { questionStore, updateQuestionsList } from "@root/questions";

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

interface Props {
  totalIndex: number;
}

export default function Emoji({ totalIndex }: Props) {
  const [switchState, setSwitchState] = useState<string>("setting");
  const [open, setOpen] = useState<boolean>(false);
  const [anchorElement, setAnchorElement] = useState<HTMLDivElement | null>(
    null
  );
  const [currentIndex, setCurrentIndex] = useState<number>(0);
  const { listQuestions } = questionStore();
  const quizId = Number(useParams().quizId);
  const theme = useTheme();
  const question = listQuestions[quizId][totalIndex] as QuizQuestionEmoji;
  const isMobile = useMediaQuery(theme.breakpoints.down(790));
  const isTablet = useMediaQuery(theme.breakpoints.down(1000));

  const SSHC = (data: string) => {
    setSwitchState(data);
  };

  return (
    <>
      <Box sx={{ padding: "20px" }}>
        <AnswerDraggableList
          variants={question.content.variants}
          totalIndex={totalIndex}
          additionalContent={(variant, index) => (
            <>
              {!isTablet && (
                <Box sx={{ cursor: "pointer" }}>
                  <Box
                    onClick={({ currentTarget }) => {
                      setAnchorElement(currentTarget);
                      setCurrentIndex(index);
                      setOpen(true);
                    }}
                  >
                    <Box
                      sx={{
                        display: "flex",
                        justifyContent: "center",
                        alignItems: "center",
                        gap: "5px",
                      }}
                    >
                      {variant.extendedText ? (
                        <Box
                          sx={{
                            height: "40px",
                            width: "60px",
                            display: "flex",
                            alignItems: "center",
                            justifyContent: "space-between",
                            background: "#EEE4FC",
                            borderRadius: "3px",
                            marginRight: "15px",
                          }}
                        >
                          <Box
                            sx={{
                              width: "100%",
                              display: "flex",
                              justifyContent: "center",
                            }}
                          >
                            {variant.extendedText}
                          </Box>
                          <Box>
                            <PlusImage />
                          </Box>
                        </Box>
                      ) : (
                        <AddEmoji />
                      )}
                    </Box>
                  </Box>
                </Box>
              )}
            </>
          )}
          additionalMobile={(variant, index) => (
            <>
              {isTablet && (
                <Box
                  onClick={({ currentTarget }) => {
                    setAnchorElement(currentTarget);
                    setCurrentIndex(index);
                    setOpen(true);
                  }}
                  sx={{
                    display: "flex",
                    alignItems: "center",
                    m: "8px",
                    position: "relative",
                  }}
                >
                  <Box
                    sx={{
                      width: "100%",
                      background: "#EEE4FC",
                      height: "40px",
                    }}
                  />
                  {variant.extendedText ? (
                    <Box
                      sx={{
                        position: "absolute",
                        color: "#7E2AEA",
                        fontSize: "20px",
                        left: "45%",
                        right: "55%",
                      }}
                    >
                      {variant.extendedText}
                    </Box>
                  ) : (
                    <EmojiIcons
                      style={{
                        position: "absolute",
                        color: "#7E2AEA",
                        fontSize: "20px",
                        left: "45%",
                        right: "55%",
                      }}
                    />
                  )}
                  <Box
                    sx={{
                      display: "flex",
                      justifyContent: "center",
                      alignItems: "center",
                      width: "20px",
                      background: "#EEE4FC",
                      height: "40px",
                      color: "white",
                      backgroundColor: "#7E2AEA",
                    }}
                  >
                    +
                  </Box>
                </Box>
              )}
            </>
          )}
        />
        <Popover
          open={open}
          anchorEl={anchorElement}
          onClick={(event) => event.stopPropagation()}
          onClose={() => setOpen(false)}
          anchorOrigin={{
            vertical: "bottom",
            horizontal: "right",
          }}
          sx={{
            ".MuiPaper-root.MuiPaper-rounded": {
              borderRadius: "10px",
            },
          }}
        >
          <EmojiPicker
            onEmojiSelect={({ native }) => {
              setOpen(false);
              const cloneVariants = [...question.content.variants];

              cloneVariants[currentIndex] = {
                ...cloneVariants[currentIndex],
                extendedText: native,
              };

              updateQuestionsList<QuizQuestionEmoji>(quizId, totalIndex, {
                content: { ...question.content, variants: cloneVariants },
              });
            }}
          />
        </Popover>
        <Box
          sx={{
            display: "flex",
            alignItems: "center",
            gap: "10px",
            marginBottom: isMobile ? "17px" : "20px",
          }}
        >
          <Link
            component="button"
            variant="body2"
            sx={{ color: theme.palette.brightPurple.main }}
            onClick={() => {
              const answerNew = question.content.variants.slice();
              answerNew.push({ answer: "", extendedText: "", hints: "" });

              updateQuestionsList<QuizQuestionEmoji>(quizId, totalIndex, {
                content: { ...question.content, variants: answerNew },
              });
            }}
          >
            Добавьте ответ
          </Link>
          {!isTablet && (
            <>
              <Typography
                sx={{
                  fontWeight: 400,
                  lineHeight: "21.33px",
                  color: theme.palette.grey2.main,
                  fontSize: "16px",
                }}
              >
                или нажмите Enter
              </Typography>
              <EnterIcon
                style={{
                  color: "#7E2AEA",
                  fontSize: "24px",
                  marginLeft: "6px",
                }}
              />
            </>
          )}
        </Box>
      </Box>
      <ButtonsOptions
        switchState={switchState}
        SSHC={SSHC}
        totalIndex={totalIndex}
      />
      <SwitchEmoji switchState={switchState} totalIndex={totalIndex} />
    </>
  );
}
