"""依頼238：make237.py を写して名前だけ 238 に（台本の c238・出力 closeup238_*・events238.txt など）。依頼237：make235.py を写して名前だけ 237 に。以下は 235 の説明：
(1) closeup235_<場面>.mp4：frames/<場面>/f_*.png に台本の秒の音（mix238_<場面>.wav）を重ねる。早回し（台本 c235.ff・×c235.ff_speed）の所は 4 コマに 1 枚を使い、
    音は無し（声の無い所だけを早回しにしている）、右上に「早回し ×4・実際の時間 m:ss」を出す。⑨-2 は 30 秒になった所から 3 秒、左下に「立ち往生 1件（30 秒以上動けず、人が動かした）」
(2) closeup235_<場面>_top.mp4：frames/<場面>_top に 234 の描き方で床の線・通った跡・印・左上の字を重ねる。床の線は、ものごとに穴を埋めた外形
    （閉じる処理 半径 6 画素 → 中の穴埋め）の中には描かない。もの：ハコブくん（赤）・車いすと茂さん（緑）・大城さん（青）・寺島さん（アルファ）。
    大城さんが両手で握りにのせている間（台本 c235.grip_both）は、大城さんと車いすと茂さんを 1 つのものとして埋める（依頼235）
    重ねる順：コマ → 床の線（外形の所は消す）→ 通った跡 → 印 → 左上の字。早回しの所は (1) と同じコマだけ
動画の終わり：ハコブくんが物の一番後ろを通り過ぎてから 2.5 秒（events238.txt）
使い方: python3 make235.py <Unity の出力フォルダ> <台本> <場面> <音の wav> <動画を置くフォルダ>"""
import csv, json, math, os, re, subprocess, sys, tempfile, wave
import numpy as np, cv2
from scipy import ndimage
from PIL import Image, ImageChops, ImageDraw, ImageFont

FF = "/opt/homebrew/Caskroom/miniforge/base/envs/ros_env/bin/ffmpeg"
D, SC, SHOT, WAV, VD = sys.argv[1:6]
os.makedirs(VD, exist_ok=True)
SJ = json.load(open(SC)); shot = [s for s in SJ["shots"] if s["name"] == SHOT][0]; C = shot["c238"]
FPS, W, H, S = 30, 1920, 1080, 2
ev = open(os.path.join(D, "events238.txt"), encoding="utf-8").read()
t_stop = float(re.search(r"([\d.]+) 秒 止まった（", ev).group(1))
t_res = float(re.search(r"([\d.]+) 秒 走り出す", ev).group(1))
t_pass = float(re.search(r"([\d.]+) 秒 物の一番後ろを通り過ぎた", ev).group(1))
f0 = int(round(C["video_from"] * FPS)); f1 = min(int(round((t_pass + 2.5) * FPS)), int(round(C["video_to"] * FPS)))
SPD = C["ff_speed"]; ffseg = [(int(round(a * FPS)), int(round(b * FPS))) for a, b in C["ff"]]
def in_ff(f):
    for a, b in ffseg:
        if a < f < b: return (a, b)
    return None
seq = [f for f in range(f0, f1 + 1) if in_ff(f) is None or (f - in_ff(f)[0]) % SPD == 0]
FONT_PATH = "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc"
def font(px, s=1): return ImageFont.truetype(FONT_PATH, px * s)
label = C.get("stuck_label")
# 立ち往生の字は「止まってから 30 秒」のコマ（Unity の止まった時刻から）
t30 = t_stop + 30.0 if label else None

# ---------------------------------------------------------------- (1) 本番の動画
tmp = tempfile.mkdtemp(prefix="m238_", dir="/tmp")
FB, FS = font(40), font(30)
info = {"frames": len(seq), "ff_frames": sum(1 for f in seq if in_ff(f)), "label": None}
for k, f in enumerate(seq):
    src = os.path.join(D, "frames", SHOT, f"f_{f:05d}.png"); assert os.path.exists(src), src
    t = f / FPS
    ff = in_ff(f); lab = t30 is not None and t30 - 1e-4 <= t < t30 + 3.0 - 1e-4
    if not ff and not lab:
        os.symlink(os.path.abspath(src), os.path.join(tmp, f"v_{k:05d}.png")); continue
    im = Image.open(src).convert("RGB"); d = ImageDraw.Draw(im)
    if ff:
        real = t - C["video_from"]; s = f"早回し ×{SPD}　実際の時間 {int(real) // 60}:{int(real) % 60:02d}"
        bb = d.textbbox((0, 0), s, font=FB); w = bb[2] - bb[0]
        d.rounded_rectangle([W - w - 70, 24, W - 24, 24 + 72], radius=12, fill=(255, 255, 255))
        d.text((W - w - 47, 60), s, font=FB, fill=(25, 25, 25), anchor="lm")
    if lab:
        if info["label"] is None: info["label"] = t
        s = "立ち往生 1件（30 秒以上動けず、人が動かした）"
        bb = d.textbbox((0, 0), s, font=FB); w = bb[2] - bb[0]
        d.rounded_rectangle([24, H - 24 - 80, 24 + w + 50, H - 24], radius=12, fill=(242, 194, 48))
        d.text((49, H - 24 - 40), s, font=FB, fill=(25, 25, 25), anchor="lm")
    im.save(os.path.join(tmp, f"v_{k:05d}.png"))
