#!/usr/bin/env python3
"""
Demo script showing TOC overlay pagination functionality.

This demonstrates:
1. Opening a navigation overlay with many chapters
2. Navigating through pages using Next/Previous buttons
3. Switching between Contents and Bookmarks tabs with pagination
"""

import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from dreader import EbookReader, TouchEvent, GestureType

def main():
    print("=" * 60)
    print("TOC Pagination Demo")
    print("=" * 60)

    # Create reader
    reader = EbookReader(page_size=(800, 1200))

    # Create a mock book with many chapters for demonstration
    from dreader.html_generator import generate_navigation_overlay

    # Generate test data: 35 chapters and 20 bookmarks
    chapters = [{"index": i, "title": f"Chapter {i+1}: The Adventure Continues"} for i in range(35)]
    bookmarks = [{"name": f"Bookmark {i+1}", "position": f"Page {i*10}"} for i in range(20)]

    print("\nTest Data:")
    print(f"  - {len(chapters)} chapters")
    print(f"  - {len(bookmarks)} bookmarks")
    print(f"  - Items per page: 10")
    print()

    # Demonstrate pagination on Contents tab
    print("Contents Tab Pagination:")
    print("-" * 60)

    # Page 1 of TOC (chapters 1-10)
    print("\n[Page 1/4] Chapters 1-10:")
    html_page1 = generate_navigation_overlay(
        chapters=chapters,
        bookmarks=bookmarks,
        active_tab="contents",
        page_size=(800, 1200),
        toc_page=0,
        toc_items_per_page=10
    )
    # Extract chapter titles for display
    for i in range(10):
        print(f"  {i+1}. {chapters[i]['title']}")
    print("  [← Prev] Page 1 of 4 [Next →]")

    # Page 2 of TOC (chapters 11-20)
    print("\n[Page 2/4] Chapters 11-20:")
    html_page2 = generate_navigation_overlay(
        chapters=chapters,
        bookmarks=bookmarks,
        active_tab="contents",
        page_size=(800, 1200),
        toc_page=1,
        toc_items_per_page=10
    )
    for i in range(10, 20):
        print(f"  {i+1}. {chapters[i]['title']}")
    print("  [← Prev] Page 2 of 4 [Next →]")

    # Page 3 of TOC (chapters 21-30)
    print("\n[Page 3/4] Chapters 21-30:")
    html_page3 = generate_navigation_overlay(
        chapters=chapters,
        bookmarks=bookmarks,
        active_tab="contents",
        page_size=(800, 1200),
        toc_page=2,
        toc_items_per_page=10
    )
    for i in range(20, 30):
        print(f"  {i+1}. {chapters[i]['title']}")
    print("  [← Prev] Page 3 of 4 [Next →]")

    # Page 4 of TOC (chapters 31-35)
    print("\n[Page 4/4] Chapters 31-35:")
    html_page4 = generate_navigation_overlay(
        chapters=chapters,
        bookmarks=bookmarks,
        active_tab="contents",
        page_size=(800, 1200),
        toc_page=3,
        toc_items_per_page=10
    )
    for i in range(30, 35):
        print(f"  {i+1}. {chapters[i]['title']}")
    print("  [← Prev] Page 4 of 4 [Next →]")

    # Demonstrate pagination on Bookmarks tab
    print("\n" + "=" * 60)
    print("Bookmarks Tab Pagination:")
    print("-" * 60)

    # Page 1 of Bookmarks (1-10)
    print("\n[Page 1/2] Bookmarks 1-10:")
    html_bm1 = generate_navigation_overlay(
        chapters=chapters,
        bookmarks=bookmarks,
        active_tab="bookmarks",
        page_size=(800, 1200),
        toc_page=0,
        bookmarks_page=0,
        toc_items_per_page=10
    )
    for i in range(10):
        print(f"  {bookmarks[i]['name']} - {bookmarks[i]['position']}")
    print("  [← Prev] Page 1 of 2 [Next →]")

    # Page 2 of Bookmarks (11-20)
    print("\n[Page 2/2] Bookmarks 11-20:")
    html_bm2 = generate_navigation_overlay(
        chapters=chapters,
        bookmarks=bookmarks,
        active_tab="bookmarks",
        page_size=(800, 1200),
        toc_page=0,
        bookmarks_page=1,
        toc_items_per_page=10
    )
    for i in range(10, 20):
        print(f"  {bookmarks[i]['name']} - {bookmarks[i]['position']}")
    print("  [← Prev] Page 2 of 2 [Next →]")

    print("\n" + "=" * 60)
    print("Pagination Controls:")
    print("-" * 60)
    print("  - Click 'Next →' to go to next page")
    print("  - Click '← Prev' to go to previous page")
    print("  - Page indicator shows: 'Page X of Y'")
    print("  - Buttons are disabled at boundaries:")
    print("    • '← Prev' disabled on page 1")
    print("    • 'Next →' disabled on last page")
    print()

    print("=" * 60)
    print("Interactive Gesture Flow:")
    print("-" * 60)
    print("1. User swipes up → Opens navigation overlay (page 1)")
    print("2. User taps 'Next →' → Shows page 2")
    print("3. User taps 'Next →' → Shows page 3")
    print("4. User taps chapter → Navigates to chapter & closes overlay")
    print("5. OR taps '← Prev' → Goes back to page 2")
    print()

    print("HTML Features Implemented:")
    print("-" * 60)
    print("✓ Pagination links: <a href='page:next'> and <a href='page:prev'>")
    print("✓ Page indicator: 'Page X of Y' text")
    print("✓ Disabled styling: opacity 0.3 + pointer-events: none")
    print("✓ Separate pagination for Contents and Bookmarks tabs")
    print("✓ Automatic page calculation based on total items")
    print("✓ Graceful handling of empty lists")
    print()

    print("=" * 60)
    print("Demo Complete!")
    print("=" * 60)

if __name__ == "__main__":
    main()
