import sys,math
from PIL import Image, ImageDraw, ImageFont
# usage: blueline_image.py en|sv out.png distance
R=int(sys.argv[3]); lang=sys.argv[1]; outp=sys.argv[2]
import os
c=Image.open(os.path.join(os.path.dirname(os.path.abspath(__file__)),'data','rink_stitch.png')).convert('RGB'); W,H=c.size; p=c.load()
CX,GL=145,46
ICE={(172,255,255),(206,255,255),(144,206,255),(116,144,206)}
def mix(a,b,t): return tuple(int(a[i]*(1-t)+b[i]*t) for i in range(3))
for sy in range(H):
  for sx in range(W):
    if p[sx,sy] not in ICE: continue
    x=sx-CX; y=261-(sy-GL)
    if y>261: continue
    d=math.hypot(x,264-y)
    p[sx,sy]=mix(p[sx,sy],(30,190,50),0.45) if d>=R else mix(p[sx,sy],(255,140,30),0.42)
c=c.crop((4,0,290,H))
S=3; big=c.resize((c.width*S,c.height*S),Image.NEAREST); d=ImageDraw.Draw(big)
pts=[]
for a10 in range(-900,901,3):
  a=math.radians(a10/10); x=R*math.sin(a); y=264-R*math.cos(a)
  pts.append(((x+CX-4)*S,(GL+261-y)*S))
d.line(pts,fill=(20,20,20),width=10); d.line(pts,fill=(255,225,0),width=5)
f=ImageFont.truetype('/usr/share/fonts/TTF/DejaVuSans-Bold.ttf',26)
fs=ImageFont.truetype('/usr/share/fonts/TTF/DejaVuSans.ttf',22)
def label(xy,txt,font,bg):
  l,t,r,b=d.textbbox(xy,txt,font=font); d.rectangle((l-8,t-6,r+8,b+6),fill=bg,outline=(20,20,20),width=2); d.text(xy,txt,font=font,fill=(255,255,255))
T={'en':['GREEN: 15 % chance the goalie misses','ORANGE: unchanged','blue line','Blue-line shots in NHL 94 (v5.1W patch)',
 [f'Yellow line = distance {R} from the net. From the green','zone the game rolls once per shot whether the goalie','misses (15 %). The puck must still go under the bar.']],
 'sv':['GRÖNT: 15 % chans att målvakten missar','ORANGE: som vanligt','blålinjen','Blålinjeskott i NHL 94 (v5.1W, patch 2026-10-21)',
 [f'Gul linje = avstånd {R} från målet. Skjuter du från det gröna','slår spelet en gång per skott om målvakten missar pucken (15 %).','Pucken måste ändå gå under ribban och mellan stolparna.']]}[lang]
label((50,622),T[0],f,(30,140,45))
label((215,330),T[1],f,(200,100,15))
label((40,668),T[2],fs,(40,70,160))
pan=Image.new('RGB',(big.width,170),(25,30,40)); dp=ImageDraw.Draw(pan)
dp.text((20,14),T[3],font=f,fill=(255,230,0))
for i,l in enumerate(T[4]): dp.text((20,56+i*34),l,font=fs,fill=(235,235,235))
out=Image.new('RGB',(big.width,big.height+pan.height)); out.paste(big,(0,0)); out.paste(pan,(0,big.height))
out.save(outp)
