"""Shows what an evaluation copy looks like: a document written with no licence key,
which the library stamps with a watermark on every page.

The Python twin of the `write_eval_watermark` example in Rust. A licensed document
is what most examples show; this one is the opposite. No key is set, so the library
writes an evaluation copy and marks every page as one. Set a licence key and the
mark is gone.

The page under the mark is held in `Words`, once per language, and `HQF_PDF_LANG`
picks which set is drawn. The watermark itself is the library's, not the page's, and
does not follow the page's language.

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

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import _language
import _out

import hqf_pdf

# The size each line is set at, and the baseline it sits on, in the order they are laid
# down.
PLACES = (
    (20.0, 760.0),
    (11.0, 730.0),
    (11.0, 700.0),
    (11.0, 684.0),
    (10.0, 120.0),
    (10.0, 106.0),
)


@dataclass(frozen=True)
class Words:
    """Every word the page draws, in one language.

    The watermark is not here: the library writes it, and it says the same thing
    whatever language the page under it is in.
    """

    # A short document laid under the watermark, and under it a note that says what the
    # watermark is, in the order the lines are laid down.
    lines: tuple[str, str, str, str, str, str]


# The page in English.
ENGLISH = Words(
    lines=(
        "Quarterly report",
        "Prepared for HQF Development",
        "Revenue held steady across the three months, and costs fell as the new",
        "rendering pipeline replaced the tools it was built to retire.",
        "This is an evaluation copy: the library stamps every page with the",
        "watermark above until a licence key is set. Set one and it is gone.",
    ),
)

# The page in French.
FRENCH = Words(
    lines=(
        "Rapport trimestriel",
        "Établi pour HQF Development",
        "Le chiffre d’affaires est resté stable sur les trois mois, et les",
        "coûts ont baissé, la nouvelle chaîne de rendu remplaçant les anciens outils.",
        "Ceci est une copie d’évaluation : la bibliothèque marque chaque page",
        "du filigrane ci-dessus sans clé de licence. Avec une clé, il disparaît.",
    ),
)

# The page in Dutch.
DUTCH = Words(
    lines=(
        "Kwartaalrapport",
        "Opgesteld voor HQF Development",
        "De omzet bleef alle drie de maanden stabiel, en de kosten daalden toen de nieuwe",
        "opmaakketen de hulpmiddelen verving die ze moest vervangen.",
        "Dit is een evaluatiekopie: de bibliotheek zet op elke pagina het",
        "watermerk hierboven tot er een licentiesleutel is ingesteld. Met een sleutel verdwijnt het.",
    ),
)

# The page in Portuguese.
PORTUGUESE = Words(
    lines=(
        "Relatório trimestral",
        "Preparado para HQF Development",
        "As receitas mantiveram-se nos três meses, e os custos desceram quando a nova",
        "cadeia de paginação substituiu as antigas ferramentas.",
        "Esta é uma cópia de avaliação: a biblioteca carimba cada página com a",
        "marca de água acima até que se defina uma chave de licença. Com uma, desaparece.",
    ),
)

# The page in Italian.
ITALIAN = Words(
    lines=(
        "Relazione trimestrale",
        "Preparata per HQF Development",
        "I ricavi sono rimasti stabili nei tre mesi, e i costi sono scesi quando la nuova",
        "catena di impaginazione ha sostituito gli strumenti che doveva mandare in pensione.",
        "Questa è una copia di valutazione: la libreria stampa su ogni pagina la",
        "filigrana qui sopra finché non si imposta una chiave di licenza. Con una, sparisce.",
    ),
)

# The page in Spanish.
SPANISH = Words(
    lines=(
        "Informe trimestral",
        "Preparado para HQF Development",
        "Los ingresos se mantuvieron en los tres meses, y los costes bajaron cuando",
        "la nueva cadena de maquetación sustituyó a las herramientas que venía a retirar.",
        "Esta es una copia de evaluación: la biblioteca pone en cada página la",
        "marca de agua de arriba hasta que se fija una clave de licencia. Con una, desaparece.",
    ),
)

# The page in German.
GERMAN = Words(
    lines=(
        "Quartalsbericht",
        "Erstellt für HQF Development",
        "Der Umsatz blieb über die drei Monate stabil, und die Kosten sanken, als die neue",
        "Rendering-Pipeline die Werkzeuge ersetzte, die sie ablösen sollte.",
        "Dies ist eine Evaluierungskopie: Die Bibliothek stempelt jede Seite mit dem",
        "Wasserzeichen oben, bis ein Lizenzschlüssel gesetzt ist. Mit einem Schlüssel verschwindet es.",
    ),
)


# 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, _language.GERMAN: GERMAN, _language.SPANISH: SPANISH, _language.ITALIAN: ITALIAN, _language.PORTUGUESE: PORTUGUESE, _language.DUTCH: DUTCH}


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("eval.pdf", language)).stem)

    document = hqf_pdf.Document()
    # No key is set: the document is an evaluation copy, and the library marks every
    # page of it with a watermark.
    document.set_license(hqf_pdf.License.evaluation())
    font = document.add_font(hqf_pdf.Font.from_path(_out.font_path()))

    content = hqf_pdf.Content()
    for line, (size, top) in zip(words.lines, PLACES):
        content.draw_text(font, size, 72.0, top, line)

    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()
