from datetime import datetime
from PIL import Image, ImageDraw, ImageFont

def scale_to_height(icon):
    scale = 100 / icon.height
    return icon.resize((int(icon.width * scale), int(icon.height * scale)))

BLACK  = 0x000000 # 00 BGR
WHITE  = 0xffffff # 01
YELLOW = 0x00ffff # 10
RED    = 0x0000ff # 11
font18 = ImageFont.truetype('Font.ttc', 18)
font24 = ImageFont.truetype('Font.ttc', 24)
font40 = ImageFont.truetype('Font.ttc', 40)
font64 = ImageFont.truetype('Font.ttc', 64)
font100 = ImageFont.truetype('Font.ttc', 100)

image = Image.new('RGB', (800, 480), WHITE)
draw = ImageDraw.Draw(image)

# Date/time
now = datetime.now()
draw.text((10, 480-10-64), f'{now:%y/%m/%d}', font=font64, fill=BLACK)
draw.text((10, 480-10-64-10-100), f'{now:%I:%M}', font=font100, fill=BLACK)

# TODO: weather
draw.text((10, 10), '?mm', font=font64, fill=BLACK)
draw.text((10, 10+64), '20°', font=font100, fill=BLACK)
draw.text((10, 10+64+100), '6° - 24°', font=font64, fill=BLACK)

# Bus
y = 0
with Image.open('icon_bus.png') as icon:
    image.paste(scale_to_height(icon), (230, y+50))
draw.polygon((200, 0, 475, 0, 475, 50, 212.5, 50), fill=BLACK)
draw.text((215, y+5+16), 'Camberwell', font=font24, fill=WHITE)
draw.text((400, y+5), '612 S', font=font40, fill=WHITE, anchor='ma')
draw.text((400, y+55), '15', font=font64, fill=BLACK, anchor='ma')
draw.text((550, y+5), '612 N', font=font40, fill=BLACK, anchor='ma')
draw.text((550, y+55), '5', font=font64, fill=BLACK, anchor='ma')
draw.text((700, y+5), '766 N', font=font40, fill=BLACK, anchor='ma')
draw.text((700, y+55), '20', font=font64, fill=BLACK, anchor='ma')

# Train
y = 160
with Image.open('icon_train.png') as icon:
    image.paste(scale_to_height(icon), (280, y+50))
draw.polygon((240, 160, 595, 160, 595, 210, 252.5, 210), fill=BLACK)
draw.text((255, y+5+16), 'City', font=font24, fill=WHITE)
draw.text((400, y+5), 'Express', font=font40, fill=WHITE, anchor='ma')
draw.text((400, y+55), '15', font=font64, fill=BLACK, anchor='ma')
draw.text((550, y+5), 'All', font=font40, fill=WHITE, anchor='ma')
draw.text((550, y+55), '5', font=font64, fill=BLACK, anchor='ma')
draw.text((700, y+5), 'Outbound', font=font40, fill=BLACK, anchor='ma')
draw.text((700, y+55), '20', font=font64, fill=BLACK, anchor='ma')

# Tram (y=320 to 470)
y = 320
with Image.open('icon_tram.png') as icon:
    image.paste(scale_to_height(icon), (310, y+50))
draw.polygon((280, 320, 800, 320, 800, 370, 292.5, 370), fill=BLACK)
draw.text((295, y+5+16), 'City', font=font24, fill=WHITE)
draw.text((550, y+5), '109', font=font40, fill=WHITE, anchor='ma')
draw.text((550, y+55), '7', font=font64, fill=BLACK, anchor='ma')
draw.text((700, y+5), '70', font=font40, fill=WHITE, anchor='ma')
draw.text((700, y+55), '14', font=font64, fill=BLACK, anchor='ma')

# Line between left and right
draw.line((200, 0, 400, 800), fill=BLACK)

image.save('test_before_palette.png')

# Dither into eink palette
pal_image = Image.new('P', (1, 1))
pal_image.putpalette((0x00,0x00,0x00, 0xff,0xff,0xff, 0xff,0xff,0x00, 0xff,0x00,0x00) + (0x00,0x00,0x00)*252)
# Convert the source image to the 4 colors, dithering if needed
image_4color = image.quantize(palette=pal_image)
image_4color.save('test.png')

show_on_screen = False
if show_on_screen:
    import epd7in3g as epd
    try:
        epd.init()
        epd.display(epd.get_buffer(image))
        input('Press Enter to clear')
    finally:
        epd.clear()
        epd.epdconfig.module_exit(cleanup=True)
