"""Code-drawn clock and snowball visitors, matching the existing native sprite art."""
from PIL import Image
import math
DIGITS={
 '0':['111','101','101','101','111'],'1':['010','110','010','010','111'],
 '2':['111','001','111','100','111'],'3':['111','001','111','001','111'],
 '4':['101','101','111','001','001'],'5':['111','100','111','001','111'],
 '6':['111','100','111','101','111'],'7':['111','001','010','010','010'],
 '8':['111','101','111','101','111'],'9':['111','101','111','001','111'],
 ':':['000','010','000','010','000'],'/':['001','001','010','100','100'],
 '-':['000','000','111','000','000']}

def winter_assets(Art):
 assets=[];groups={}
 def add(name,im):assets.append((name,im))
 a=Art(57,68)
 # Round oak case, ivory dial and a restrained LCD date panel beneath.
 a.ellipse((1,4,55,58),'#25313a')
 a.ellipse((0,0,54,54),'#795941')
 a.ellipse((2,2,52,52),'#be9b6f')
 a.ellipse((5,5,49,49),'#e7e3ce')
 a.ellipse((6,6,48,48),None,'#c6bda6',.6)
 # Runtime centre is (567,79): sprite origin is (540,51).
 for i in range(60):
  theta=i*math.tau/60;r=20.4;inner=18.4 if i%5==0 else 19.6
  a.line([(27+r*math.sin(theta),28-r*math.cos(theta)),(27+inner*math.sin(theta),28-inner*math.cos(theta))],'#59615c',1 if i%5==0 else .45)
 for txt,x,y in [('12',23.5,11),('3',41,26),('6',25.5,40),('9',11,26)]:
  for ch in txt:
   for yy,row in enumerate(DIGITS[ch]):
    for xx,v in enumerate(row):
     if v=='1':a.rect((x+xx*.8,y+yy*.8,x+xx*.8+.6,y+yy*.8+.6),'#333f41')
   x+=3.2
 a.rect((1,54,54,67),'#795941',3)
 a.rect((3,56,52,65),'#d5dcc7',2)
 groups['wallclock']=len(assets);add('wallclock',a.image())
 groups['clockfont']=len(assets)
 # Exact pixel glyphs keep the date legible at the room's compact scale.
 for index,ch in enumerate('0123456789:/-'):
  im=Image.new('RGBA',(4,6),(0,0,0,0))
  for y,row in enumerate(DIGITS[ch]):
   for x,v in enumerate(row):
    if v=='1':im.putpixel((x,y),(40,57,48,255))
  add('clkfont'+str(index),im)
 groups['snowkid']=len(assets)
 for kind in range(3):
  for direction in range(2):
   for frame in range(8):
    im=child(Art,kind,frame,False)
    if direction:im=im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
    add(f'snowkid{kind}{direction}{frame}',im)
  for frame in range(8):add(f'snowkid{kind}p{frame}',child(Art,kind,frame,True))
 return assets,groups

def child(Art,kind,frame,pose):
 a=Art(32,44)
 coats=['#b95351','#4797a7','#d0a14b'];scarves=['#d9bd72','#bd5752','#5c869a'];skin=['#e5ba93','#bc8e71','#e7c4a3'][kind]
 coat=coats[kind];scarf=scarves[kind];t=frame*math.tau/8
 stride=math.sin(t)*5 if not pose else .8
 bob=abs(math.sin(t))*1.0 if not pose else (frame%2)*.3
 # Boots and bent trouser legs read clearly while they run.
 hip=(16,29-bob)
 a.line([hip,(13+stride*.4,34),(12+stride,40)],'#354756',4)
 a.line([hip,(18-stride*.4,34),(19-stride,40)],'#405968',4)
 a.rect((9+stride,39,16+stride,42),'#303a40',1)
 a.rect((17-stride,39,24-stride,42),'#303a40',1)
 a.rect((10,17-bob,22,31-bob),coat,3)
 a.line([(16,20-bob),(16,30-bob)],'#e2ccb0',.6)
 for yy in [23,27]:a.ellipse((16,yy-bob,17,yy+1-bob),'#f0d9ac')
 # Running arms, then wind-up, release, and a cheeky two-frame wave.
 if not pose:
  left=[(11,20-bob),(8,23),(9-stride*.7,27)]
  right=[(21,20-bob),(24,23),(24+stride*.6,18)]
 elif frame==0:
  left=[(11,20),(8,19),(7,13)]
  right=[(21,20),(25,16),(24,11)]
 elif frame==1:
  left=[(11,20),(7,22),(5,20)]
  right=[(21,20),(24,14),(19,8)]
 else:
  left=[(11,20),(7,25),(10,27)]
  right=[(21,20),(25,14),(24+(frame%2)*3,9+(frame%2)*2)]
 for points in [left,right]:
  a.line(points,coat,3.6);x,y=points[-1];a.ellipse((x-1.9,y-1.8,x+1.9,y+1.8),scarf)
 if pose and frame==0:a.ellipse((21,6,27,12),'#eff7f5')
 # Winter hats, scarves, visible faces and pom-poms.
 a.ellipse((10,6-bob,22,18-bob),skin)
 a.ellipse((9.5,3-bob,22.5,12-bob),scarf)
 a.rect((9,9-bob,23,12-bob),coat,1)
 a.ellipse((13,0-bob,19,5-bob),'#e9e2c9')
 a.ellipse((14,12-bob,15,13.4-bob),'#28383e')
 a.ellipse((19,12-bob,20,13.4-bob),'#28383e')
 a.line([(16,16-bob),(18,16.5-bob),(20,15.5-bob)],'#8c5850',.6)
 a.rect((10,17-bob,23,20-bob),scarf,1)
 a.poly([(20,19-bob),(27,23+stride*.4),(24,26+stride*.3),(18,21-bob)],scarf)
 return a.image()
