"""The "Classique" skin of WinZik: the classic look of the MP3 players of 1999 (a slate blue-grey case, title bars
with golden grooves, black LCDs with green digits, light grey bevelled buttons, a green-on-black play list), drawn
here pixel by pixel from a description of that style (no picture of another program is copied, no logo).
Run: python3 tools/skins/classique.py assets/skins/classique
"""
import os
import sys
from PIL import Image, ImageDraw, ImageFont

OUT = sys.argv[1] if len(sys.argv) > 1 else 'assets/skins/classique'
os.makedirs(OUT, exist_ok=True)

METAL = (40, 40, 64)
METAL2 = (60, 60, 92)
LIGHT = (120, 120, 160)
DARK = (14, 14, 26)
LCD = (0, 0, 0)
LCD_GRID = (0, 32, 0)
GREEN = (0, 215, 0)
GOLD = (235, 223, 98)
FACE = (167, 167, 171)
FACE_HI = (222, 222, 228)
FACE_LO = (84, 84, 100)
INK = (24, 24, 32)


def font(size):
    try:
        return ImageFont.load_default(size=size)
    except TypeError:
        return ImageFont.load_default()


def new(w, h, color=(0, 0, 0, 0)):
    return Image.new('RGBA', (w, h), color)


def save(im, name):
    im.save(os.path.join(OUT, name + '.png'))


def metal(im, box=None):
    """Brushed metal: a vertical gradient with fine horizontal lines."""
    d = ImageDraw.Draw(im)
    x0, y0, x1, y1 = box or (0, 0, im.width, im.height)
    for y in range(y0, y1):
        t = (y - y0) / max(1, y1 - y0 - 1)
        c = tuple(int(METAL2[i] * (1 - t) + METAL[i] * t) for i in range(3))
        d.line([(x0, y), (x1 - 1, y)], fill=c)


def bevel(d, x, y, w, h, raised=True, face=FACE):
    d.rectangle([x, y, x + w - 1, y + h - 1], fill=face)
    hi, lo = (FACE_HI, FACE_LO) if raised else (FACE_LO, FACE_HI)
    d.line([(x, y), (x + w - 1, y)], fill=hi)
    d.line([(x, y), (x, y + h - 1)], fill=hi)
    d.line([(x, y + h - 1), (x + w - 1, y + h - 1)], fill=lo)
    d.line([(x + w - 1, y), (x + w - 1, y + h - 1)], fill=lo)
    d.rectangle([x - 1, y - 1, x + w, y + h], outline=DARK)


def sunken(d, x, y, w, h, fill=LCD, grid=False):
    d.rectangle([x, y, x + w - 1, y + h - 1], fill=fill)
    d.line([(x - 1, y - 1), (x + w, y - 1)], fill=DARK)
    d.line([(x - 1, y - 1), (x - 1, y + h)], fill=DARK)
    d.line([(x - 1, y + h), (x + w, y + h)], fill=LIGHT)
    d.line([(x + w, y - 1), (x + w, y + h)], fill=LIGHT)
    if grid:
        for gy in range(y + 1, y + h, 2):
            for gx in range(x + 1, x + w, 2):
                d.point((gx, gy), fill=LCD_GRID)


def text(d, xy, s, size=8, fill=INK, anchor='la'):
    d.fontmode = '1'
    d.text(xy, s, font=font(size), fill=fill, anchor=anchor)


def glyph(d, kind, cx, cy, color=INK):
    """The transport symbols, drawn with polygons."""
    if kind == 'prev':
        d.rectangle([cx - 6, cy - 4, cx - 5, cy + 4], fill=color)
        d.polygon([(cx + 1, cy - 4), (cx + 1, cy + 4), (cx - 4, cy)], fill=color)
        d.polygon([(cx + 6, cy - 4), (cx + 6, cy + 4), (cx + 1, cy)], fill=color)
    elif kind == 'next':
        d.polygon([(cx - 6, cy - 4), (cx - 6, cy + 4), (cx - 1, cy)], fill=color)
        d.polygon([(cx - 1, cy - 4), (cx - 1, cy + 4), (cx + 4, cy)], fill=color)
        d.rectangle([cx + 5, cy - 4, cx + 6, cy + 4], fill=color)
    elif kind == 'play':
        d.polygon([(cx - 3, cy - 5), (cx - 3, cy + 5), (cx + 4, cy)], fill=color)
    elif kind == 'pause':
        d.rectangle([cx - 4, cy - 4, cx - 2, cy + 4], fill=color)
        d.rectangle([cx + 2, cy - 4, cx + 4, cy + 4], fill=color)
    elif kind == 'stop':
        d.rectangle([cx - 4, cy - 4, cx + 4, cy + 4], fill=color)
    elif kind == 'eject':
        d.polygon([(cx - 5, cy + 1), (cx + 5, cy + 1), (cx, cy - 5)], fill=color)
        d.rectangle([cx - 5, cy + 3, cx + 5, cy + 4], fill=color)


