import { Skeleton, Typography, useMediaQuery, useTheme } from "@mui/material";
import React, { useEffect, useState } from "react";
import Box from "@mui/material/Box";
import { useCurrentQuiz } from "@root/quizes/hooks";
import { useQuizStore } from "@root/quizes/store";
import { useNavigate } from "react-router-dom";
import { PartnersBoard } from "./PartnersBoard/PartnersBoard";
import { QuizMetricType } from "@model/quizSettings";

interface IntegrationsPageProps {
  heightSidebar: number;
  mobileSidebar: boolean;
}

export const IntegrationsPage = ({
  heightSidebar,
  mobileSidebar,
}: IntegrationsPageProps) => {
  const quiz = useCurrentQuiz();
  const { editQuizId } = useQuizStore();
  const theme = useTheme();
  const navigate = useNavigate();
  const isMobile = useMediaQuery(theme.breakpoints.down(660));
  const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
  const [companyName, setCompanyName] = useState<
    keyof typeof QuizMetricType | null
  >(null);

  const [isAmoCrmModalOpen, setIsAmoCrmModalOpen] = useState<boolean>(false);
  const [isZapierModalOpen, setIsZapierModalOpen] = useState<boolean>(false);
  const [isPostbackModalOpen, setIsPostbackModalOpen] = useState<boolean>(false);

  useEffect(() => {
    if (editQuizId === null) navigate("/list");
  }, [navigate, editQuizId]);
  const heightBar = heightSidebar + 51 + 88 + 36 + 25;

  if (quiz === undefined)
    return (
      <Skeleton sx={{ width: "100vw", height: "100vh", transform: "none" }} />
    );

  const handleCloseModal = () => {
    setIsModalOpen(false);
  };
  const handleCloseAmoSRMModal = () => {
    setIsAmoCrmModalOpen(false);
  };
  const handleCloseZapierModal = () => {
    setIsZapierModalOpen(false);
  };
  const handleClosePostbackModal = () => {
    setIsPostbackModalOpen(false);
  };

  return (
    <>
      <Box
        sx={{
          width: "100%",
          padding: isMobile
            ? mobileSidebar
              ? `calc(${heightBar}px - 92px) 16px 70px 16px`
              : "67px 16px 70px 16px"
            : "25px",
          height: isMobile ? "100vh" : "calc(100vh - 80px)",
        }}
      >
        <Typography
          variant="h5"
          sx={{ marginBottom: "40px", color: theme.palette.grey3.main }}
        >
          Интеграции
        </Typography>
        <PartnersBoard
          setIsModalOpen={setIsModalOpen}
          companyName={companyName}
          setCompanyName={setCompanyName}
          isModalOpen={isModalOpen}
          handleCloseModal={handleCloseModal}
          setIsAmoCrmModalOpen={setIsAmoCrmModalOpen}
          isAmoCrmModalOpen={isAmoCrmModalOpen}
          handleCloseAmoSRMModal={handleCloseAmoSRMModal}
          setIsZapierModalOpen={setIsZapierModalOpen}
          isZapierModalOpen={isZapierModalOpen}
          handleCloseZapierModal={handleCloseZapierModal}
          setIsPostbackModalOpen={setIsPostbackModalOpen}
          isPostbackModalOpen={isPostbackModalOpen}
          handleClosePostbackModal={handleClosePostbackModal}
        />
      </Box>
    </>
  );
};
