# ━━━ infographic.ling — the miao tutorial poster, drawn by miao's own ━━━
# design system. Run from the ling-source root:
#   ling run --interp tools/infographic.ling
# It opens a 1600x980 window, draws the poster, writes a PNG into
# ./screenshots/ (via the screenshot builtin), and closes itself.

use "../ling/theme.ling"
use "../ling/core.ling"
use "../ling/ds.ling"
use "../ling/icons.ling"

fn settings_save() { return 0.0 }

# a word in big per-letter rainbow glyphs
fn ig_rainbow_word(x, y, s, text) {
    bind cx = x
    bind i = 0.0
    while i < len(text) {
        bind ch = str_slice(text, i, 1.0)
        g_text(cx, y, s, c_pal(i), ch)
        bind cx = cx + g_text_w(s, ch) + 4.0
        bind i = i + 1.0
    }
    return cx
}

# storage glyphs drawn by hand (the fonts lack ○ ◐ ◍)
fn ig_glyph(kind, cx, cy, r, col) {
    if kind == "whole" {
        g_ring(cx, cy, r, col)
    }
    if kind == "delta" {
        g_disc(cx, cy, r, col)
        g_rect(cx, cy - r, r + 1.0, r * 2.0 + 1.0, C_BG_SIDE)
        g_ring(cx, cy, r, col)
    }
    if kind == "chunks" {
        g_disc(cx, cy, r, col)
        g_disc(cx - r * 0.4, cy - r * 0.3, 1.5, C_BG_SIDE)
        g_disc(cx + r * 0.4, cy - r * 0.1, 1.5, C_BG_SIDE)
        g_disc(cx, cy + r * 0.4, 1.5, C_BG_SIDE)
    }
    return 0.0
}

fn ig_rule(x, y, w) {
    bind seg = w / 7.0
    bind i = 0.0
    while i < 7.0 {
        g_rect(x + i * seg, y, seg, 3.0, c_pal(i))
        bind i = i + 1.0
    }
    return 0.0
}

# one workflow step: colored disc, command, explanation, git chip
fn ig_step(x, y, idx, cmd, what, git) {
    bind c = c_pal(idx)
    g_disc(x + 14.0, y + 14.0, 11.0, c)
    g_text_c(x + 14.0, y + 7.0, 12.0, C_BG_DEEP, num_str(idx + 1.0))
    g_text(x + 36.0, y, 14.0, C_TEXT_STRONG, cmd)
    g_text(x + 36.0, y + 19.0, 11.0, C_TEXT_DIM, what)
    bind gw = g_text_w(10.0, git) + 12.0
    g_rrect(x + 414.0 - gw, y + 1.0, gw, 16.0, 3.0, C_BG_DEEP)
    g_rframe(x + 414.0 - gw, y + 1.0, gw, 16.0, 3.0, C_LINE)
    g_text(x + 420.0 - gw, y + 3.0, 10.0, c, git)
    if idx < 6.0 {
        g_line(x + 14.0, y + 27.0, x + 14.0, y + 43.0, C_LINE)
    }
    return 0.0
}

# a terminal line with a colored gutter glyph
fn ig_term(x, y, col, text) {
    g_text(x, y, 11.5, col, text)
    return 0.0
}

# a storage-strategy card
fn ig_strat(x, y, w, col, glyph, name, l1, l2) {
    g_rrect(x, y, w, 58.0, 4.0, C_BG_SIDE)
    g_rframe(x, y, w, 58.0, 4.0, col)
    ig_glyph(glyph, x + 17.0, y + 15.0, 7.0, col)
    g_text(x + 32.0, y + 8.0, 13.0, C_TEXT_STRONG, name)
    g_text(x + 10.0, y + 27.0, 10.0, C_TEXT_DIM, l1)
    g_text(x + 10.0, y + 41.0, 10.0, C_TEXT_DIM, l2)
    return 0.0
}

fn ig_arrow_down(x, y0, y1, col) {
    g_line(x, y0, x, y1 - 6.0, col)
    g_tri(x - 5.0, y1 - 8.0, x + 5.0, y1 - 8.0, x, y1, col)
    return 0.0
}

