import { ExportButton } from "./ExportButton";
import { Button, Tooltip } from "antd";
import { useTable } from "../../stores/useTable";
import { FullscreenExitOutlined, FullscreenOutlined } from "@ant-design/icons";
import { useEffect, useState } from "react";
import { TableSettings } from "./TableSettings";

const ToggleFullScreenButton = () => {
  const {
    tableState: { fullScreen },
    toggleFullScreen,
  } = useTable();
  const [hover, setHover] = useState(false);

  useEffect(() => {
    const timer = setTimeout(() => setHover(false), 1500);

    return () => clearTimeout(timer);
  }, [hover]);

  const handleMouseEnter = () => {
    setHover(true);
  };
  const handleMouseLeave = () => {
    setHover(false);
  };

  const handleClick = (e) => {
    e.stopPropagation();
    toggleFullScreen();
  };

  return (
    <Tooltip
      title={fullScreen ? "Collapse" : "Expand to full screen"}
      placement={"topRight"}
      open={hover}
    >
      <Button
        onClick={handleClick}
        onMouseEnter={handleMouseEnter}
        onMouseLeave={handleMouseLeave}
      >
        {fullScreen ? <FullscreenExitOutlined /> : <FullscreenOutlined />}
      </Button>
    </Tooltip>
  );
};

export const HeaderWrapper = ({
  leftColumn,
  rightColumn,
  exportProvider,
  classes,
  orderColumns
}) => {
  return (
    <div className={"flex items-center w-full justify-between"}>
      <div className={classes?.leftColumn}>
        <span className="py-[5px]">Attribute table</span>
        {leftColumn}
      </div>
      <div className={classes?.rightColumn}>
        {rightColumn}
        <div className="flex items-center gap-x-1">
          <TableSettings orderColumns={orderColumns} />
          {exportProvider && <ExportButton provider={exportProvider} />}
          <ToggleFullScreenButton />
        </div>
      </div>
    </div>
  );
};
