import json, random, math
P,K='#f5f5f0','#0d0d0d'
HEAD='''<!doctype html><html><head><meta charset="utf-8">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;700&display=swap" rel="stylesheet">
<style>html,body{margin:0;background:%s}svg{display:block;width:1500px;height:1000px}</style></head><body>
<svg viewBox="0 0 1500 1000" xmlns="http://www.w3.org/2000/svg" font-family="'IBM Plex Mono', monospace">'''
TAIL='</svg></body></html>'
def t(x,y,s,txt,fill=K,anchor='start',w=400,ls=2,op=1):
    return f'<text x="{x}" y="{y}" font-size="{s}" font-weight="{w}" letter-spacing="{ls}" fill="{fill}" text-anchor="{anchor}" opacity="{op}">{txt}</text>'

# A: 40本の試行線
random.seed(3)
x0,x1,y0,y1=80,1420,140,740
def run():
    pts=[];q=0
    for i in range(0,121,4):
        target=14*math.sin(min(i,100)/100*math.pi)**1.3*(1 if i<100 else (120-i)/20)
        q=max(0,q+(target-q)*0.45+random.gauss(0,2.2))
        pts.append((i,q))
    return pts
X=lambda m:x0+(x1-x0)*m/120; Y=lambda v:y1-(y1-y0)*min(v,24)/24
def step(pts):
    s=f'M{X(pts[0][0]):.0f} {Y(pts[0][1]):.0f}'
    for (a,_),(b,v) in zip(pts,pts[1:]): s+=f' H{X(b):.0f} V{Y(v):.0f}'
    return s
runs=[run() for _ in range(40)]
mean=[(runs[0][i][0],sum(r[i][1] for r in runs)/40) for i in range(len(runs[0]))]
a=HEAD%P+f'<rect width="1500" height="1000" fill="{P}"/>'
a+=f'<line x1="{x0}" y1="{y1}" x2="{x1}" y2="{y1}" stroke="{K}" stroke-width="2"/>'
for r in runs: a+=f'<polyline points="{" ".join(f"{X(m):.0f},{Y(v):.0f}" for m,v in r)}" fill="none" stroke="{K}" stroke-width="1.6" opacity=".3"/>'
a+=f'<path d="{step(mean)}" fill="none" stroke="{K}" stroke-width="7"/>'
a+=t(80,96,18,'DOOR WAIT · 40 RUNS PER CONDITION',ls=4)
a+=t(1420,96,18,'0 → 120 MIN',anchor='end',ls=4)
a+=t(80,850,60,'SERVICE ROBOT',w=700,ls=1)+t(80,920,60,'SIMULATION',w=700,ls=1)
a+=t(1420,920,18,'SIMPY · 7,665 CONDITIONS',anchor='end',ls=4)
open('A_runs.html','w').write(a+TAIL)

# B: 操作パネル
b=HEAD%K+f'<rect width="1500" height="1000" fill="{K}"/>'
def chips(y,label,opts,sel):
    s=t(80,y-22,16,label,fill=P,ls=4,op=.7); x=80
    for o in opts:
        w=24+len(o)*19; on=o==sel
        s+=f'<rect x="{x}" y="{y}" width="{w}" height="62" fill="{P if on else "none"}" stroke="{P}" stroke-width="2.5"/>'
        s+=t(x+w/2,y+41,26,o,fill=K if on else P,anchor='middle',w=500,ls=1); x+=w+14
    return s
