"""Code-drawn native sprites for the sleigh, room visit and original cookie maze."""
from pathlib import Path
from PIL import Image,ImageDraw
import math
FONT={
'A':'0E11111F111111','B':'1E11111E11111E','C':'0F10101010100F','D':'1E11111111111E','E':'1F10101E10101F','F':'1F10101E101010','G':'0F10101711110F','H':'1111111F111111','I':'0E04040404040E','J':'0702020212120C','K':'11121418141211','L':'1010101010101F','M':'111B1515111111','N':'11191513111111','O':'0E11111111110E','P':'1E11111E101010','Q':'0E11111115120D','R':'1E11111E141211','S':'0F10100E01011E','T':'1F040404040404','U':'1111111111110E','V':'11111111110A04','W':'11111115151B11','X':'11110A040A1111','Y':'11110A04040404','Z':'1F01020408101F',
'0':'0E11131519110E','1':'040C040404040E','2':'0E11010204081F','3':'1E01010601011E','4':'02060A121F0202','5':'1F10101E01011E','6':'0E10101E11110E','7':'1F010204080808','8':'0E11110E11110E','9':'0E11110F01010E',
' ':'00000000000000',"'":'06060400000000','/':'01010204081010',':':'00040400040400','.':'00000000000606',',':'00000000000604','!':'04040404040004','?':'0E110102040004','-':'0000001F000000','+':'0004041F040400','(':'02040808080402',')':'08040202020408','=':'00001F001F0000','<':'02040810080402','>':'08040201020408',';':'00040400040408','"':'0A0A0A00000000','#':'0A0A1F0A1F0A0A','%':'19190204081313','&':'0C12140C15120D','*':'00150E1F0E1500','@':'0E11171517100E','[':'0E08080808080E',']':'0E02020202020E','_':'0000000000001F','\\':'10100804020101','$':'040F140E051E04','^':'040A1100000000'}
def pixeltext(im,text,x,y,scale=1,col='#fff2cf'):
 d=ImageDraw.Draw(im)
 for ch in text.upper():
  rows=FONT.get(ch,FONT['?'])
  for yy in range(7):
   bits=int(rows[yy*2:yy*2+2],16)
   for xx in range(5):
    if bits&(16>>xx):d.rectangle((x+xx*scale,y+yy*scale,x+(xx+1)*scale-1,y+(yy+1)*scale-1),fill=col)
  x+=6*scale

