import { Box, useMediaQuery, useTheme } from "@mui/material";
import { deleteTimeoutedQuestions } from "@utils/deleteTimeoutedQuestions";
import { useCallback } from "react";
import { BranchingMap } from "./BranchingMap";
import { DraggableList } from "./DraggableList";
import { SwitchBranchingPanel } from "./SwitchBranchingPanel";

interface Props {
  openBranchingPage: boolean;
  setOpenBranchingPage: (a: boolean) => void;
  widthMain: number;
}

export const QuestionSwitchWindowTool = ({
  openBranchingPage,
  setOpenBranchingPage,
  widthMain,
}: Props) => {
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down(600));
  const isTablet = useMediaQuery(theme.breakpoints.down(1000));

  const openBranchingPageHC = useCallback(() => {
    if (!openBranchingPage) {
      deleteTimeoutedQuestions();
    }
    setOpenBranchingPage(!openBranchingPage);
  }, [openBranchingPage, setOpenBranchingPage]);

  return (
    <Box
      sx={{
        display: "flex",
        gap: "20px",
        flexWrap: "wrap",
        marginBottom: isMobile ? "20px" : undefined,
      }}
    >
      <Box sx={{ width: isTablet ? "100%" : "796px" }}>
        {openBranchingPage ? (
          <BranchingMap />
        ) : (
          <DraggableList
            openBranchingPage={openBranchingPage}
            setOpenBranchingPage={openBranchingPageHC}
          />
        )}
      </Box>
      {openBranchingPage && (
        <SwitchBranchingPanel
          openBranchingPage={openBranchingPage}
          setOpenBranchingPage={openBranchingPageHC}
          widthMain={widthMain}
        />
      )}
    </Box>
  );
};
