import ugfx, badge, appglue, utime

doSyntaxAnimation = False
doNichtRijderAnimation = False

def frame_one():
    return bytes([
        0, 0, 128, 0,
        0, 0, 0, 0,
        0, 0, 128, 0,
        0, 0, 0, 0,
        0, 0, 128, 0,
        0, 0, 0, 0
    ])

def frame_two():
    return bytes([
        0, 0, 0, 0,
        0, 0, 128, 0,
        0, 0, 0, 0,
        0, 0, 128, 0,
        0, 0, 0, 0,
        0, 0, 128, 0
    ])

def show_syntax_logo():

    # prepare the e-ink screen.
    ugfx.set_lut(ugfx.LUT_NORMAL)
    ugfx.clear(ugfx.WHITE)
    ugfx.flush()

    # set the image to the e-ink display.
    try:
        ugfx.set_lut(ugfx.GREYSCALE)
        badge.eink_png(0,0,'/lib/syntax_logo/logo.png')
    except:
        ugfx.string(0, 0, "SYNTAX LOGO LOAD ERROR", "Roboto_Regular12", ugfx.BLACK)
        ugfx.string(0, 20, "Oopsie woopsy we made a fucky wucky oWo", "Roboto_Regular12", ugfx.BLACK)
    ugfx.flush()

    # meta joke
    ugfx.string(70, 110, "Kan dit op een andere badge?", "Roboto_Regular12", ugfx.BLACK)
    ugfx.flush()


def show_syntax_animation(pressed):

    print("[*] Starting Syntax animation..")

    global doSyntaxAnimation
    global doNichtRijderAnimation
    doSyntaxAnimation = True
    doNichtRijderAnimation = False

    # start the led sequence, recognisable by 6 leds ON.
    badge.leds_send_data(bytes([0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0]), 24)

    # start the animation.
    i = 0
    while doSyntaxAnimation:
        if (i % 2 == 0):
            frame = frame_two()
        else:
            frame = frame_one()

        badge.leds_send_data(frame, 24)
        utime.sleep(.1)
        i += 1

    badge.leds_disable()

def klootviool(pressed):

    print("[*] Starting K.U.T.T. ..")

    global doNichtRijderAnimation
    global doSyntaxAnimation
    doNichtRijderAnimation = True

    badge.leds_disable()

    badge.leds_send_data(bytes([0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0]), 24)

    while doNichtRijderAnimation:
        badge.leds_send_data(bytes([
            0, 128, 0, 0,
            0, 0, 0, 0,
            0, 128, 0, 0,
            0, 0, 0, 0,
            0, 0, 0, 0,
            0, 0, 0, 0
        ]), 24)
        utime.sleep(.1)

    badge.leds_disable()


def stopHouOpIkVindHetNietMeerLeuk(pressed):

    print("[!] Stopping all..")

    global doNichtRijderAnimation
    global doSyntaxAnimation

    doNichtRijderAnimation = False
    doSyntaxAnimation = False


def exit_prog(pressed):

    # disable leds.
    badge.leds_disable()

    # return to the menu
    appglue.start_app("")

def main_prog():
    print("--- SYNTAX APP ---")

    # prep
    ugfx.init()
    ugfx.set_lut(ugfx.LUT_NORMAL)
    ugfx.clear(ugfx.BLACK)
    ugfx.flush()
    print("[*] Init and flushed.")

    # press joystick DOWN for nichtrijder
    # press joystick UP for syntax LEDs
    # press B to stop all
    print("[*] Registering event listeners..")
    ugfx.input_attach(ugfx.JOY_DOWN, klootviool)
    ugfx.input_attach(ugfx.JOY_UP, show_syntax_animation)
    ugfx.input_attach(ugfx.BTN_B, stopHouOpIkVindHetNietMeerLeuk)


    # return home on SELECT press
    ugfx.input_attach(ugfx.BTN_SELECT, exit_prog)
    print("    .. done!")

    # show logo on init
    print("[*] Calling show_syntax_logo..")
    show_syntax_logo()

    # start syntax led sequence now
    print("[*] Calling show_syntax_animation..")
    show_syntax_animation(True)

    print("[!] Idle..")


# __main__ doesn't work :(
#if __name__ == "__main__":
main_prog()