/**
 * ReaderToolbar — fixed bottom toolbar for the reading view.
 */

import { useTranslation } from "react-i18n-lite";
import type { ReadingProgress } from "../../types/reader";
import { ReaderTocButton } from "./ReaderTocButton";

interface ReaderToolbarProps {
  bookTitle: string;
  chapterTitle: string;
  progress: ReadingProgress | null;
  onBack?: () => void;
  onToggleToc: () => void;
  onToggleSettings: () => void;
  onToggleMarkers?: () => void;
  onBookmarkPage?: () => void;
  /** PDF: filled bookmark when the current page is already bookmarked */
  isCurrentPageBookmarked?: boolean;
}

export default function ReaderToolbar({
  bookTitle,
  chapterTitle,
  progress,
  onBack,
  onToggleToc,
  onToggleSettings,
  onToggleMarkers,
  onBookmarkPage,
  isCurrentPageBookmarked = false,
}: ReaderToolbarProps) {
  const { t } = useTranslation();
  const percentage = progress?.percentage ?? 0;

  return (
    <>
      <header className="reader-top-bar">
        {onBack ? (
          <button
            type="button"
            className="reader-bar-btn"
            onClick={onBack}
            aria-label={t("reader.backToLibraryAria")}
          >
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="M20 12H4M10 18l-6-6 6-6" />
            </svg>
          </button>
        ) : (
          <ReaderTocButton onClick={onToggleToc} />
        )}
        <div className="reader-bar-title">
          <span className="reader-bar-book">{bookTitle}</span>
          <span className="reader-bar-chapter">{chapterTitle}</span>
        </div>
        <div className="reader-bar-actions">
          {onBack && <ReaderTocButton onClick={onToggleToc} />}
          {onBookmarkPage && (
            <button
              type="button"
              className="reader-bar-btn"
              onClick={onBookmarkPage}
              aria-label={t("reader.bookmarkPageAria")}
              aria-pressed={isCurrentPageBookmarked}
              title={t("reader.bookmarkPage")}
            >
              {isCurrentPageBookmarked ? (
                <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z" />
                </svg>
              ) : (
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z" />
                </svg>
              )}
            </button>
          )}
          {onToggleMarkers && (
            <button
              type="button"
              className="reader-bar-btn"
              onClick={onToggleMarkers}
              aria-label={t("annotations.inBookPanel")}
            >
              {onBookmarkPage ? (
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01" />
                </svg>
              ) : (
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z" />
                </svg>
              )}
            </button>
          )}
        <button
          type="button"
          className="reader-bar-btn"
          onClick={onToggleSettings}
          aria-label={t("reader.settingsAria")}
        >
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <circle cx="12" cy="12" r="3" />
            <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z" />
          </svg>
        </button>
        </div>
      </header>

      <div className="reader-progress-bar">
        <div
          className="reader-progress-fill"
          style={{ width: `${Math.min(percentage, 100)}%` }}
        />
      </div>
    </>
  );
}
