import display
from time import sleep
import audio
import buttons
import mch22
import sys
import os

APP_PATH = "/".join(__file__.split("/")[:-1])
sys.path.append(APP_PATH)

audio_file = APP_PATH + "/tschunk.mp3"
print("audio: ", audio_file)

import dragon

audio_init = audio.play(audio_file, volume=0)

global bg
global kite
global kitet
bg = True
kite = True
kitet = True

colors = [
    0xFF0000,
    0x00FF00,
    0x0000FF,
    0xFF00FF,
    0xFFFF00,
    0x00FFFF
]

colors2 = [
    0x00FFFF,
    0xFF0000,
    0x00FF00,
    0x0000FF,
    0xFF00FF,
    0xFFFF00
]

class State:
    name = "Andz Rave 2022"
    color_index = 0
    x = 0
    y = 0
    x_motion = 2
    y_motion = 2
    x_scale = 1
    y_scale = 1

def reboot(pressed):
  if pressed:
    mch22.exit_python()

def playaudio(pressed):
  if pressed:
    tschunk_id = audio.play(audio_file, volume=150)
    print("Button A pressed")
    
def bgcolor(pressed):
  if pressed:
    global bg
    if bg:
      bg = False
    else:
      bg = True
    print("Button START pressed")
    
def kitecolor(pressed):
  if pressed:
    global kite
    if kite:
      kite = False
    else:
      kite = True
    print("Button SELECT pressed")
    
def kitetoggle(pressed):
  if pressed:
    global kitet
    if kitet:
      kitet = False
    else:
      kitet = True
    print("Button MENU pressed")

buttons.attach(buttons.BTN_HOME,reboot)
buttons.attach(buttons.BTN_A,playaudio)
buttons.attach(buttons.BTN_START,bgcolor)
buttons.attach(buttons.BTN_SELECT,kitecolor)
buttons.attach(buttons.BTN_MENU,kitetoggle)

def cycle_color():
    # Increment the color index
    State.color_index = (State.color_index + 1) % len(colors)


def draw_text():
    display.drawText(State.x, State.y, State.name ,colors[State.color_index], "PermanentMarker22", State.x_scale, State.y_scale)
    display.flush()

def movetick():
    # Move the text
    State.x += State.x_motion
    State.y += State.y_motion

    x_upperbound = display.width() - (display.getTextWidth(State.name, "PermanentMarker22") * State.x_scale)
    y_upperbound = display.height() - (display.getTextHeight("PermanentMarker22") * State.y_scale)
    # Check for collisions
    if State.x < 0 or State.x > x_upperbound:
        State.x_motion = -State.x_motion
        State.x += State.x_motion
    if State.y < 0 or State.y > y_upperbound:
        State.y_motion = -State.y_motion
        State.y += State.y_motion

while True:
    #print("bg: ", bg)
    if bg:
      display.drawFill(colors[State.color_index])
    else:
      display.drawFill(0x000000)
    movetick()
    cycle_color()
    draw_text()
    #dragon.brightup()
    #dragon.brightanimation(1)
    display.flush()
    #print("kite: ", kite)
    if kitet:
      dragon.lightup_led()
    else:
      dragon.lightdown_led()
    if kite:
      if kitet:
        dragon.brightup()
    else:
      if kitet:
        dragon.brightdown()
    sleep(0.1)