def extra_assets(Art,dog,root):
 assets=[];groups={}
 def group(name,images):
  groups[name]=len(assets)
  for i,im in enumerate(images):assets.append((name+str(i),im))
 a=Art(40,26);a.line([(2,22),(32,22),(38,18)],'#f6d68b',2);a.line([(9,16),(8,22),(30,22),(29,16)],'#cfa655',1.5)
 a.poly([(1,2),(8,3),(12,11),(32,11),(36,5),(39,5),(37,17),(8,18),(3,14)],'#b83034');a.line([(3,3),(11,12),(32,12),(37,6)],'#ffe1a3',1.2);a.rect((10,7,23,13),'#417c61',1);a.rect((16,7,18,13),'#f5d580')
 group('sleigh',[a.image()])
 deer=[]
 for p in range(6):
  a=Art(24,24);stride=math.sin(p*math.pi/2)*3
  a.ellipse((2,9,17,17),'#ae7844');a.poly([(14,12),(16,4),(21,5),(21,12)],'#b68954');a.ellipse((17,3,23,9),'#c49a65');a.ellipse((21,7,24,9),'#fa665a')
  a.line([(18,5),(16,0),(14,2)],'#ecd5b0',1);a.line([(20,4),(22,0),(23,2)],'#ecd5b0',1)
  a.line([(5,15),(4+stride,21),(7+stride,21)],'#6c492f',1.4);a.line([(13,15),(14-stride,21),(17-stride,21)],'#6c492f',1.4)
  a.line([(3,12),(0,9)],'#c49a65',1.5);a.ellipse((19,5,21,7),'#211f25');a.rect((15,9,20,11),'#bb3940')
  im=a.image()
  if p==4:
   d=ImageDraw.Draw(im);d.ellipse((18,4,21,7),fill='white');d.point((20,5),fill='#222233')
  if p==5:im=im.rotate(65,resample=Image.Resampling.BICUBIC)
  deer.append(im)
 group('deer',deer)
 def bigsanta(p):
  a=Art(48,88);nod=8 if p in (8,9) else 0
  stride=math.sin(p*math.pi/4)*5 if p<8 else 0
  a.ellipse((22,27,47,63),'#866041');a.line([(27,29),(38,25),(43,31)],'#cda963',2)
  a.rect((9+stride,67,20+stride,86),'#302c38',3);a.rect((25-stride,67,38-stride,86),'#302c38',3)
  a.rect((8,36+nod,39,72),'#c93943',8);a.rect((8,67,39,74),'#f8ebd2',2)
  a.rect((8,55,39,61),'#30313c');a.rect((21,54,28,62),'#e8bc69',1);a.rect((23,56,26,60),'#30313c')
  a.ellipse((12,11+nod,36,37+nod),'#eab68d');a.ellipse((11,21+nod,37,47+nod),'#fbefda');a.ellipse((16,17+nod,32,30+nod),'#eab68d')
  a.poly([(10,16+nod),(18,1+nod),(30,3+nod),(39,18+nod),(29,12+nod)],'#cd3c43');a.rect((10,13+nod,34,19+nod),'#fff4e0',2);a.ellipse((35,15+nod,42,22+nod),'#fff4e0')
  a.ellipse((18,22+nod,20,24+nod),'#292f39');a.ellipse((27,22+nod,29,24+nod),'#292f39');a.ellipse((22,25+nod,27,29+nod),'#dc8e79')
  if p==10:a.line([(11,45),(8,32),(21,30)],'#cd3c43',8);a.line([(21,31),(22,25)],'#f1bd96',3)
  elif p in (8,9):a.line([(10,46),(5,61),(22+p-8,73)],'#ca3941',8);a.ellipse((19,68,28,75),'#f2c4a2')
  else:a.line([(10,43),(5,54+stride),(9,62+stride)],'#d7474a',8);a.ellipse((5,58+stride,13,65+stride),'#fff0d7')
  return a.image()
 large=[bigsanta(p) for p in range(11)]
 large += [bigsanta(0).resize((max(6,round(48*(1-n/27))),max(11,round(88*(1-n/27)))),Image.Resampling.LANCZOS) for n in range(24)]
 group('santa',large)
 small=Image.new('RGBA',(26,26));small.alpha_composite(bigsanta(0).resize((13,24),Image.Resampling.LANCZOS),(5,1))
 group('smallsanta',[small]+[small.rotate(n*45,resample=Image.Resampling.BICUBIC) for n in range(8)])
 gifts=[]
 for c in ['#bc424d','#418d89','#7454a5']:
  a=Art(18,18);a.rect((1,5,17,17),c,1);a.rect((7,5,10,17),'#ffe2a1');a.rect((1,8,17,10),'#ffe2a1');a.arc((3,0,9,7),0,360,'#ffe2a1',1.4);a.arc((8,0,14,7),0,360,'#ffe2a1',1.4);gifts.append(a.image())
 group('delivery',gifts)
 dogs=[]
 for p in range(18):
  a=Art(102,80);a.ellipse((20,33,91,69),'#8c6045');a.ellipse((29,35,86,66),'#ad8057');a.ellipse((58,50,95,70),'#967049');a.arc((51,46,95,70),330,200,'#c29b6c',5)
  rise=min(1,p/5);dy=21-18*rise+(3*math.sin((p-6)*math.pi/3) if p>=6 else 0)
  a.ellipse((11,21+dy,43,46+dy),'#ba9063');a.ellipse((10,20+dy,24,43+dy),'#694b3b');a.ellipse((33,21+dy,45,43+dy),'#71513c');a.ellipse((20,36+dy,44,49+dy),'#d2b38a');a.ellipse((30,35+dy,38,40+dy),'#3e3530')
  for ex in (26,37):
   a.ellipse((ex-3,29+dy,ex+3,34+dy),'#ead8b6');a.ellipse((ex-1,28+dy,ex+1,32+dy),'#352e26')
  a.ellipse((40,63,62,70),'#bc976c');a.ellipse((44,66,66,72),'#c4a279');dogs.append(a.image())
 group('dogwatch',dogs)
 a=Art(64,24);a.rect((0,0,63,20),'#fff1d6',6);a.poly([(9,18),(4,24),(20,18)],'#fff1d6');im=a.image();pixeltext(im,'SHHH...',11,7,col='#60463b');group('shhh',[im])
 maze=(Path(root)/'Maze').read_text().splitlines()
 a=Art(600,390,'#101d2a');a.rect((12,9,587,380),None,12,'#356858',1)
 for x in range(25,580,18):a.line([(x,12),(x+9,18),(x+18,12)],'#587f65',2)
 for x in range(33,580,36):a.ellipse((x,13,x+3,16),'#f3c17f')
 for y,row in enumerate(maze):
  for x,c in enumerate(row):
   if c=='#':
    px=57+x*18;py=56+y*18;a.rect((px+1,py+1,px+16,py+16),'#28574f',3);a.line([(px+4,py+3),(px+13,py+3)],'#5e9380',1)
 im=a.image();pixeltext(im,"SANTA'S COOKIE MAZE",192,23,scale=2,col='#ffdc97');pixeltext(im,'SCORE',58,42,col='#8ec9bb');pixeltext(im,'LEVEL',244,42,col='#8ec9bb');pixeltext(im,'LIVES',403,42,col='#8ec9bb');pixeltext(im,'K KEYS  P PAUSE  N NEW GAME  Q/ESC ROOM',180,372,col='#b5d4cb')
 # Prompt is fitted inside the 600-pixel artboard.
 group('gameboard',[im])
 a=Art(7,7);a.ellipse((0,0,6,6),'#e5b779');a.ellipse((1,1,5,5),'#f6cd92');a.ellipse((1,2,2,3),'#785039');a.ellipse((4,1,5,2),'#785039');a.ellipse((3,4,4,5),'#785039');group('cookie',[a.image()])
 a=Art(13,16);a.poly([(2,1),(11,1),(10,14),(3,14)],'#c1e8ed');a.poly([(3,5),(10,5),(9,13),(4,13)],'#fff8e6');a.line([(2,1),(11,1)],'#f6ffff');a.line([(3,2),(4,12)],'#e8fcff');group('milk',[a.image()])
 players=[]
 for direction in range(4):
  for phase in range(2):
   im=bigsanta(phase*2).resize((14,22),Image.Resampling.LANCZOS);canvas=Image.new('RGBA',(18,22));canvas.alpha_composite(im,(2,0));d=ImageDraw.Draw(canvas)
   dx,dy=[(-1,0),(1,0),(0,-1),(0,1)][direction];d.ellipse((8+dx*3,6+dy*2,9+dx*3,7+dy*2),fill='#231e2b');players.append(canvas)
 group('gamesanta',players)
 elves=[]
 for kind in range(5):
  for phase in range(2):
   a=Art(18,22);col=['#d35165','#e5b451','#719ad9','#9b7bc3','#6bd7e2'][kind];a.rect((4,10,14,18),col,3);a.poly([(2,7),(8,0),(15,5),(17,7)],col);a.ellipse((4,5,14,12),'#f5c99c' if kind<4 else '#d4f8f7');a.poly([(1,7),(5,8),(4,11)],'#e6ae81');a.poly([(13,8),(17,7),(14,11)],'#e6ae81');a.ellipse((6,7,7,8),'#203443');a.ellipse((11,7,12,8),'#203443');a.line([(5,18),(3+phase*2,21),(7,21)],col,2);a.line([(12,18),(15-phase*2,21),(11,21)],col,2);a.line([(6,15),(12,15)],'#ffe0a0',1);elves.append(a.image())
 group('elf',elves)
 glyphs=[]
 for n in range(32,96):
  im=Image.new('RGBA',(6,8));pixeltext(im,chr(n),0,0,col='#f9edc8');glyphs.append(im)
 group('gamefont',glyphs)
 return assets,groups
