#!/usr/bin/env python3
"""
Test the debug overlay visualization feature.
This creates an overlay with debug bounding boxes and saves it as an image.
"""

import sys
import os
from pathlib import Path

# Enable debug mode
os.environ['DREADER_DEBUG_OVERLAY'] = '1'

sys.path.insert(0, str(Path(__file__).parent))

from dreader.application import EbookReader
from dreader.overlays.settings import SettingsOverlay
from dreader.overlays.navigation import NavigationOverlay

def test_settings_overlay_debug():
    """Create settings overlay with debug visualization."""
    print("="*70)
    print("Creating Settings Overlay with Debug Bounding Boxes")
    print("="*70)

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

    # Load a test book
    test_book = Path(__file__).parent / "tests" / "data" / "library-epub" / "pg11-images-3.epub"
    if not test_book.exists():
        print(f"Test book not found: {test_book}")
        return

    reader.load_epub(str(test_book))

    # Create settings overlay
    settings_overlay = SettingsOverlay(reader)
    base_page = reader.get_current_page()

    # Open overlay (with debug mode, this will draw bounding boxes)
    overlay_image = settings_overlay.open(
        base_page,
        font_scale=1.0,
        line_spacing=5,
        inter_block_spacing=15,
        word_spacing=0
    )

    # Save the result
    output_path = Path(__file__).parent / "settings_overlay_debug.png"
    overlay_image.save(output_path)
    print(f"\nSettings overlay with debug boxes saved to: {output_path}")
    print("Red boxes show clickable areas")

    reader.close()


def test_navigation_overlay_debug():
    """Create navigation overlay with debug visualization."""
    print("\n" + "="*70)
    print("Creating Navigation Overlay with Debug Bounding Boxes")
    print("="*70)

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

    # Load a test book
    test_book = Path(__file__).parent / "tests" / "data" / "library-epub" / "pg11-images-3.epub"
    if not test_book.exists():
        print(f"Test book not found: {test_book}")
        return

    reader.load_epub(str(test_book))

    # Create navigation overlay
    nav_overlay = NavigationOverlay(reader)
    base_page = reader.get_current_page()

    # Get chapters
    chapters = reader.get_chapters()

    # Open overlay (with debug mode, this will draw bounding boxes)
    overlay_image = nav_overlay.open(
        base_page,
        chapters=chapters,
        bookmarks=[{"name": "Test Bookmark"}],
        active_tab="contents"
    )

    # Save the result
    output_path = Path(__file__).parent / "navigation_overlay_debug.png"
    overlay_image.save(output_path)
    print(f"\nNavigation overlay with debug boxes saved to: {output_path}")
    print("Red boxes show clickable areas")

    reader.close()


if __name__ == "__main__":
    test_settings_overlay_debug()
    test_navigation_overlay_debug()
    print("\n" + "="*70)
    print("Debug visualizations created!")
    print("Check the PNG files to see clickable areas marked with red boxes.")
    print("="*70)