def button(w, h, kind=None, label=None, size=8, face=FACE, color=INK):
    im = new(w, h)
    d = ImageDraw.Draw(im)
    bevel(d, 1, 1, w - 2, h - 2, face=face)
    if kind:
        glyph(d, kind, w // 2, h // 2, color)
    if label:
        text(d, (w // 2, h // 2), label, size, color, 'mm')
    return im


# --- the player (main.bmp, 258 x 128: 99 shown, 128 opened) -----------------------------------------------------
main = new(258, 128, METAL + (255,))
metal(main)
d = ImageDraw.Draw(main)
d.rectangle([0, 0, 257, 127], outline=DARK)
d.line([(1, 1), (256, 1)], fill=LIGHT)
# the gold title stripe behind the title LCD
for y in range(3, 12, 2):                         # the golden grooves of the title bar
    d.line([(26, y), (231, y)], fill=GOLD)
    d.line([(26, y + 1), (231, y + 1)], fill=(120, 110, 40))
sunken(d, 47, 1, 163, 12)                       # the scrolling title (Text4)
sunken(d, 27, 17, 197, 12, grid=True)           # the bargraph
sunken(d, 0, 31, 70, 13)                        # "Volume : xx %"
sunken(d, 112, 31, 32, 13)                      # kbps
sunken(d, 186, 31, 70, 13)                      # "Balance : x %"
sunken(d, 0, 81, 56, 17, grid=True)             # the counter
text(d, (252, 88), 'WINZIK', 8, GOLD, 'rm')
# the bottom part (opened with the hidden button): a brand plate and a speaker grid
d.line([(4, 101), (253, 101)], fill=DARK)
d.line([(4, 102), (253, 102)], fill=LIGHT)
for x in range(8, 120, 4):
    for y in range(107, 124, 4):
        d.ellipse([x, y, x + 2, y + 2], fill=DARK)
sunken(d, 126, 107, 126, 16, fill=(30, 32, 44))
save(main, 'main')

# the buttons of the player
save(button(25, 17, label='H', face=FACE), 'hd')
save(button(25, 17, label='H', face=(120, 230, 120)), 'he')
save(button(25, 17, label='?', face=FACE), 'info')
save(button(15, 15, label='_', face=FACE), 'petit')
save(button(17, 15, label='x', face=FACE), 'close')
tray = button(17, 15)
ImageDraw.Draw(tray).polygon([(4, 5), (12, 5), (8, 10)], fill=INK)
save(tray, 'tray')
save(button(17, 15, label='L', face=FACE), 'lyr')
for name, kind in [('barr', 'prev'), ('bpause', 'pause'), ('bplay', 'play'), ('bstop', 'stop'), ('bav', 'next'), ('bpl', None)]:
    im = button(25, 18, kind)
    if name == 'bpl':
        dd = ImageDraw.Draw(im)
        text(dd, (12, 9), 'PL', 8, INK, 'mm')
    save(im, name)

# the sliders: the volume groove goes from green to red, the balance groove is green at the middle
def groove(w, h, colors):
    im = new(w, h)
    dd = ImageDraw.Draw(im)
    sunken(dd, 1, 2, w - 2, h - 4, fill=(10, 12, 16))
    for x in range(3, w - 3):
        t = (x - 3) / (w - 7)
        c = colors(t)
        dd.line([(x, 4), (x, h - 5)], fill=c)
    return im

save(groove(116, 10, lambda t: (int(255 * t), int(200 - 60 * t), 0)), 'fbson')
save(groove(116, 10, lambda t: (int(255 * abs(t - .5) * 2), int(200 - 120 * abs(t - .5)), 0)), 'fbbal')
save(groove(255, 10, lambda t: (0, 60, 0)), 'fpos')
for name, face in [('bson', FACE), ('bbal', FACE), ('bpos', GOLD)]:
    im = new(17, 9)
    dd = ImageDraw.Draw(im)
    bevel(dd, 1, 1, 15, 7, face=face)
    dd.line([(8, 2), (8, 6)], fill=FACE_LO)
    save(im, name)

# --- the play list (mainpl.bmp, 258 x 362) ----------------------------------------------------------------------
pl = new(258, 362, METAL + (255,))
metal(pl)
d = ImageDraw.Draw(pl)
d.rectangle([0, 0, 257, 361], outline=DARK)
for y in range(35, 44, 2):
    d.line([(30, y), (227, y)], fill=GOLD)
    d.line([(30, y + 1), (227, y + 1)], fill=(120, 110, 40))
d.rectangle([84, 34, 174, 44], fill=METAL)
text(d, (129, 39), 'WINZIK PLAYLIST', 8, FACE_HI, 'mm')
sunken(d, 1, 48, 247, 300, fill=(0, 0, 0))
sunken(d, 249, 48, 8, 296, fill=(10, 12, 16))
save(pl, 'mainpl')
for name, label in [('plp', '+'), ('plm', '-'), ('plload', 'L'), ('plsave', 'S')]:
    save(button(25, 17, label=label, size=10), name)
rem = button(25, 17)
dd = ImageDraw.Draw(rem)
dd.rectangle([9, 6, 15, 13], outline=INK)
dd.line([(8, 5), (16, 5)], fill=INK)
dd.line([(11, 4), (13, 4)], fill=INK)
save(rem, 'plrem')
save(button(82, 16, label='MODE EDITION', size=8), 'pledit')
save(button(82, 16, label='MODE LISTE', size=8, face=(120, 230, 120)), 'pledit2')
# the scroll bar: its track and its thumb
track = new(8, 296, (10, 12, 16, 255))
ImageDraw.Draw(track).line([(3, 2), (3, 293)], fill=(40, 60, 40))
save(track, 'posplay')
thumb = new(8, 17)
bevel(ImageDraw.Draw(thumb), 1, 1, 6, 15, face=GOLD)
save(thumb, 'btnplay')
# the icons of the lines: the kind of file, the lyrics
for name, label, color in [('infomp3', 'MP3', (0, 200, 0)), ('infowav', 'WAV', (0, 170, 220)), ('infomid', 'MID', (220, 170, 0)), ('infocd', 'CD', (200, 200, 200))]:
    im = new(17, 9)
    dd = ImageDraw.Draw(im)
    dd.rectangle([0, 0, 16, 8], outline=color)
    text(dd, (8, 4), label, 7, color, 'mm')
    save(im, name)
for name, color in [('lyrok', (255, 40, 40)), ('lyrno', (60, 30, 30))]:
    im = new(17, 9)
    ImageDraw.Draw(im).ellipse([3, 1, 13, 7], fill=color)
    save(im, name)

# --- the compact bar (mainp.bmp, 112 x 12) and its buttons -----------------------------------------------------
mp = new(112, 12, METAL + (255,))
metal(mp)
save(mp, 'mainp')
for name, kind, label in [('arrp', 'prev', None), ('pausep', 'pause', None), ('playp', 'play', None), ('avp', 'next', None),
                          ('petitp', None, '_'), ('helpp', None, '?'), ('lyrp', None, 'L')]:
    im = new(17, 13)
    dd = ImageDraw.Draw(im)
    bevel(dd, 1, 1, 15, 11)
    if kind:
        sm = new(25, 18)
        glyph(ImageDraw.Draw(sm), kind, 12, 9)
        im.alpha_composite(sm.resize((17, 12), Image.NEAREST), (0, 0))
    if label:
        text(dd, (8, 6), label, 8, INK, 'mm')
    save(im, name)

# --- the icon -----------------------------------------------------------------------------------------------------
ico = new(32, 32)
dd = ImageDraw.Draw(ico)
dd.rounded_rectangle([1, 1, 30, 30], 5, fill=METAL, outline=GOLD)
dd.rectangle([5, 7, 26, 16], fill=LCD)
text(dd, (16, 12), 'WZ', 8, GREEN, 'mm')
glyph(dd, 'play', 16, 23, GOLD)
save(ico, 'ico')

# --- INFO.txt: the colours, as VB wrote them (&H00BBGGRR&) -------------------------------------------------------
def vb(rgb):
    r, g, b = rgb
    return '&H00%02X%02X%02X&' % (b, g, r)

colors = [(0, 230, 0), (0, 215, 0), (0, 215, 0), (0, 215, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0),
          (0, 230, 0), (0, 230, 0), (240, 200, 0), (240, 200, 0), (255, 40, 40), (255, 40, 40), (255, 255, 255)]
lines = ['[INF]', 'Classique (façon Winamp 2)'] + [vb(c) for c in colors] + [vb((0, 255, 0)) for i in range(23)]
with open(os.path.join(OUT, 'INFO.txt'), 'w', encoding='utf-8') as f:
    f.write('\n'.join(lines) + '\n')
print('skin written in', OUT)

# VIS.txt: where WinZik draws its visualizer (the spectrum of the sound), in the opened part of the player
with open(os.path.join(OUT, 'VIS.txt'), 'w') as f:
    f.write('127 108 124 14\n')
