#!/usr/bin/env python3
"""Export face-id / heightmap / upscaled refs from thick-stone-wall alone.

Run from repo root:
  python3 client/scripts/export-thick-wall-refs.py

Requires: Pillow
"""
from __future__ import annotations

from pathlib import Path

from PIL import Image, ImageDraw

ROOT = Path(__file__).resolve().parents[1]
ALONE = ROOT / "assets/blocks/_examples/stone-wall/thick-stone-wall.png"
OUT = ROOT / "assets/blocks/_examples/stone-wall"

# Source greys → semantic face roles (see docs/STONE_WALL_SHAPES.md)
FACE_RGB = {
    (175, 175, 175): (255, 255, 255),  # N bevel
    (149, 149, 149): (220, 60, 60),  # W face
    (108, 108, 108): (60, 100, 220),  # E face
    (84, 84, 84): (60, 180, 80),  # S face
    (128, 128, 128): (220, 180, 60),  # top plateau
    (64, 64, 64): (20, 20, 20),  # outline
}

HEIGHT = {
    (64, 64, 64): 40,
    (84, 84, 84): 80,
    (108, 108, 108): 140,
    (149, 149, 149): 160,
    (128, 128, 128): 200,
    (175, 175, 175): 220,
}


def main() -> None:
    OUT.mkdir(parents=True, exist_ok=True)
    alone = Image.open(ALONE).convert("RGBA")
    w, h = alone.size
    face = Image.new("RGBA", (w, h), (0, 0, 0, 0))
    height = Image.new("L", (w, h), 0)

    for y in range(h):
        for x in range(w):
            r, g, b, a = alone.getpixel((x, y))
            if a == 0:
                continue
            key = (r, g, b)
            if key not in FACE_RGB:
                raise SystemExit(f"Unknown grey {key} at ({x},{y})")
            fr, fg, fb = FACE_RGB[key]
            face.putpixel((x, y), (fr, fg, fb, 255))
            height.putpixel((x, y), HEIGHT[key])

    face.save(OUT / "ref-thick-face-id-16.png")
    face.resize((w * 16, h * 16), Image.NEAREST).save(OUT / "ref-thick-face-id-256.png")
    height.save(OUT / "ref-thick-height-16.png")
    height.resize((w * 16, h * 16), Image.NEAREST).save(OUT / "ref-thick-height-256.png")
    alone.resize((w * 16, h * 16), Image.NEAREST).save(OUT / "ref-thick-alone-256.png")

    leg = Image.new("RGBA", (280, 200), (30, 30, 30, 255))
    d = ImageDraw.Draw(leg)
    items = [
        ((255, 255, 255), "North bevel 0xAF"),
        ((220, 60, 60), "West face 0x95"),
        ((60, 100, 220), "East face 0x6C"),
        ((60, 180, 80), "South face 0x54"),
        ((220, 180, 60), "Top plateau 0x80"),
        ((20, 20, 20), "Outline 0x40"),
    ]
    for i, (c, label) in enumerate(items):
        yy = 16 + i * 28
        d.rectangle([16, yy, 40, yy + 20], fill=(*c, 255))
        d.text((52, yy + 2), label, fill=(230, 230, 230, 255))
    leg.save(OUT / "ref-thick-face-legend.png")

    # Labeled contact sheet of all 16 thick *shape* states (bit order).
    wall_dir = OUT
    names = [
        ("alone", "thick-stone-wall.png"),
        ("n", "thick-stone-wall-n.png"),
        ("e", "thick-stone-wall-e.png"),
        ("ne", "thick-stone-wall-ne.png"),
        ("s", "thick-stone-wall-s.png"),
        ("ns", "thick-stone-wall-ns.png"),
        ("es", "thick-stone-wall-es.png"),
        ("nes", "thick-stone-wall-nes.png"),
        ("w", "thick-stone-wall-w.png"),
        ("wn", "thick-stone-wall-wn.png"),
        ("we", "thick-stone-wall-we.png"),
        ("wne", "thick-stone-wall-wne.png"),
        ("sw", "thick-stone-wall-sw.png"),
        ("swn", "thick-stone-wall-swn.png"),
        ("esw", "thick-stone-wall-esw.png"),
        ("nesw", "thick-stone-wall-nesw.png"),
    ]
    scale, pad, label_h, cols = 8, 28, 14, 4
    cell = 16 * scale
    rows = 4
    sheet_w = cols * cell + (cols + 1) * pad
    sheet_h = rows * (cell + label_h) + (rows + 1) * pad
    sheet = Image.new("RGBA", (sheet_w, sheet_h), (24, 24, 28, 255))
    draw = ImageDraw.Draw(sheet)
    for i, (label, fname) in enumerate(names):
        r, c = divmod(i, cols)
        x = pad + c * (cell + pad)
        y = pad + r * (cell + label_h + pad)
        im = Image.open(wall_dir / fname).convert("RGBA").resize((cell, cell), Image.NEAREST)
        sheet.paste(im, (x, y), im)
        draw.text((x, y + cell + 2), label, fill=(220, 220, 220, 255))
    sheet.save(OUT / "ref-thick-all-variants.png")

    print(f"Wrote refs to {OUT}")


if __name__ == "__main__":
    main()
