"""Paints axial, radial and function-based gradients, each into a clipped box.

The Python twin of the `write_gradient` example in Rust. Five panels: a
horizontal two-colour gradient, a diagonal one, a radial one whose outer colour
is extended to fill the corners, a three-stop gradient built from a stitched
function, and a checkerboard whose colour is read from a calculation of both
coordinates of the point, the reader asked to filter its sudden colour steps
against aliasing. Each is added to the document once, then painted into a
rectangle the page clips to.

The title and the five labels are held in `Words`, once per language, and `HQF_PDF_LANG`
picks which set is drawn.

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

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import _language
import _licence
import _out

import hqf_pdf

# The width and height of every panel, in points.
BOX = (210.0, 170.0)

# How many squares the checkerboard is cut into along each of its two axes.
SQUARES = 8.0

# How much of the colour it lands on a darkened square of the checkerboard keeps.
SHADED = 0.45


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

    # The title across the top of the page.
    title: str
    # The label under each of the five panels.
    horizontal: str
    diagonal: str
    radial: str
    three_stops: str
    checkerboard: str


# The page in English.
ENGLISH = Words(
    title="Gradients, and a colour read from a function",
    horizontal="Axial, two colours",
    diagonal="Axial, on the diagonal",
    radial="Radial, outer extended",
    three_stops="Axial, three stops",
    checkerboard="Function-based: a checkerboard on x and y at once, anti-aliasing asked for",
)

# The page in French.
FRENCH = Words(
    title="Dégradés, et une couleur lue dans une fonction",
    horizontal="En ligne droite, deux couleurs",
    diagonal="En ligne droite, en diagonale",
    radial="En cercle, couleur extérieure prolongée",
    three_stops="En ligne droite, trois couleurs",
    checkerboard="Par fonction : un damier en largeur et en hauteur à la fois, anticrénelage demandé",
)


# 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 panel(content, font, shading, x, y, label):
    """Paint a gradient into the box at (x, y), sized BOX, and label it."""
    content.save_state()
    content.rect(x, y, BOX[0], BOX[1])
    content.clip()
    content.end_path()
    content.draw_shading(shading)
    content.restore_state()

    content.draw_text(font, 10.0, x, y - 15.0, label)


def checkerboard(x, y):
    """The gradient that paints a checkerboard into the box at (x, y), sized BOX.

    Its colour is read from a calculation of both coordinates of the point at once:
    red grows across the box, green grows up it, and every other square of the
    SQUARES by SQUARES board keeps only SHADED of the colour it lands on. The reader is
    asked to filter the sudden steps between squares against aliasing.
    """
    return hqf_pdf.FunctionBased(
        ((0.0, 1.0), (0.0, 1.0)),
        (BOX[0], 0.0, 0.0, BOX[1], x, y),
        hqf_pdf.DeviceSpace.Rgb,
        hqf_pdf.Calculation(
            [(0.0, 1.0), (0.0, 1.0)],
            [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0)],
            [
                hqf_pdf.Op.integer(2),
                hqf_pdf.Op.copy(),
                hqf_pdf.Op.real(SQUARES),
                hqf_pdf.Op.multiply(),
                hqf_pdf.Op.floor(),
                hqf_pdf.Op.exchange(),
                hqf_pdf.Op.real(SQUARES),
                hqf_pdf.Op.multiply(),
                hqf_pdf.Op.floor(),
                hqf_pdf.Op.add(),
                hqf_pdf.Op.to_integer(),
                hqf_pdf.Op.integer(2),
                hqf_pdf.Op.modulo(),
                hqf_pdf.Op.integer(0),
                hqf_pdf.Op.not_equal(),
                hqf_pdf.Op.integer(3),
                hqf_pdf.Op.integer(1),
                hqf_pdf.Op.roll(),
                hqf_pdf.Op.real(0.2),
                hqf_pdf.Op.integer(4),
                hqf_pdf.Op.integer(3),
                hqf_pdf.Op.roll(),
                hqf_pdf.Op.if_(
                    [
                        hqf_pdf.Op.real(SHADED),
                        hqf_pdf.Op.multiply(),
                        hqf_pdf.Op.integer(3),
                        hqf_pdf.Op.integer(1),
                        hqf_pdf.Op.roll(),
                        hqf_pdf.Op.real(SHADED),
                        hqf_pdf.Op.multiply(),
                        hqf_pdf.Op.integer(3),
                        hqf_pdf.Op.integer(1),
                        hqf_pdf.Op.roll(),
                        hqf_pdf.Op.real(SHADED),
                        hqf_pdf.Op.multiply(),
                        hqf_pdf.Op.integer(3),
                        hqf_pdf.Op.integer(1),
                        hqf_pdf.Op.roll(),
                    ]
                ),
            ],
        ),
    ).anti_alias(True)


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

    document = hqf_pdf.Document()
    document.set_license(_licence.licensed())
    handle = document.add_font(hqf_pdf.Font.from_path(_out.font_path()))

    # Left column x, right column x, then the foot of each of the three rows.
    left, right, top, middle, bottom = 70.0, 315.0, 560.0, 320.0, 100.0

    # A horizontal two-colour gradient across the top-left box.
    horizontal = document.add_shading(
        hqf_pdf.Axial.rgb(
            (left, top),
            (left + BOX[0], top),
            hqf_pdf.Rgb(0.85, 0.12, 0.12),
            hqf_pdf.Rgb(0.12, 0.2, 0.8),
        )
    )

    # A diagonal two-colour gradient across the top-right box.
    diagonal = document.add_shading(
        hqf_pdf.Axial.rgb(
            (right, middle + BOX[1]),
            (right + BOX[0], top + BOX[1]),
            hqf_pdf.Rgb(0.95, 0.8, 0.1),
            hqf_pdf.Rgb(0.1, 0.55, 0.2),
        )
    )

    # A radial gradient in the middle-left box, its outer colour extended so it reaches
    # the corners.
    radial = document.add_shading(
        hqf_pdf.Radial.rgb(
            (left + BOX[0] / 2.0, middle + BOX[1] / 2.0, 0.0),
            (left + BOX[0] / 2.0, middle + BOX[1] / 2.0, BOX[0] / 2.0),
            hqf_pdf.Rgb(1.0, 1.0, 1.0),
            hqf_pdf.Rgb(0.1, 0.15, 0.45),
        ).extend(False, True)
    )

    # A three-stop gradient in the middle-right box, from a stitched function: red to
    # green over the first half, green to blue over the second.
    rainbow_function = hqf_pdf.Function.stitching(
        (0.0, 1.0),
        [
            hqf_pdf.Function.exponential(
                (0.0, 1.0), 1.0, ([0.85, 0.12, 0.12], [0.12, 0.6, 0.2])
            ),
            hqf_pdf.Function.exponential(
                (0.0, 1.0), 1.0, ([0.12, 0.6, 0.2], [0.12, 0.2, 0.8])
            ),
        ],
        [0.5],
        [(0.0, 1.0), (0.0, 1.0)],
    )
    rainbow = document.add_shading(
        hqf_pdf.Axial(
            (right, middle),
            (right + BOX[0], middle),
            hqf_pdf.DeviceSpace.Rgb,
            rainbow_function,
        )
    )

    # A checkerboard across the bottom-left box.
    board = document.add_shading(checkerboard(left, bottom))

    content = hqf_pdf.Content()

    content.draw_text(handle, 18.0, left, 770.0, words.title)

    panel(content, handle, horizontal, left, top, words.horizontal)
    panel(content, handle, diagonal, right, top, words.diagonal)
    panel(content, handle, radial, left, middle, words.radial)
    panel(content, handle, rainbow, right, middle, words.three_stops)
    panel(content, handle, board, left, bottom, words.checkerboard)

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