b+=chips(150,'STAFF',['1','2','3','4','5'],'2')
b+=chips(310,'ROBOTS',['0','1','2','3','4'],'2')
b+=chips(470,'MODE',['SERVE','BUS','PATROL'],'SERVE')
b+=t(80,608,16,'ARRIVALS / HOUR',fill=P,ls=4,op=.7)
b+=f'<line x1="80" y1="650" x2="620" y2="650" stroke="{P}" stroke-width="3"/><rect x="80" y="646" width="300" height="8" fill="{P}"/><circle cx="380" cy="650" r="20" fill="{P}"/>'
b+=f'<line x1="740" y1="140" x2="740" y2="700" stroke="{P}" stroke-width="2" opacity=".4"/>'
b+=t(800,190,18,'DOOR WAIT',fill=P,ls=5)
b+=t(790,470,250,'14.0',fill=P,w=700,ls=-6)
b+=t(1420,540,30,'MIN',fill=P,anchor='end',w=500,ls=4)
b+=t(800,610,22,'3 STAFF, NO ROBOT   7.7 MIN',fill=P,ls=2,op=.6)
b+=f'<line x1="80" y1="770" x2="1420" y2="770" stroke="{P}" stroke-width="2"/>'
b+=t(80,870,60,'SERVICE ROBOT',fill=P,w=700,ls=1)+t(80,940,60,'SIMULATION',fill=P,w=700,ls=1)
b+=t(1420,940,18,'CHANGE THE STORE, SEE THE QUEUE',fill=P,anchor='end',ls=4,op=.7)
open('B_panel.html','w').write(b+TAIL)

# C: IF / THEN
c=HEAD%P+f'<rect width="1500" height="1000" fill="{P}"/>'
c+=f'<rect x="80" y="90" width="92" height="50" fill="{K}"/>'+t(126,126,30,'IF',fill=P,anchor='middle',w=700,ls=2)
c+=t(80,250,92,'3 STAFF → 2 STAFF',w=700,ls=-1)
c+=t(80,360,92,'+ 2 ROBOTS',w=700,ls=-1)
c+=f'<rect x="80" y="440" width="150" height="50" fill="{K}"/>'+t(155,476,30,'THEN',fill=P,anchor='middle',w=700,ls=2)
c+=t(80,620,150,'7.7 → 14.0',w=700,ls=-4)
c+=t(80,690,30,'MIN WAITING AT THE DOOR',w=500,ls=3)
c+=f'<line x1="80" y1="790" x2="1420" y2="790" stroke="{K}" stroke-width="3"/>'
c+=t(80,880,44,'SERVICE ROBOT SIMULATION',w=700,ls=2)
c+=t(80,930,18,'FAMILY RESTAURANT · 35 TABLES · PEAK 2 HOURS',ls=4,op=.6)
open('C_ifthen.html','w').write(c+TAIL)

# D: 7,665条件のドット（実データ：入店待ちの平均でドットの大きさ）
rows=json.load(open('/home/claude/results.json'))['rows']
cols,nrow,pitch=105,73,10
gx,gy=1500-80-cols*pitch,80
d=HEAD%K+f'<rect width="1500" height="1000" fill="{K}"/><g fill="{P}">'
for i,r in enumerate(rows):
    v=min(r['seat_wait_mean'],30)/30
    rad=0.5+4.1*math.sqrt(v)
    d+=f'<circle cx="{gx+(i%cols)*pitch+pitch/2:.1f}" cy="{gy+(i//cols)*pitch+pitch/2:.1f}" r="{rad:.2f}"/>'
d+='</g>'
d+=t(80,120,18,'7,665',fill=P,ls=3,w=700)+t(80,146,14,'CONDITIONS',fill=P,ls=4)
d+=t(80,210,14,'1 DOT = 1 CONDITION',fill=P,ls=3,op=.7)+t(80,234,14,'SIZE = DOOR WAIT',fill=P,ls=3,op=.7)
d+=f'<circle cx="86" cy="280" r="1.5" fill="{P}"/><circle cx="110" cy="280" r="3.5" fill="{P}"/><circle cx="138" cy="280" r="5.2" fill="{P}"/>'
d+=t(80,900,60,'SERVICE ROBOT',fill=P,w=700,ls=1)+t(80,965,60,'SIMULATION',fill=P,w=700,ls=1)
open('D_dots.html','w').write(d+TAIL)
print(gx)
