"""
Demonstration of bundled fonts in pyWebLayout.

This example shows:
1. How to use the bundled DejaVu font families
2. Different font variants (regular, bold, italic, bold-italic)
3. The three font families (Sans, Serif, Monospace)
4. Convenient Font.from_family() method for easy font selection

The demo creates a page showcasing all bundled fonts with different styles.
"""

from pyWebLayout.concrete import Page
from pyWebLayout.abstract import Paragraph, Word
from pyWebLayout.style import Font, FontWeight, FontStyle, BundledFont
from pyWebLayout.style.page_style import PageStyle
from pyWebLayout.layout.document_layouter import DocumentLayouter


def create_font_showcase_page():
    """
    Create a page demonstrating all bundled fonts and variants.
    """
    # Create page with some padding
    page = Page(size=(800, 1000), style=PageStyle(border_width=20))
    layouter = DocumentLayouter(page)

    # Title
    title_font = Font.from_family(
        BundledFont.SANS,
        font_size=32,
        colour=(0, 0, 100),
        weight=FontWeight.BOLD
    )
    title = Paragraph(title_font)
    title.add_word(Word("Bundled", title_font))
    title.add_word(Word("Fonts", title_font))
    title.add_word(Word("Showcase", title_font))
    layouter.layout_paragraph(title)
    page._current_y_offset += 20

    # Introduction
    intro_font = Font.from_family(BundledFont.SANS, font_size=14, colour=(50, 50, 50))
    intro = Paragraph(intro_font)
    intro_text = "pyWebLayout bundles the DejaVu font family with three font types and four variants each."
    for word in intro_text.split():
        intro.add_word(Word(word, intro_font))
    layouter.layout_paragraph(intro)
    page._current_y_offset += 25

    # --- Sans Serif Section ---
    section_font = Font.from_family(
        BundledFont.SANS,
        font_size=20,
        colour=(0, 100, 0),
        weight=FontWeight.BOLD
    )
    sans_section = Paragraph(section_font)
    sans_section.add_word(Word("Sans-Serif", section_font))
    sans_section.add_word(Word("(DejaVu", section_font))
    sans_section.add_word(Word("Sans)", section_font))
    layouter.layout_paragraph(sans_section)
    page._current_y_offset += 10

    # Sans Regular
    sans_regular = Font.from_family(BundledFont.SANS, font_size=16)
    demo_text_paragraph(layouter, page, sans_regular, "Regular:")

    # Sans Bold
    sans_bold = Font.from_family(BundledFont.SANS, font_size=16, weight=FontWeight.BOLD)
    demo_text_paragraph(layouter, page, sans_bold, "Bold:")

    # Sans Italic
    sans_italic = Font.from_family(BundledFont.SANS, font_size=16, style=FontStyle.ITALIC)
    demo_text_paragraph(layouter, page, sans_italic, "Italic:")

    # Sans Bold Italic
    sans_bold_italic = Font.from_family(
        BundledFont.SANS,
        font_size=16,
        weight=FontWeight.BOLD,
        style=FontStyle.ITALIC
    )
    demo_text_paragraph(layouter, page, sans_bold_italic, "Bold Italic:")
    page._current_y_offset += 20

    # --- Serif Section ---
    serif_section = Paragraph(section_font)
    serif_section.add_word(Word("Serif", section_font))
    serif_section.add_word(Word("(DejaVu", section_font))
    serif_section.add_word(Word("Serif)", section_font))
    layouter.layout_paragraph(serif_section)
    page._current_y_offset += 10

    # Serif Regular
    serif_regular = Font.from_family(BundledFont.SERIF, font_size=16)
    demo_text_paragraph(layouter, page, serif_regular, "Regular:")

    # Serif Bold
    serif_bold = Font.from_family(BundledFont.SERIF, font_size=16, weight=FontWeight.BOLD)
    demo_text_paragraph(layouter, page, serif_bold, "Bold:")

    # Serif Italic
    serif_italic = Font.from_family(BundledFont.SERIF, font_size=16, style=FontStyle.ITALIC)
    demo_text_paragraph(layouter, page, serif_italic, "Italic:")

    # Serif Bold Italic
    serif_bold_italic = Font.from_family(
        BundledFont.SERIF,
        font_size=16,
        weight=FontWeight.BOLD,
        style=FontStyle.ITALIC
    )
    demo_text_paragraph(layouter, page, serif_bold_italic, "Bold Italic:")
    page._current_y_offset += 20

    # --- Monospace Section ---
    mono_section = Paragraph(section_font)
    mono_section.add_word(Word("Monospace", section_font))
    mono_section.add_word(Word("(DejaVu", section_font))
    mono_section.add_word(Word("Sans", section_font))
    mono_section.add_word(Word("Mono)", section_font))
    layouter.layout_paragraph(mono_section)
    page._current_y_offset += 10

    # Mono Regular
    mono_regular = Font.from_family(BundledFont.MONOSPACE, font_size=14)
    demo_code_paragraph(layouter, page, mono_regular, "Regular:")

    # Mono Bold
    mono_bold = Font.from_family(BundledFont.MONOSPACE, font_size=14, weight=FontWeight.BOLD)
    demo_code_paragraph(layouter, page, mono_bold, "Bold:")

    # Mono Italic
    mono_italic = Font.from_family(BundledFont.MONOSPACE, font_size=14, style=FontStyle.ITALIC)
    demo_code_paragraph(layouter, page, mono_italic, "Italic:")

    # Mono Bold Italic
    mono_bold_italic = Font.from_family(
        BundledFont.MONOSPACE,
        font_size=14,
        weight=FontWeight.BOLD,
        style=FontStyle.ITALIC
    )
    demo_code_paragraph(layouter, page, mono_bold_italic, "Bold Italic:")
    page._current_y_offset += 20

    # Footer
    footer_font = Font.from_family(BundledFont.SANS, font_size=12, colour=(100, 100, 100))
    footer = Paragraph(footer_font)
    footer_text = "All fonts are free and open source under the Bitstream Vera License."
    for word in footer_text.split():
        footer.add_word(Word(word, footer_font))
    layouter.layout_paragraph(footer)

    return page