# 音：ふだんの所は台本の秒の音、早回しの所は無音（その長さはコマの数 / 30）
wr = wave.open(WAV); SR = wr.getframerate(); x = np.frombuffer(wr.readframes(wr.getnframes()), np.int16)
out = []
for f in seq:
    a = int(round(f / FPS * SR)); b = a + SR // FPS
    out.append(np.zeros(SR // FPS, np.int16) if in_ff(f) else (x[a:b] if b <= len(x) else np.pad(x[a:min(b, len(x))], (0, b - min(b, len(x))))))
aw = os.path.join(tmp, "a.wav"); w2 = wave.open(aw, "w"); w2.setnchannels(1); w2.setsampwidth(2); w2.setframerate(SR); w2.writeframes(np.concatenate(out).tobytes()); w2.close()
vid = os.path.join(VD, f"closeup238_{SHOT}.mp4")
subprocess.run([FF, "-y", "-loglevel", "error", "-framerate", "30", "-i", os.path.join(tmp, "v_%05d.png"), "-i", aw,
                "-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", "18", "-r", "30",
                "-c:a", "aac", "-b:a", "192k", "-ar", "48000", "-ac", "2", "-shortest", vid], check=True)
# 動画の秒 ↔ 台本の秒
vmap = {f: k / FPS for k, f in enumerate(seq)}
json.dump({"seq_first": seq[0], "seq_last": seq[-1], "n": len(seq), "video_s": len(seq) / FPS, "t_stop": t_stop, "t_resume": t_res, "t_pass": t_pass,
           "label_t": info["label"], "t30": t30, "ff": C["ff"], "vmap_keys": {str(f): round(v, 4) for f, v in vmap.items() if f % 15 == 0}},
          open(os.path.join(D, f"vmap238_{SHOT}.json"), "w"), ensure_ascii=False)
print("動画", vid, len(seq), "コマ", len(seq) / FPS, "秒・早回しのコマ", info["ff_frames"], "・立ち往生の字", info["label"])

# ---------------------------------------------------------------- (2) 真上の動画
CAM = json.load(open(os.path.join(D, "top238_cam.json")))
HUD_PX, LINE_LABEL_PX, MARK_LABEL_PX = 70, 36, 40
COL = {"center": (120, 120, 120), "x042": (142, 36, 170), "lane": (46, 173, 107), "chair": (239, 108, 0), "robot": (0, 105, 92)}
LW = {"center": 3, "dash": 5, "trail": 10, "mark_r": 13, "mark_edge": 3, "leader": 3}
DASH, GAP = 28, 16
ox, oy = CAM["px_origin"]; xx, xy = CAM["px_per_x"]; zx, zy = CAM["px_per_z"]
def to_px(x, z): return (ox + xx * x + zx * z, oy + xy * x + zy * z)
def from_px(px, py):
    det = xx * zy - zx * xy; dx, dy = px - ox, py - oy
    return ((dx * zy - zx * dy) / det, (xx * dy - dx * xy) / det)
rows = {int(r["frame"]): r for r in csv.DictReader(open(os.path.join(D, "frames238.csv")))}
top = {int(r["frame"]): r for r in csv.DictReader(open(os.path.join(D, "top238.csv")))}
man = C["man"]
MARKS = [("chair", man["t0"], "寄せ始め"), ("chair", man["t1"], "寄せ終わり"), ("robot", t_stop, "止まった"), ("robot", t_res, "走り出し")]
mark_f = [int(round(t * FPS)) for _, t, _ in MARKS]
FL, FM, FH = font(LINE_LABEL_PX, S), font(MARK_LABEL_PX, S), font(HUD_PX, S)
def sc(p): return (p[0] * S, p[1] * S)
def text_box(d, xy, s, f, fg, anchor, pad=8, bg=(255, 255, 255, 235), edge=None):
    x, y = xy; bb = d.textbbox((x, y), s, font=f, anchor=anchor)
    d.rounded_rectangle([bb[0] - pad * S, bb[1] - pad * S, bb[2] + pad * S, bb[3] + pad * S], radius=6 * S, fill=bg, outline=edge, width=2 * S if edge else 0)
    d.text((x, y), s, font=f, fill=fg, anchor=anchor)
def dashed(d, p0, p1, col, w):
    (x0, y0), (x1, y1) = sc(p0), sc(p1); L = math.hypot(x1 - x0, y1 - y0); ux, uy = (x1 - x0) / L, (y1 - y0) / L; s = 0.0
    while s < L:
        e = min(L, s + DASH * S); d.line([(x0 + ux * s, y0 + uy * s), (x0 + ux * e, y0 + uy * e)], fill=col, width=w * S); s += (DASH + GAP) * S
def down(layer): return layer.resize((W, H), Image.LANCZOS)
z_lo, z_hi = from_px(0, 0)[1], from_px(W, 0)[1]
LINES = {"center": 0.0, "x042": 0.42, "lane": -0.33}
line_py = {k: to_px(x, z_lo)[1] for k, x in LINES.items()}
st = Image.new("RGBA", (W * S, H * S), (0, 0, 0, 0)); d = ImageDraw.Draw(st)
d.line([sc(to_px(0.0, z_lo)), sc(to_px(0.0, z_hi))], fill=COL["center"] + (255,), width=LW["center"] * S)
for k in ("x042", "lane"): dashed(d, to_px(LINES[k], z_lo), to_px(LINES[k], z_hi), COL[k] + (255,), LW["dash"])
text_box(d, sc((14, line_py["center"] - 8)), "真ん中 0.00", FL, COL["center"], "lb", edge=COL["center"])
text_box(d, sc((14, line_py["x042"] + 8)), "寄せたあと +0.42", FL, COL["x042"], "lt", edge=COL["x042"])
text_box(d, sc((W - 14, line_py["lane"] - 8)), "ハコブくんのふだん −0.33", FL, COL["lane"], "rb", edge=COL["lane"])
static = down(st); static_A = static.getchannel("A"); static_An = np.array(static_A)
rail_lo_py = to_px(0.875, 0)[1]; rail_hi_py = to_px(-0.875, 0)[1]
def mark_layer(shown):
    L = Image.new("RGBA", (W * S, H * S), (0, 0, 0, 0)); d = ImageDraw.Draw(L)
    for i, (who, t, head) in enumerate(MARKS):
        if not shown[i]: continue
        r = top[mark_f[i]]
        px, py = (float(r["axle_px"]), float(r["axle_py"])) if who == "chair" else (float(r["robot_px"]), float(r["robot_py"]))
        if who == "chair": ly = rail_lo_py + 40 + (70 if head == "寄せ終わり" else 0); anchor = "mt"
        else: ly = rail_hi_py - 40 - (60 if head == "走り出し" else 0); anchor = "mb"
        d.line([sc((px, py)), sc((px, ly))], fill=COL[who] + (255,), width=LW["leader"] * S)
        R = LW["mark_r"] * S; e = LW["mark_edge"] * S
        d.ellipse([px * S - R - e, py * S - R - e, px * S + R + e, py * S + R + e], fill=(255, 255, 255, 255))
        d.ellipse([px * S - R, py * S - R, px * S + R, py * S + R], fill=COL[who] + (255,))
        tv = vmap.get(mark_f[i]); tv = tv if tv is not None else vmap[min(vmap, key=lambda q: abs(q - mark_f[i]))]
        text_box(d, sc((px, ly)), f"{head} {tv:.1f}秒", FM, COL[who], anchor, edge=COL[who])
    return down(L)
def num_ok(s):
    try: v = float(s)
    except ValueError: return None
    return v if math.isfinite(v) and abs(v) < 1e6 else None
def hud_lines(f):
    r = rows[f]; cx = num_ok(r["chair_x"]); cy = num_ok(r["chair_yaw"]); g = num_ok(r["gap_m"])
    s0 = f"{vmap[f]:.1f} 秒" + (f"（早回し ×{SPD}）" if in_ff(f) else "")
    return [s0, "車いすの横の位置 " + (f"x {cx * 100:+.1f} cm・向き {cy:+.0f}°" if cx is not None else "—"),
            "ハコブくんと車いす・人のすき間 " + (f"{g:.2f} m" if g is not None and g < 1.5 else "1.5 m より遠い")]
RC = 6
KER = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * RC + 1, 2 * RC + 1)); PAD = RC + 24
def fill_obj(soft):
    ys, xs = np.nonzero(soft); added = np.zeros(soft.shape, bool)
    if len(xs) == 0: return added
    y0, y1 = max(0, ys.min() - PAD), min(H, ys.max() + PAD + 1); x0, x1 = max(0, xs.min() - PAD), min(W, xs.max() + PAD + 1)
    b = (soft[y0:y1, x0:x1] > 0).astype(np.uint8)
    bp = cv2.copyMakeBorder(b, PAD, PAD, PAD, PAD, cv2.BORDER_CONSTANT, value=0)
    cl = cv2.morphologyEx(bp, cv2.MORPH_CLOSE, KER, borderType=cv2.BORDER_CONSTANT, borderValue=0)[PAD:-PAD, PAD:-PAD].astype(bool)
    filled = ndimage.binary_fill_holes(cl | b.astype(bool))
    added[y0:y1, x0:x1] = filled & ~b.astype(bool)
    return added
