import { useGetOverviewSnapshot } from "../../hooks/useGetOverviewSnapshot";
import NavigationArrow from "../UI/NavigationArrow";

import Loading from "../UI/Loading";
import ErrorState from "../UI/ErrorState";
type SnapshotContainerProps = {
  side: string;
  settingsPage?: boolean;
  zoomLevel?: number;
  onZoomLevelChange?: (level: number) => void;
};

export const SnapshotContainer = ({ side, settingsPage, zoomLevel, onZoomLevelChange }: SnapshotContainerProps) => {
  const { canvasRef, isError, isPending } = useGetOverviewSnapshot(side);

  const handleZoomClick = () => {
    const baseLevel = zoomLevel ?? 1;
    const newLevel = baseLevel >= 4 ? 1 : baseLevel * 2;

    if (onZoomLevelChange) onZoomLevelChange(newLevel);

    if (!zoomLevel) return;
  };

  return (
    <div className="flex flex-col md:flex-row">
      <NavigationArrow side={side} settingsPage={settingsPage} />
      <div className="w-full bg-[#253445] rounded-md overflow-hidden md:h-[500px] lg:h-[70vh]">
        {isError && <ErrorState />}
        {isPending && (
          <div className="absolute inset-0 grid place-items-center">
            <Loading message="Camera Preview" />
          </div>
        )}
        <canvas onClick={handleZoomClick} ref={canvasRef} className="block w-full h-full z-20" />
      </div>
    </div>
  );
};
