"""Writes a page of Aztec codes, drawn as squares.

The Python twin of the `write_aztec` example in Rust: the same page, through
the binding rather than through the library directly.

Whether an Aztec code works is not a question the page can answer, and not one
a human eye can: it is answered by a scanner. So the page carries what each
code says under it, and `scripts/check_aztec.sh` points a decoder at the
rendered image and compares.

An Aztec code needs no quiet zone: a scanner finds the bullseye at its centre
without one. The codes are still set apart from each other, so that one code's
modules are not read as another's.

The head of the sheet says what the squares are for, so that the page is worth
something to whoever opens it without the code beside it. Those words are held in
`Words`, once per language, and `HQF_PDF_LANG` picks which set the sheet is headed in.
What the codes carry stands apart from them: a scanner reads it, not a person, and it is
the same in every language.

Usage: python examples/write_aztec.py [out.pdf] [font.ttf]
       HQF_PDF_LANG=fr python examples/write_aztec.py
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import _language
import _licence
import _out

import hqf_pdf

# What the codes carry, and how much of each is given over to error correction: a
# boarding pass at the share the standard advises, a tracking link at twice that, and a
# line of mixed punctuation at the least a code will take.
CARRIED = [
    ("HQF2026 PARIS LONDON 14B 07JUL", 23),
    ("https://example.test/track/FR-1964413", 46),
    ("a-Z 0..9 +*#% and on", 5),
]

# The side of one module, in points. Four pixels to a module at 288dpi, so every
# module's edge lands on a pixel boundary.
MODULE = 3.0

# The clear space left between one code and the next, in points.
GAP = 18.0

# How far to the right of a code its label is set, in points.
LABEL = 12.0

# Where the head of the first code sits, in points up from the foot of the sheet.
TOP = 700.0

# How far in from the left edge of the sheet the head of the page is set. The codes
# stand there too, so the heading stands over their left edge.
MARGIN = 72.0

# Where the baseline of the heading sits, in points up from the foot of the sheet.
HEAD_TOP = 794.0

# The size the heading is set at, in points.
HEADING = 16.0

# The size the lines under the heading are set at, in points.
BODY = 10.5

# How far under the heading the first line beneath it sits, in points.
HEAD_GAP = 24.0

# How far below one line under the heading the next one sits, in points.
LINE_STEP = 15.0

@dataclass(frozen=True)
class Words:
    """Every word the sheet is headed in, in one language."""

    # What the page says it is, at its head and in what the file says of itself.
    title: str
    # What the page says under its heading.
    lines: tuple[str, str]


# The head of the sheet in English.
ENGLISH = Words(
    title="Aztec codes, drawn as squares",
    lines=(
        "An Aztec code asks for no white margin around it, so it fits where nothing else",
        "would; a train ticket carries one. Beside each is what a scanner reads back.",
    ),
)

# The head of the sheet in French.
FRENCH = Words(
    title="Des codes Aztec, dessinés en carrés",
    lines=(
        "Un code Aztec ne demande aucune marge blanche autour de lui : il tient là où rien",
        "d'autre ne tiendrait. Un billet de train en porte un. À côté, ce qu'un scanner lit.",
    ),
)

# Every language the example is written in. A language is added by writing its own set
# of words and naming it here.
WORDS = {_language.ENGLISH: ENGLISH, _language.FRENCH: FRENCH}


def head(content, handle, words) -> None:
    """Draw the heading, and the lines under it that say what the page is for."""
    content.draw_text(handle, HEADING, MARGIN, HEAD_TOP, words.title)

    y = HEAD_TOP - HEAD_GAP
    for line in words.lines:
        content.draw_text(handle, BODY, MARGIN, y, line)
        y -= LINE_STEP


def main() -> None:
    language = _language.from_environment()
    words = _language.words_of(WORDS, language)

    # A named file is written as named; the default one carries the language, so the two
    # languages do not overwrite each other in `tmp/`.
    out = _out.output_path(Path(_language.file_name("aztec.pdf", language)).stem)

    document = hqf_pdf.Document()
    document.set_license(_licence.licensed())
    document.set_info("Title", words.title)

    handle = document.add_font(hqf_pdf.Font.from_path(_out.font_path()))

    content = hqf_pdf.Content()
    content.set_fill(hqf_pdf.Rgb(0.0, 0.0, 0.0))
    head(content, handle, words)

    # One code to a row, its label level with the middle of it, so no label runs into
    # the code or the label beside it.
    left = MARGIN
    top = TOP
    for carried, correction in CARRIED:
        code = hqf_pdf.Aztec(carried, correction)
        side = code.size * MODULE
        bottom = top - side

        content.draw_aztec(code, left, bottom, side)
        content.draw_text(handle, 8.0, left + side + LABEL, bottom + side / 2.0, carried)

        top = bottom - GAP

    page = hqf_pdf.Page.a4()
    page.set_content(content)
    document.add_page(page)

    written = document.write(out)
    print(f"wrote {out} ({written} bytes)")


if __name__ == "__main__":
    main()
