#!/usr/bin/env python3
"""Generate the figure of the sequences chapter, docs/sequences/.

An original schematic drawing that transcribes the README of the chapter: where
the introduction and the ending stand in a run of the program. It reads no file.

Usage:
    python3 scripts/sequences/make_sequence_figures.py [output_directory]

The default is docs/sequences/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/sequences/img'

W, H = 700, 250
out = frame(W, H, 'The two sequences in a run of the program',
            'Where the introduction and the ending stand. The program starts with the introduction, which a key '
            'skips once the first scene is shown, then the start menu, whose RUN INTRODUCTION line plays it again. '
            'The game follows. When the final opponent’s second form dies on level 16, the game writes FINAL.SAV, '
            'plays the ending and returns to DOS.',
            'Neither sequence changes the game: the introduction leads to the start menu, the ending to DOS.')
steps = [('Program\nstarts', 'end'), ('Introduction', 'state'), ('Start menu', 'step'), ('The game', 'strong'),
         ('FINAL.SAV\nwritten', 'step'), ('Ending', 'state'), ('DOS', 'end')]
x = 20
xs = []
for label, kind in steps:
    box(out, x, 100, 84, 50, label, kind, 11)
    xs.append(x)
    x += 97
for k in range(len(steps) - 1):
    arrow(out, f'M{xs[k] + 84},125 L{xs[k + 1] - 3},125')
arrow(out, f'M{xs[2] + 42},100 C{xs[2] + 42},70 {xs[1] + 42},70 {xs[1] + 42},97', 'RUN INTRODUCTION', xs[1] + 92, 66)
out.append(text(xs[3] + 132, 180, 'the final opponent’s second', 10.5, INK2, None, 'middle'))
out.append(text(xs[3] + 132, 195, 'form dies on level 16', 10.5, INK2, None, 'middle'))
out.append(text(xs[1] + 42, 180, 'a key skips it', 10.5, INK2, None, 'middle'))
write(OUT, 'sequences-timeline.svg', out)