gb = C["grip_both"]
tmpT = tempfile.mkdtemp(prefix="m238t_", dir="/tmp")
trail = Image.new("RGBA", (W * S, H * S), (0, 0, 0, 0)); td = ImageDraw.Draw(trail)
prev = {"chair": None, "robot": None}; shown = [False] * len(MARKS); mcache = mark_layer(shown)
per = []; top_stills = {}
want = {int(round(s[0] * FPS)): s[2] for s in shot["stills"] if s[1] == "top"}
k = 0
seqset = set(seq)
for f in range(0, f1 + 1):
    r = top[f]
    for who, kx, ky in (("chair", "axle_px", "axle_py"), ("robot", "robot_px", "robot_py")):
        p = (round(float(r[kx]) * S) / S, round(float(r[ky]) * S) / S)
        if prev[who] is not None: td.line([sc(prev[who]), sc(p)], fill=COL[who] + (255,), width=LW["trail"] * S)
        R = LW["trail"] * S / 2; td.ellipse([p[0] * S - R, p[1] * S - R, p[0] * S + R, p[1] * S + R], fill=COL[who] + (255,))
        prev[who] = p
    if f not in seqset: continue
    changed = False
    for i in range(len(MARKS)):
        if not shown[i] and f >= mark_f[i]: shown[i] = True; changed = True
    if changed: mcache = mark_layer(shown)
    raw = Image.open(os.path.join(D, "frames", SHOT + "_top", f"f_{f:05d}.png")).convert("RGBA")
    mkn = np.array(Image.open(os.path.join(D, "frames", SHOT + "_top_mask", f"f_{f:05d}.png")).convert("L"))
    ob = np.array(Image.open(os.path.join(D, "frames", SHOT + "_top_obj", f"f_{f:05d}.png")).convert("RGBA"))
    joined = gb[min(f, len(gb) - 1)] == 1
    groups = [ob[:, :, 0], np.maximum(ob[:, :, 1], ob[:, :, 2]), ob[:, :, 3]] if joined else [ob[:, :, 0], ob[:, :, 1], ob[:, :, 2], ob[:, :, 3]]
    add_any = np.zeros((H, W), bool)
    for g in groups: add_any |= fill_obj(g)
    Fn = np.maximum(mkn, np.where(add_any, 255, 0).astype(np.uint8))
    st_m = static.copy(); st_m.putalpha(ImageChops.multiply(static_A, ImageChops.invert(Image.fromarray(Fn, "L"))))
    lines = hud_lines(f)
    hl = Image.new("RGBA", (W * S, H * S), (0, 0, 0, 0)); hd = ImageDraw.Draw(hl); x0, y0, lh = 30, 22, 88
    wmax = max(hd.textbbox((0, 0), s, font=FH)[2] for s in lines) / S
    hd.rounded_rectangle([x0 * S - 8 * S, y0 * S - 6 * S, (x0 + wmax + 28) * S, (y0 + lh * 3 + 6) * S], radius=10 * S, fill=(255, 255, 255, 235))
    for i, s in enumerate(lines): hd.text(((x0 + 10) * S, (y0 + i * lh) * S), s, font=FH, fill=(25, 25, 25))
    im = raw.copy(); im.alpha_composite(st_m); im.alpha_composite(down(trail)); im.alpha_composite(mcache); im.alpha_composite(down(hl))
    o = im.convert("RGB"); o.save(os.path.join(tmpT, f"v_{k:05d}.png")); k += 1
    stA = np.array(st_m.getchannel("A"))
    per.append((f, int(joined), int(((stA > 0) & (Fn == 255)).sum()), int(add_any.sum())))
    if f in want: top_stills[want[f]] = o
vidT = os.path.join(VD, f"closeup238_{SHOT}_top.mp4")
subprocess.run([FF, "-y", "-loglevel", "error", "-framerate", "30", "-i", os.path.join(tmpT, "v_%05d.png"), "-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", "18", "-r", "30", vidT], check=True)
sd = os.path.join(VD, "stills"); os.makedirs(sd, exist_ok=True)
for nm, im in top_stills.items(): im.save(os.path.join(sd, f"{SHOT}_{nm}.png"))
open(os.path.join(D, f"per238_{SHOT}.csv"), "w").write("frame,joined,line_left_inside,added\n" + "".join(",".join(map(str, p)) + "\n" for p in per))
print("真上", vidT, k, "コマ・外形の中に残る線 合計", sum(p[2] for p in per), "・ひとまとめのコマ", sum(p[1] for p in per))