def demo_text_paragraph(layouter, page, font, label):
    """Create a paragraph showing sample text with the given font."""
    # Label in smaller font
    label_font = Font.from_family(BundledFont.SANS, font_size=12, colour=(100, 100, 100))
    label_para = Paragraph(label_font)
    label_para.add_word(Word(label, label_font))
    layouter.layout_paragraph(label_para)
    page._current_y_offset += 5

    # Sample text
    para = Paragraph(font)
    sample = "The quick brown fox jumps over the lazy dog. 0123456789"
    for word in sample.split():
        para.add_word(Word(word, font))
    layouter.layout_paragraph(para)
    page._current_y_offset += 8


def demo_code_paragraph(layouter, page, font, label):
    """Create a paragraph showing sample code with the given font."""
    # Label in smaller font
    label_font = Font.from_family(BundledFont.SANS, font_size=12, colour=(100, 100, 100))
    label_para = Paragraph(label_font)
    label_para.add_word(Word(label, label_font))
    layouter.layout_paragraph(label_para)
    page._current_y_offset += 5

    # Sample code
    para = Paragraph(font)
    code = "def hello(): print('Hello, World!') # 0123456789"
    for word in code.split():
        para.add_word(Word(word, font))
    layouter.layout_paragraph(para)
    page._current_y_offset += 8


if __name__ == "__main__":
    print("\n")
    print("=" * 70)
    print("Bundled Fonts Demonstration")
    print("=" * 70)
    print()

    print("Creating font showcase page...")
    page = create_font_showcase_page()

    print("Rendering page...")
    image = page.render()

    output_file = "demo_08_bundled_fonts.png"
    image.save(output_file)
    print(f"Saved: {output_file}")

    print()
    print("=" * 70)
    print("Demo complete!")
    print()
    print("The page showcases all bundled fonts:")
    print("  - DejaVu Sans (Sans-serif)")
    print("  - DejaVu Serif (Serif)")
    print("  - DejaVu Sans Mono (Monospace)")
    print()
    print("Each family has 4 variants:")
    print("  - Regular")
    print("  - Bold")
    print("  - Italic")
    print("  - Bold Italic")
    print("=" * 70)
    print()
