"""Compose the field render poster from render_fields.py outputs.

Main panel: Kida-Pelz vorticity magnitude slice (z = pi plane) at peak
enstrophy, log-scaled brass colormap on ink, with in-plane streamlines.
Bottom panel: per-site xy-plaquette values on one thermalized SU(2) time
slice, drawn as a woven diamond pattern. All values read from
render_meta.json, so the labels carry the measured numbers.
"""
import json

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap, LogNorm, Normalize
import numpy as np

OUT = "/home/hatch/workspace/millennium-lab/poster"
meta = json.load(open("%s/render_meta.json" % OUT))
kp = meta["kida_pelz"]
ym = meta["yang_mills"]

vort = np.load("%s/kida_peak_vortmag.npy" % OUT)  # [x, y]
u = np.load("%s/kida_peak_u.npy" % OUT)
v = np.load("%s/kida_peak_v.npy" % OUT)
plaq = np.load("%s/ym_plaquette_slice.npy" % OUT)  # [x, y], 8x8

INK = "#14110d"
PAPER = "#f7f2e7"
BONE = "#e8ddc4"
MUTED = "#a89c83"
BRASS = "#c9a227"
BRASS_DEEP = "#9a7b2d"

cmap = LinearSegmentedColormap.from_list("brass_fire",
    ["#14110d", "#3a2c14", "#7a5c1e", "#c9a227", "#e8d9a8", "#fffdf6"])

fig = plt.figure(figsize=(10.8, 13.5), dpi=100)
fig.patch.set_facecolor(INK)

# Header
fig.text(0.06, 0.968, "MILLENNIUM LAB  |  COMPUTED, NOT ILLUSTRATED",
         fontsize=12.5, color=BRASS, fontweight="bold", va="top")
fig.text(0.06, 0.938, "The field at its peak", fontsize=30, color=PAPER,
         fontweight="bold", va="top", family="DejaVu Serif")
fig.text(0.06, 0.908,
         "Kida-Pelz vorticity, 48 cubed, viscosity 0.001, at t = %.1f, the moment of peak enstrophy."
         % kp["peak_time"], fontsize=12.5, color=MUTED, va="top")
fig.text(0.06, 0.890,
         "Enstrophy growth %.2f times. It stayed smooth." % kp["enstrophy_growth"],
         fontsize=12.5, color=MUTED, va="top")

# Main panel: vorticity slice + streamlines
ax = fig.add_axes([0.05, 0.375, 0.90, 0.475])
ax.set_facecolor(INK)
x = np.linspace(0, 2 * np.pi, vort.shape[0])
ax.imshow(vort.T, origin="lower", extent=[0, 2 * np.pi, 0, 2 * np.pi],
          cmap=cmap, norm=LogNorm(vmin=0.05, vmax=vort.max()),
          interpolation="bilinear", aspect="auto")
ax.streamplot(x, x, u.T, v.T, color=(0.91, 0.87, 0.77, 0.55), linewidth=0.8,
              density=1.4, arrowsize=0.7)
ax.set_xlim(0, 2 * np.pi)
ax.set_ylim(0, 2 * np.pi)
ax.set_xticks([])
ax.set_yticks([])
for s in ax.spines.values():
    s.set_color(BRASS_DEEP)
    s.set_linewidth(1.2)
ax.set_title("Vorticity magnitude |ω| with streamlines, z = π plane, max |ω| in slice %.2f"
             % kp["max_vorticity_peak_slice"],
             fontsize=11.5, color=MUTED, loc="left", pad=8)

# Middle stats band
fig.text(0.06, 0.345,
         "ENSTROPHY GROWTH  %.2f x      PEAK t = %.1f      MAX |ω|  %.2f"
         % (kp["enstrophy_growth"], kp["peak_time"], kp["max_vorticity_peak_slice"]),
         fontsize=12, color=BRASS, fontweight="bold", va="top")
fig.text(0.06, 0.322,
         "The blowup candidate, at its strongest moment, on the lab's own solver.",
         fontsize=12, color=MUTED, va="top")

# Bottom panel: plaquette diamond pattern
fig.text(0.06, 0.295, "SU(2) PLAQUETTE FIELD, BETA 2.3, ONE TIME SLICE",
         fontsize=12, color=BRASS, fontweight="bold", va="top")
ax2 = fig.add_axes([0.30, 0.045, 0.40, 0.215])
ax2.set_facecolor(INK)
gx, gy = np.meshgrid(np.arange(8), np.arange(8))
ax2.scatter(gx.ravel(), gy.ravel(), marker="D", s=5200,
            c=plaq.T.ravel(), cmap=cmap,
            norm=Normalize(vmin=ym["plaquette_min"], vmax=ym["plaquette_max"]),
            edgecolors=INK, linewidths=2.5)
ax2.set_xlim(-0.9, 7.9)
ax2.set_ylim(-0.9, 7.9)
ax2.set_aspect("equal")
ax2.set_xticks([])
ax2.set_yticks([])
for s in ax2.spines.values():
    s.set_visible(False)
fig.text(0.06, 0.22,
         "Each diamond is one\n"
         "plaquette, (1/2) Re Tr U_p,\n"
         "8 by 8 sites.\n"
         "Slice mean %.2f,\n"
         "range %.2f to %.2f.\n"
         "Thermalized 400 sweeps."
         % (ym["plaquette_mean_slice"], ym["plaquette_min"], ym["plaquette_max"]),
         fontsize=11.5, color=MUTED, va="top")

# Footer
fig.text(0.06, 0.018,
         "Re-computed here from the lab's own solvers. The mathematics belongs to the people who built it.",
         fontsize=11, color=MUTED, va="bottom")
fig.text(0.06, 0.004,
         "neuruhai.github.io/neuruh-demos/millennium-lab",
         fontsize=11, color=MUTED, va="bottom")

out = "%s/millennium-field-render.png" % OUT
fig.savefig(out, facecolor=INK)
print("wrote", out)
