import { QuizQuestionResult } from "@frontend/squzanswerer";
import { Box, Button, Modal, Typography } from "@mui/material";
import { useQuestionsStore } from "@root/questions/store";
import { useCurrentQuiz } from "@root/quizes/hooks";
import { useEffect, useRef, useState } from "react";
import { EmailSettingsCard } from "./cards/EmailSettingsCard";
import { ResultCard, checkEmptyData } from "./cards/ResultCard";
import { WhenCard } from "./cards/WhenCard";

export const ResultSettings = () => {
  const quiz = useCurrentQuiz();
  const results = useQuestionsStore().questions.filter((q): q is QuizQuestionResult => q.type === "result");
  const [quizExpand, setQuizExpand] = useState(true);
  const [resultContract, setResultContract] = useState(true);
  const isReadyToLeaveRef = useRef(true);

  useEffect(
    function calcIsReadyToLeave() {
      let isReadyToLeave = true;
      results.forEach((result) => {
        if (checkEmptyData({ resultData: result })) {
          isReadyToLeave = false;
        }
      });
      isReadyToLeaveRef.current = isReadyToLeave;
    },
    [results]
  );

  const cnsl = results.filter((q) => q.content.usage);

  return (
    <Box sx={{ maxWidth: "796px" }}>
      <Box
        sx={{
          display: "flex",
          alignItems: "center",
          margin: "60px 0 40px 0",
        }}
      >
        <Typography variant="h5">Настройки результатов</Typography>
        <Button
          disableRipple
          sx={{
            ml: "auto",
            width: "73px",
            height: "19px",
            color: "#7E2AEA",
            textDecoration: "underline",
            fontSize: "16px",
            "&:hover": {
              background: "none",
              textDecoration: "underline",
            },
          }}
          variant="text"
          onClick={() => setQuizExpand(!quizExpand)}
        >
          Свернуть
        </Button>
      </Box>

      <WhenCard quizExpand={quizExpand} />
      {quiz?.config.resultInfo.when === "email" && <EmailSettingsCard quizExpand={quizExpand} />}

      <Box sx={{ display: "flex", alignItems: "center", mb: "15px", mt: "15px" }}>
        <Typography
          variant="p1"
          sx={{ color: "#4D4D4D", fontSize: "14px" }}
        >
          Создайте результат
        </Typography>
        <Button
          disableRipple
          sx={{
            ml: "auto",
            height: "19px",
            color: "#7E2AEA",
            textDecoration: "underline",
            fontSize: "16px",

            "&:hover": {
              background: "none",
              textDecoration: "underline",
            },
          }}
          variant="text"
          onClick={() => setResultContract(!resultContract)}
        >
          Развернуть все
        </Button>
      </Box>

      {cnsl.map((resultQuestion) => (
        <ResultCard
          resultContract={resultContract}
          resultData={resultQuestion}
          key={resultQuestion.id}
        />
      ))}
      <Modal
        open={false}
        aria-labelledby="modal-modal-title"
        aria-describedby="modal-modal-description"
      >
        <></>
      </Modal>
    </Box>
  );
};
