#!/usr/bin/env python3

import time
from PIL import Image, ImageDraw
import lib.display

def start(stop_event, args):
  text1 = "Ich liebe PIA Media"
  text2 = "Wir sind ja so was von auf der Überholspur und bremsen für niemanden!"
  image_buffer = lib.display.matrix.buffer()
  #width, height = image_buffer.size
  draw = ImageDraw.Draw(image_buffer)

  # Relative position of upper text (0=start at right)
  pos1 = 0
  # Relative position of lower text (0=start at right)
  pos2 = 0

  textwidth1 = lib.display.matrix.large_font.getlength(text1)
  textwidth2 = lib.display.matrix.large_font.getlength(text2)

  while not stop_event.is_set():
    # All to black
    draw.rectangle( ((0,0),(lib.display.matrix.width-1,lib.display.matrix.height-1)), fill=(0,0,0) )

    # Upper text
    draw.text((lib.display.matrix.width - pos1, 0), text1, fill=(255,255,255), font=lib.display.matrix.large_font)

    # Lower text
    draw.text((lib.display.matrix.width - pos2, 30), text2, fill=(255,255,0), font=lib.display.matrix.large_font)

    lib.display.matrix.show(image_buffer)
    time.sleep(0.01)

    # Move the upper text
    pos1 += 1

    # Move the lower text
    pos2 += 2

    # Start new if text has run through
    if pos1 > (textwidth1 + lib.display.matrix.width):
      pos1 = 0
    if pos2 > (textwidth2 + lib.display.matrix.width):
      pos2 = 0

