import { useState } from "react";
import { useParams } from "react-router-dom";
import {
  Box,
  Link,
  Typography,
  Button,
  useTheme,
  useMediaQuery,
} from "@mui/material";

import ButtonsOptions from "../ButtonsOptions";
import { AnswerDraggableList } from "../AnswerDraggableList";
import { CropModal } from "@ui_kit/Modal/CropModal";
import { UploadImageModal } from "../UploadImage/UploadImageModal";
import { questionStore, updateQuestionsList } from "@root/questions";

import EnterIcon from "../../../assets/icons/questionsPage/enterIcon";
import AddImage from "../../../assets/icons/questionsPage/addImage";
import Image from "../../../assets/icons/questionsPage/image";
import SwitchAnswerOptionsPict from "./switchOptionsPict";
import PlusImage from "@icons/questionsPage/plus";

interface Props {
  totalIndex: number;
}

export default function OptionsPicture({ totalIndex }: Props) {
  const [open, setOpen] = useState(false);
  const [opened, setOpened] = useState<boolean>(false);
  const [currentIndex, setCurrentIndex] = useState<number>(0);
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down(790));
  const quizId = Number(useParams().quizId);
  const [switchState, setSwitchState] = useState("setting");
  const { listQuestions } = questionStore();

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

  const uploadImage = (files: FileList | null) => {
    if (files?.length) {
      const [file] = Array.from(files);

      const clonContent = { ...listQuestions[quizId][totalIndex].content };
      clonContent.variants[currentIndex].image = URL.createObjectURL(file);
      updateQuestionsList(quizId, totalIndex, { content: clonContent });

      setOpen(false);
      setOpened(true);
    }
  };

  const addNewAnswer = () => {
    const answerNew =
      listQuestions[quizId][totalIndex].content.variants.slice();
    answerNew.push({ answer: "", hints: "", emoji: "", image: "" });

    updateQuestionsList(quizId, totalIndex, {
      content: {
        ...listQuestions[quizId][totalIndex].content,
        variants: answerNew,
      },
    });
  };

  return (
    <>
      <Box sx={{ padding: "20px" }}>
        <AnswerDraggableList
          variants={listQuestions[quizId][totalIndex].content.variants}
          totalIndex={totalIndex}
          additionalContent={(variant, index) => (
            <>
              {!isMobile && (
                <Box
                  sx={{ cursor: "pointer" }}
                  onClick={() => {
                    setCurrentIndex(index);
                    setOpen(true);
                  }}
                >
                  {variant.image ? (
                    <Box
                      sx={{
                        overflow: "hidden",
                        width: "60px",
                        display: "flex",
                        alignItems: "center",
                        background: "#EEE4FC",
                        borderRadius: "3px",
                        margin: "0 10px",
                        height: "40px",
                      }}
                    >
                      <Box sx={{ display: "flex", width: "40px" }}>
                        <img
                          src={variant.image}
                          alt=""
                          style={{ width: "100%" }}
                        />
                      </Box>
                      <PlusImage />
                    </Box>
                  ) : (
                    <Button component="label" sx={{ padding: "0px" }}>
                      <AddImage
                        sx={{ height: "40px", width: "60px", margin: "0 10px" }}
                      />
                    </Button>
                  )}
                </Box>
              )}
            </>
          )}
          additionalMobile={(variant, index) => (
            <>
              {isMobile && (
                <Box
                  onClick={() => {
                    setCurrentIndex(index);
                    setOpen(true);
                  }}
                  sx={{
                    overflow: "hidden",
                    display: "flex",
                    alignItems: "center",
                    m: "8px",
                    position: "relative",
                    borderRadius: "3px",
                  }}
                >
                  <Box
                    sx={{
                      width: "100%",
                      background: "#EEE4FC",
                      height: "40px",
                      display: "flex",
                      alignItems: "center",
                      justifyContent: "center",
                    }}
                  >
                    {variant.image ? (
                      <Box
                        sx={{
                          overflow: "hidden",
                          width: "40px",
                          display: "flex",
                          alignItems: "center",
                          background: "#EEE4FC",
                          height: "30px",
                          borderRadius: "3px",
                        }}
                      >
                        <img
                          src={variant.image}
                          alt=""
                          style={{ width: "100%" }}
                        />
                      </Box>
                    ) : (
                      <Button component="label" sx={{ padding: "0px" }}>
                        <Image
                          sx={{
                            height: "40px",
                            width: "60px",
                            margin: "0 10px",
                          }}
                        />
                      </Button>
                    )}
                  </Box>
                  <Box
                    sx={{
                      display: "flex",
                      justifyContent: "center",
                      alignItems: "center",
                      width: "20px",
                      background: "#EEE4FC",
                      height: "40px",
                      color: "white",
                      backgroundColor: "#7E2AEA",
                    }}
                  >
                    +
                  </Box>
                </Box>
              )}
            </>
          )}
        />
        <UploadImageModal
          open={open}
          onClose={() => setOpen(false)}
          imgHC={uploadImage}
        />
        <CropModal
          opened={opened}
          onClose={() => setOpened(false)}
          picture={
            listQuestions[quizId][totalIndex].content.variants[currentIndex]
              .image
          }
        />
        <Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
          <Link
            component="button"
            variant="body2"
            sx={{ color: theme.palette.brightPurple.main }}
            onClick={addNewAnswer}
          >
            Добавьте ответ
          </Link>
          {isMobile ? null : (
            <>
              <Typography
                sx={{
                  fontWeight: 400,
                  fontSize: "18px",
                  lineHeight: "21.33px",
                  color: theme.palette.grey2.main,
                }}
              >
                или нажмите Enter
              </Typography>
              <EnterIcon />
            </>
          )}
        </Box>
      </Box>
      <ButtonsOptions
        switchState={switchState}
        SSHC={SSHC}
        totalIndex={totalIndex}
      />
      <SwitchAnswerOptionsPict
        switchState={switchState}
        totalIndex={totalIndex}
      />
    </>
  );
}
