from grove_rgb_lcd import *
import sys
import select

whiteTime = 5 * 60
blackTime = 5 * 60
current = "white"

def compute_seconds(total_seconds):
    return total_seconds % 60

def compute_minutes(total_seconds):
    return total_seconds // 60

def update_timer(white_total_time, black_total_time):
    white_minutes = compute_minutes(white_total_time)
    white_seconds = compute_seconds(white_total_time)

    black_minutes = compute_minutes(black_total_time)
    black_seconds = compute_seconds(black_total_time)

    setText(f"W {white_minutes:02d}:{white_seconds:02d}\nB {black_minutes:02d}:{black_seconds:02d}")

if __name__ == "__main__":
    setRGB(255, 255, 255)
    while whiteTime > 0 and blackTime > 0:
        currentTime = 0
        if current == "white":
            whiteTime -= 1
        else:
            blackTime -= 1

        update_timer(whiteTime, blackTime)

        rlist, _, _ = select.select([sys.stdin], [], [], 1)

        if rlist:
            sys.stdin.readline()  # consume Enter
            current = "black" if current == "white" else "white"
