import { Box, Typography, useTheme } from "@mui/material";
import React, { FC, lazy, Suspense } from "react";
import { ServiceButton } from "./buttons/ServiceButton";
import { ZapierButton } from "./buttons/ZapierButton";
import { PostbackButton } from "./buttons/PostbackButton";
import { YandexMetricaLogo } from "../mocks/YandexMetricaLogo";
import { VKPixelLogo } from "../mocks/VKPixelLogo";
import { QuizMetricType } from "@model/quizSettings";
import { AmoCRMLogo } from "../mocks/AmoCRMLogo";
import { useCurrentQuiz } from "@/stores/quizes/hooks";

const AnalyticsModal = lazy(() =>
  import("./AnalyticsModal/AnalyticsModal").then((module) => ({
    default: module.AnalyticsModal,
  }))
);

const AmoCRMModal = lazy(() =>
  import("../IntegrationsModal/Amo").then((module) => ({
    default: module.AmoCRMModal,
  }))
);

const ZapierModal = lazy(() =>
  import("../IntegrationsModal/Zapier").then((module) => ({
    default: module.ZapierModal,
  }))
);

const PostbackModal = lazy(() =>
  import("../IntegrationsModal/Postback").then((module) => ({
    default: module.PostbackModal,
  }))
);

type PartnersBoardProps = {
  setIsModalOpen: (value: boolean) => void;
  companyName: keyof typeof QuizMetricType | null;
  setCompanyName: (value: keyof typeof QuizMetricType) => void;
  isModalOpen: boolean;
  handleCloseModal: () => void;
  setIsAmoCrmModalOpen: (value: boolean) => void;
  isAmoCrmModalOpen: boolean;
  handleCloseAmoSRMModal: () => void;
  setIsZapierModalOpen: (value: boolean) => void;
  isZapierModalOpen: boolean;
  handleCloseZapierModal: () => void;
  setIsPostbackModalOpen: (value: boolean) => void;
  isPostbackModalOpen: boolean;
  handleClosePostbackModal: () => void;
};

export const PartnersBoard: FC<PartnersBoardProps> = ({
  setIsModalOpen,
  isModalOpen,
  handleCloseModal,
  companyName,
  setCompanyName,
  setIsAmoCrmModalOpen,
  isAmoCrmModalOpen,
  handleCloseAmoSRMModal,
  setIsZapierModalOpen,
  isZapierModalOpen,
  handleCloseZapierModal,
  setIsPostbackModalOpen,
  isPostbackModalOpen,
  handleClosePostbackModal,
}) => {
  const theme = useTheme();
  const quiz = useCurrentQuiz();

  const sectionTitleStyles = {
    textAlign: { xs: "start", sm: "start", md: "start" } as const,
    lineHeight: "1",
    marginBottom: "12px",
    marginTop: "20px",
    color: theme.palette.grey3.main,
    fontSize: "18px",
  };

  const containerStyles = {
    display: "flex",
    flexWrap: "wrap",
    justifyContent: { xs: "start", sm: "start", md: "start" },
    gap: { xs: "15px", md: "20px" },
  };

  return (
    <Box
      sx={{
        width: "100%",
        display: "flex",
        flexDirection: "column",
        justifyContent: { xs: "center", sm: "center", md: "start" },
      }}
    >
      <Box>
        <Typography
          variant="h6"
          sx={{
            ...sectionTitleStyles,
            marginTop: 0,
          }}
        >
          CRM
        </Typography>
        <Box sx={containerStyles}>
          <ServiceButton
            logo={<AmoCRMLogo />}
            setIsModalOpen={setIsAmoCrmModalOpen}
            setCompanyName={setCompanyName}
            name={"amoCRM"}
          />
        </Box>

        <Typography variant="h6" sx={sectionTitleStyles}>
          Аналитика
        </Typography>
        <Box sx={containerStyles}>
          <ServiceButton
            logo={<YandexMetricaLogo />}
            setIsModalOpen={setIsModalOpen}
            name={"yandex"}
            setCompanyName={setCompanyName}
          />
          <ServiceButton
            logo={<VKPixelLogo />}
            title={"VK Пиксель"}
            name={"vk"}
            setIsModalOpen={setIsModalOpen}
            setCompanyName={setCompanyName}
          />
        </Box>

        {/* <Typography variant="h6" sx={sectionTitleStyles}>
          Автоматизация
        </Typography>
        <Box sx={containerStyles}>
          <ZapierButton
            setIsModalOpen={setIsZapierModalOpen}
            setCompanyName={setCompanyName}
          />
          <PostbackButton
            setIsModalOpen={setIsPostbackModalOpen}
            setCompanyName={setCompanyName}
          />
        </Box> */}
      </Box>

      {companyName && (
        <Suspense>
          <AnalyticsModal
            isModalOpen={isModalOpen}
            handleCloseModal={handleCloseModal}
            companyName={companyName}
          />
        </Suspense>
      )}
      {companyName && isAmoCrmModalOpen && (
        <Suspense>
          <AmoCRMModal
            isModalOpen={isAmoCrmModalOpen}
            handleCloseModal={handleCloseAmoSRMModal}
            companyName={companyName}
            quiz={quiz!}
          />
        </Suspense>
      )}
      {companyName && isZapierModalOpen && (
        <Suspense>
          <ZapierModal
            isModalOpen={isZapierModalOpen}
            handleCloseModal={handleCloseZapierModal}
            companyName={companyName}
            quiz={quiz!}
          />
        </Suspense>
      )}
      {companyName && isPostbackModalOpen && (
        <Suspense>
          <PostbackModal
            isModalOpen={isPostbackModalOpen}
            handleCloseModal={handleClosePostbackModal}
            companyName={companyName}
            quiz={quiz!}
          />
        </Suspense>
      )}
    </Box>
  );
};
