################################################################
#						Wake Up Timer                          #
#	   Programmed and Tested by Nnyf and Feeenschtaub          #
# 						Inspired by Tea Timer                  #
#	Original: https://https://badge.team/projects/tea_timer    #
################################################################

import display
import buttons
import utime
import vibra

def headline():
    disp.print('Timer', posy=0, fg=[255, 215, 0])


def timeline(seconds):
    hn = seconds // 60 // 60
    mn = seconds // 60 - 60 * hn
    sc = seconds % 60
    message = '{:02}:{:02}:{:02}'.format(hn, mn, sc)
    disp.print(message, posy=16, fg=[255, 255, 255])


def timeline_finished():
    message = 'Finished!'
    disp.print(message, posy=16, fg=[255, 255, 255])
    vibra.set(True)
    utime.sleep_ms(300)
    vibra.set(False)
    utime.sleep_ms(300)


disp = display.open()
timer_seconds = 300
timer_state = 0  # 0 = setup, 1 = running, 2 = finished
start_time = 0

while True:
    pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT)
    if pressed & buttons.BOTTOM_LEFT != 0:
        if timer_state == 0 and timer_seconds > 30:
            timer_seconds -= 30
    if pressed & buttons.BOTTOM_RIGHT != 0:
            timer_seconds += 30
    if pressed & buttons.TOP_RIGHT != 0:
        if timer_state != 1:
            start_time = utime.time()
            timer_state = 1
    disp.clear()
    headline()
    if timer_state == 0:
        timeline(timer_seconds)
    if timer_state == 1:
        seconds = timer_seconds - (utime.time() - start_time)
        timeline(seconds)
        if seconds == 0:
            timer_state = 2
    if timer_state == 2:
        timeline_finished()
    disp.update()
    utime.sleep_ms(100)
	
if mn == 60 :
	mn = 0