#!/usr/bin/env python3
"""Generate the figure of the reference entry page, docs/reference/README.md.

An original schematic drawing of how the three parts of the book lead to the
reference: rule pages to values, structures and executable entries; file pages
to schemas and field references. It reads no file.

Usage:
    python3 scripts/reference/make_reference_figures.py [output_directory]

The default is docs/reference/img/.
"""
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
sys.path[:0] = [str(Path(__file__).resolve().parent), str(ROOT / 'scripts/common')]
from svgkit import frame, write, box, arrow, text, INK2  # noqa: E402

OUT = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / 'docs/reference/img'

W, H = 700, 330
out = frame(W, H, 'How the book reaches the reference',
            'The two first parts of the book and the reference they point to. The rule pages of How the game works '
            'link the values (enumerations), the runtime structures and the entries of the executable reference '
            'that hold the routines and data they describe. The file pages of From files to the game link the '
            'schema of each format and the field reference generated from it. The anomaly catalogue lists every '
            'anomaly and bug, with links to both. The sources and tools pages close the reference.',
            'Every rule and every file page points to the entries that give its evidence.')
box(out, 20, 80, 190, 60, 'How the game works\nrules and catalogues', 'strong', 12)
box(out, 20, 190, 190, 60, 'From files to the game\none page per file', 'strong', 12)
targets = [(290, 70, 'Values: enumerations'), (290, 112, 'Runtime structures'),
           (290, 154, 'The executable: routines,\ndata, overlays, drivers'),
           (290, 206, 'File schemas (.ksy)'), (290, 248, 'Field references,\ngenerated')]
for x, y, label in targets:
    box(out, x, y, 190, 36 if '\n' not in label else 44, label, 'state', 11)
for y in (88, 130, 176):
    arrow(out, f'M210,110 L287,{y}')
for y in (224, 270):
    arrow(out, f'M210,220 L287,{y}')
box(out, 540, 120, 140, 60, 'Anomalies\nand bugs', 'end', 12)
arrow(out, 'M480,176 L537,160')
out.append(text(540, 210, 'Sources and tools', 11, INK2))
out.append(text(540, 226, 'close the reference', 11, INK2))
write(OUT, 'reference-map.svg', out)