fn ig_temple_box(x, y, w, h, col, title, sub) {
    g_rrect(x, y, w, h, 4.0, C_BG_SIDE)
    g_rframe(x, y, w, h, 4.0, col)
    ds_brackets(x, y, w, h, 6.0, col)
    g_text_c(x + w * 0.5, y + 7.0, 12.0, C_TEXT_STRONG, title)
    g_text_c(x + w * 0.5, y + 24.0, 10.0, C_TEXT_DIM, sub)
    return 0.0
}

fn ig_draw() {
    bind W = 1600.0
    bind H = 980.0
    g_rect(0.0, 0.0, W, H, C_BG_APP)
    # ── header ────────────────────────────────────────────────────────
    bind after = ig_rainbow_word(40.0, 26.0, 34.0, "MIAO")
    g_text(after + 18.0, 30.0, 30.0, C_TEXT_STRONG, "庙")
    g_text(after + 64.0, 34.0, 16.0, C_TEXT, "the temple of versions — a tutorial")
    g_text(after + 64.0, 56.0, 11.0, C_TEXT_DIM, "a version control system in the Ling language · every command answers to three names: english · pinyin · git")
    g_text_r(W - 40.0, 30.0, 11.0, C_TEXT_DIM, "repo = temple · file version = spirit")
    g_text_r(W - 40.0, 46.0, 11.0, C_TEXT_DIM, "branch = soul · commit = rite · tag = talisman")
    ig_rule(40.0, 86.0, W - 80.0)

    # ── column 1: the daily worship ───────────────────────────────────
    bind x1 = 40.0
    ds_frame(x1, 108.0, 480.0, 436.0, ds_col("ctl"), "DAILY WORSHIP")
    bind sy = 140.0
    ig_step(x1 + 18.0, sy, 0.0, "miao consecrate", "found a temple in this folder", "init")
    ig_step(x1 + 18.0, sy + 48.0, 1.0, "miao offer .", "place changed spirits on the altar", "add")
    ig_step(x1 + 18.0, sy + 96.0, 2.0, "miao rite -m \"genesis\"", "seal the altar into history", "commit")
    ig_step(x1 + 18.0, sy + 144.0, 3.0, "miao divine · miao yinyang", "what stirs? dark removed, light added", "status/diff")
    ig_step(x1 + 18.0, sy + 192.0, 4.0, "miao soul idea · miao summon idea", "kindle a second soul and call it forth", "branch")
    ig_step(x1 + 18.0, sy + 240.0, 5.0, "miao harmonize idea", "unite two souls; clashes become discord", "merge")
    ig_step(x1 + 18.0, sy + 288.0, 6.0, "miao talisman v1.0", "fix a name to this moment forever", "tag")
    g_text(x1 + 18.0, sy + 338.0, 10.5, C_TEXT_DIM, "restless = modified · wandering = untracked · on the altar = staged")
    g_text(x1 + 18.0, sy + 354.0, 10.5, C_TEXT_DIM, "undo: miao withdraw <f> (unstage) · miao restore <f> (checkout file)")

    # a real session, colored as the terminal shows it
    g_rrect(x1, 560.0, 480.0, 204.0, 5.0, C_BG_DEEP)
    g_rframe(x1, 560.0, 480.0, 204.0, 5.0, C_LINE_SOFT)
    bind tx = x1 + 14.0
    bind ty = 572.0
    ig_term(tx, ty, C_TEXT_DIM, "$ miao offer .")
    ig_term(tx, ty + 17.0, C_YELLOW, "  + offered sutra.txt")
    ig_glyph("delta", tx + 246.0, ty + 23.0, 5.0, C_BLUE)
    g_text(tx + 256.0, ty + 17.0, 11.5, C_BLUE, "delta")
    ig_term(tx, ty + 34.0, C_YELLOW, "  + offered statue.bin")
    ig_glyph("chunks", tx + 246.0, ty + 40.0, 5.0, C_VIOLET)
    g_text(tx + 256.0, ty + 34.0, 11.5, C_VIOLET, "chunks 8")
    ig_term(tx, ty + 51.0, C_TEXT_DIM, "$ miao rite -m \"genesis\"")
    ig_term(tx, ty + 68.0, C_GREEN, "  * rite sealed")
    bind ai = 0.0
    bind aura = "6cfef03"
    while ai < 7.0 {
        g_text(tx + 110.0 + ai * 8.0, ty + 68.0, 11.5, c_pal(hex_to_number(str_slice(aura, ai, 1.0))), str_slice(aura, ai, 1.0))
        bind ai = ai + 1.0
    }
    g_text(tx + 175.0, ty + 68.0, 11.5, C_TEXT_DIM, "· 21 spirits enshrined")
    ig_term(tx, ty + 85.0, C_TEXT_DIM, "$ miao divine")
    ig_term(tx, ty + 102.0, C_GREEN, "  the temple is serene — nothing to divine")
    ig_term(tx, ty + 123.0, C_TEXT_DIM, "$ miao yinyang")
    ig_term(tx, ty + 140.0, C_GREEN, "  + the incense coil flares emerald tonight")
    ig_term(tx, ty + 157.0, C_RED, "  − the incense coil burns in the hall")
    ig_term(tx, ty + 174.0, C_INDIGO, "    ... 94 serene lines ...")

    # ROYGBIV legend under the terminal
    g_text(x1, 786.0, 12.0, C_TEXT_STRONG, "the seven colors mean things")
    bind names = ["errors·deletions", "restless spirits", "the altar", "additions·success", "souls·info", "rite ids", "talismans·wandering"]
    bind li = 0.0
    while li < 7.0 {
        bind lx = x1 + (li % 4.0) * 122.0
        bind ly = 810.0 + floor(li / 4.0) * 26.0
        g_rrect(lx, ly, 12.0, 12.0, 2.0, c_pal(li))
        g_text(lx + 17.0, ly, 9.5, C_TEXT_DIM, list_get(names, li))
        bind li = li + 1.0
    }
    g_text(x1, 874.0, 10.5, C_TEXT_DIM, "color off: --plain or MIAO_PLAIN=1")

    # ── column 2: the diviner ─────────────────────────────────────────
    bind x2 = 560.0
    ds_frame(x2, 108.0, 480.0, 330.0, ds_col("func"), "HOW SPIRITS ARE ENSHRINED")
    g_text(x2 + 18.0, 136.0, 11.0, C_TEXT, "every file version is a spirit, named by the BLAKE3 of its content.")
    g_text(x2 + 18.0, 152.0, 11.0, C_TEXT, "at offer time a diviner reads size, text-or-binary and ancestry,")
    g_text(x2 + 18.0, 168.0, 11.0, C_TEXT, "then chooses how each spirit is stored:")
    g_diamond(x2 + 240.0, 206.0, 14.0, C_YELLOW)
    g_text_c(x2 + 240.0, 220.0, 10.0, C_TEXT_DIM, "the diviner")
    ig_arrow_down(x2 + 110.0, 212.0, 244.0, C_GREEN)
    ig_arrow_down(x2 + 240.0, 220.0, 244.0, C_BLUE)
    ig_arrow_down(x2 + 370.0, 212.0, 244.0, C_VIOLET)
    ig_strat(x2 + 18.0, 246.0, 142.0, C_GREEN, "whole", "whole", "small spirits", "kept entire")
    ig_strat(x2 + 170.0, 246.0, 142.0, C_BLUE, "delta", "delta", "text files: line", "deltas, chain <= 8")
    ig_strat(x2 + 322.0, 246.0, 142.0, C_VIOLET, "chunks", "chunks", "big/binary: 64-256K", "content-cut, dedup")
    g_rrect(x2 + 18.0, 318.0, 446.0, 24.0, 4.0, C_BG_DEEP)
    g_text(x2 + 28.0, 323.0, 11.0, C_YELLOW, "measured: a 2-byte edit of a 1.2 MB file re-stores 88 KiB, not 1.2 MB")
    g_text(x2 + 18.0, 352.0, 10.5, C_TEXT_DIM, "identity is separate from storage, so `divine` is one fast hash per file,")
    g_text(x2 + 18.0, 368.0, 10.5, C_TEXT_DIM, "every byte round-trips exactly, and unchanged content is never stored twice.")
    g_text(x2 + 18.0, 392.0, 10.5, C_TEXT_DIM, "inspect any rite: miao manifest <id> shows each spirit's strategy + size.")

    # ── column 2 lower: three names ───────────────────────────────────
    ds_frame(x2, 456.0, 480.0, 210.0, ds_col("bind"), "EVERY COMMAND HAS THREE NAMES")
    bind rows_en = ["offer", "rite", "lineage", "summon", "harmonize", "enshrine"]
    bind rows_py = ["gong 供", "ji 祭", "pu 谱", "zhao 召", "he 合", "fenling 分灵"]
    bind rows_git = ["add", "commit", "log", "switch / checkout", "merge", "clone"]
    bind ri = 0.0
    while ri < 6.0 {
        bind ry = 488.0 + ri * 26.0
        g_text(x2 + 24.0, ry, 12.0, C_GREEN, "miao " + list_get(rows_en, ri))
        g_text(x2 + 190.0, ry, 12.0, C_VIOLET, list_get(rows_py, ri))
        g_text(x2 + 320.0, ry, 12.0, C_INDIGO, list_get(rows_git, ri))
        bind ri = ri + 1.0
    }
    g_text(x2 + 24.0, 644.0, 10.5, C_TEXT_DIM, "muscle memory keeps working: miao status, miao log, miao diff, miao push…")

    # ── column 2 bottom: lingos ───────────────────────────────────────
    ds_frame(x2, 684.0, 480.0, 206.0, ds_col("asm"), "ON LINGOS")
    g_text(x2 + 18.0, 712.0, 11.0, C_TEXT, "the lsh shell carries the traveling shrine 行宫 — miao kai · wen ·")
    g_text(x2 + 18.0, 728.0, 11.0, C_TEXT, "gong · ji · pu · hun · zhao — history in one append-only book of rites,")
    g_text(x2 + 18.0, 744.0, 11.0, C_TEXT, "shaped for lingfs. and the prompt shows your soul, zsh-style:")
    g_rrect(x2 + 18.0, 766.0, 446.0, 54.0, 5.0, C_BG_DEEP)
    g_text(x2 + 30.0, 774.0, 12.0, C_RED, "/home/ling")
    g_text(x2 + 108.0, 774.0, 12.0, C_TEXT_DIM, "  (")
    g_text(x2 + 124.0, 774.0, 12.0, C_VIOLET, "miao:")
    g_text(x2 + 158.0, 774.0, 12.0, C_BLUE, "main")
    g_text(x2 + 190.0, 774.0, 12.0, C_YELLOW, " +")
    g_text(x2 + 202.0, 774.0, 12.0, C_TEXT_DIM, ")")
    g_text(x2 + 30.0, 796.0, 12.0, C_BLUE, "ling")
    g_text(x2 + 58.0, 796.0, 12.0, C_TEXT_DIM, "@")
    g_text(x2 + 66.0, 796.0, 12.0, C_GREEN, "lingos")
    g_text(x2 + 110.0, 796.0, 12.0, C_RED, " # ")
    g_text(x2 + 130.0, 796.0, 12.0, C_TEXT, "miao wen")
    g_text(x2 + 18.0, 832.0, 10.5, C_TEXT_DIM, "the yellow + means offerings sit on the altar — like a dirty git branch.")
    g_text(x2 + 18.0, 848.0, 10.5, C_TEXT_DIM, "desktop and LingOS share the same ceremonies; the shrine is text-only.")

    # ── column 3: the incense network ─────────────────────────────────
    bind x3 = 1080.0
    ds_frame(x3, 108.0, 480.0, 330.0, ds_col("asm"), "SHARE: THE INCENSE NETWORK")
    ig_temple_box(x3 + 130.0, 136.0, 220.0, 40.0, C_RED, "YOUR TEMPLE", "miao fair  (opens an SSH gate)")
    g_text_c(x3 + 240.0, 184.0, 10.0, C_YELLOW, "miao+ssh://pilgrim:SECRET@host:7447/temple")
    ig_arrow_down(x3 + 160.0, 196.0, 230.0, C_GREEN)
    ig_arrow_down(x3 + 320.0, 196.0, 230.0, C_GREEN)
    ig_temple_box(x3 + 40.0, 232.0, 190.0, 40.0, C_BLUE, "A DAUGHTER TEMPLE", "miao enshrine <link>")
    ig_temple_box(x3 + 250.0, 232.0, 190.0, 40.0, C_BLUE, "ANOTHER, ANYWHERE", "miao enshrine <link>")
    g_text(x3 + 52.0, 284.0, 10.5, C_GREEN, "miao pilgrimage — pull new rites")
    g_text(x3 + 52.0, 300.0, 10.5, C_ORANGE, "miao tribute — push (needs the --tribute link)")
    g_text(x3 + 52.0, 316.0, 10.5, C_VIOLET, "miao enshrine near — find fairs on your LAN")
    g_text(x3 + 52.0, 332.0, 10.5, C_TEXT_DIM, "no network? miao vessel <file> carries the whole temple")
    g_rrect(x3 + 18.0, 354.0, 446.0, 70.0, 4.0, C_BG_DEEP)
    g_text(x3 + 28.0, 360.0, 10.5, C_GREEN, "✓ its own generated host key — your ~/.ssh is never touched")
    g_text(x3 + 28.0, 376.0, 10.5, C_GREEN, "✓ no shell, no exec: the server answers five miao ceremonies only")
    g_text(x3 + 28.0, 392.0, 10.5, C_GREEN, "✓ read-only by default; pushes need a separate secret + hash checks")
    g_text(x3 + 28.0, 408.0, 10.5, C_GREEN, "✓ one-time credentials in the link; re-open the fair to rotate")

    # ── column 3 lower: the window ────────────────────────────────────
    ds_frame(x3, 456.0, 480.0, 434.0, ds_col("mod"), "THE WINDOW — MIAO GUI")
    bind img = image_load("screenshots/lingmiao-divine2.png")
    if img >= 0.0 {
        image_draw(img, x3 + 20.0, 486.0, 440.0, 284.0)
        g_rframe(x3 + 20.0, 486.0, 440.0, 284.0, 1.0, C_LINE)
    }
    g_text(x3 + 20.0, 782.0, 10.5, C_TEXT_DIM, "stage by double-click · live yinyang pane · seal rites · browse the")
    g_text(x3 + 20.0, 798.0, 10.5, C_TEXT_DIM, "lineage with rainbow auras · run the SSH fair from inside the window.")
    g_text(x3 + 20.0, 822.0, 11.0, C_TEXT, "open it:  miao gui   (or LingMiao.bat)")
    g_text(x3 + 20.0, 844.0, 10.5, C_TEXT_DIM, "F5 re-divines · ctrl+Q departs")

    # ── footer ────────────────────────────────────────────────────────
    ig_rule(40.0, 918.0, W - 80.0)
    bind fi = 0.0
    while fi < 7.0 {
        g_disc(52.0 + fi * 14.0, 946.0, 4.0, c_pal(fi))
        bind fi = fi + 1.0
    }
    g_text(160.0, 938.0, 11.0, C_TEXT_DIM, "miao help — the full colored table · win / mac / linux / LingOS · written in the Ling language, themed after ling 灵, the numinous")
    g_text_r(W - 40.0, 938.0, 11.0, C_TEXT_DIM, "miao 1.0.0")
    return 0.0
}

bind start = do {
    open_window(1600.0, 980.0, "miao — tutorial", 1)
    px_flat()
    bind f = font_load("assets/font.ttf")
    if f < 0.0 { bind f = font_load("../assets/font.ttf") }
    bind uf = font_load("assets/fonts/DaemonSans.ttf")
    if uf < 0.0 { bind uf = font_load("../assets/fonts/DaemonSans.ttf") }
    if f >= 0.0 {
        if uf >= 0.0 { font_fallback(f, uf, 0.88) }
    }
    st_set("font", f)
    bind frame = 0.0
    while window_is_open() && frame < 40.0 {
        u_begin()
        ig_draw()
        u_end()
        present()
        if frame == 30.0 { screenshot("miao-tutorial") }
        bind frame = frame + 1.0
    }
}
