import { Close, UnfoldLess, UnfoldMore } from "@mui/icons-material";
import { Box, IconButton, Modal, Tooltip, Typography, useTheme } from "@mui/material";
import { FC, ReactNode } from "react";
import { defaultSettings } from "../../lib/settings";

interface FloatingDialog {
  actionButtons?: ReactNode;
  body: ReactNode;
  bottomBar?: ReactNode;
  close: () => void;
  maximisedState: boolean;
  openButton: ReactNode;
  openState: boolean;
  setMaximisedState: (state: boolean) => void;
  title: string;
}

export const FloatingDialog: FC<FloatingDialog> = ({
  actionButtons,
  body,
  bottomBar,
  close,
  maximisedState,
  openButton,
  openState,
  setMaximisedState,
  title,
}) => {
  // contexts
  const theme = useTheme();

  return (
    <>
      {openButton}

      <Modal
        onClose={close}
        open={openState}
      >
        <Box
          sx={{
            backdropFilter: `blur(${defaultSettings.style.blur_radius}px)`,
            backgroundColor: theme.palette.background.paper,
            borderRadius: maximisedState ? "0px" : defaultSettings.style.radius + "px",
            display: "flex",
            flexDirection: "column",
            height: maximisedState ? "100%" : defaultSettings.style.window_height + "%",
            left: "50%",
            maxHeight: maximisedState ? "100vh" : "96vh",
            maxWidth: maximisedState ? "100vw" : "96vw",
            p: 2,
            position: "absolute" as const,
            top: "50%",
            transform: "translate(-50%, -50%)",
            transition: "all ease-in-out",
            transitionDuration: defaultSettings.style.transition_duration + "ms",
            width: maximisedState ? "100vw" : defaultSettings.style.window_width + "px",
          }}
        >
          <Box
            sx={{
              alignItems: "center",
              display: "flex",
              px: 1,
            }}
          >
            <Typography variant='h6'>{title}</Typography>
            <Box sx={{ flexGrow: 1 }} />

            {actionButtons}

            <Tooltip title={maximisedState ? "Minimise" : "Maximise"}>
              <IconButton
                onClick={() => {
                  setMaximisedState(!maximisedState);
                }}
                sx={{
                  mr: 1,
                }}
              >
                {maximisedState ? <UnfoldLess /> : <UnfoldMore />}
              </IconButton>
            </Tooltip>
            <Tooltip title='Close'>
              <IconButton onClick={close}>
                <Close />
              </IconButton>
            </Tooltip>
          </Box>
          {body}
          {bottomBar}
        </Box>
      </Modal>
    </>
  );
};
