"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import type { NormalizedBook } from "@/lib/providers/types";
import { PhotoswipeReader } from "./PhotoswipeReader";
import { Button } from "@/components/ui/button";

interface ClientBookReaderProps {
  book: NormalizedBook;
  pages: number[];
}

export function ClientBookReader({ book, pages }: ClientBookReaderProps) {
  const router = useRouter();
  const [isReading, setIsReading] = useState(false);

  const handleStartReading = () => {
    setIsReading(true);
  };

  const handleCloseReader = () => {
    setIsReading(false);
    //Fetch une nouvelle route pour rafraichir les différents caches
    router.back();
  };

  if (isReading) {
    return <PhotoswipeReader book={book} pages={pages} onClose={handleCloseReader} />;
  }

  return (
    <Button onClick={handleStartReading} size="lg" className="w-full md:w-auto">
      Commencer la lecture
    </Button>
  );
}
