import time
import board
import pwmio
from adafruit_motor import servo
from adafruit_circuitplayground import cp

# Setup servo on pin A1
pwm = pwmio.PWMOut(board.A1, duty_cycle=0, frequency=50)
my_servo = servo.Servo(pwm)

# Constants
TOTAL_TIME = 10  # seconds
STEP_ANGLE = 18  # 180° / 10 steps

# State variables
timer_is_set = False
timer_running = False

# Setup NeoPixels
cp.pixels.brightness = 0.3
cp.pixels.fill((0, 0, 0))  # Turn all pixels off

def set_timer():
    global timer_is_set, timer_running
    print("Timer SET. Ready to start.")
    my_servo.angle = 180  # Fully up
    cp.pixels.fill((0, 0, 0))  # Reset NeoPixels
    timer_is_set = True
    timer_running = False

def cancel_timer():
    global timer_is_set, timer_running
    print("TIMER CANCELLED and RESET.")
    my_servo.angle = 0  # Back to 0°
    cp.pixels.fill((150, 0, 150))
    cp.play_tone(1500, 0.5)
    time.sleep(0.2)
    cp.pixels.fill((0, 0, 0))
    time.sleep(0.05)
    cp.pixels.fill((150, 0, 150))
    cp.play_tone(1500, 0.5)
    time.sleep(0.2)
    cp.pixels.fill((0, 0, 0))
    time.sleep(0.05)
    cp.pixels.fill((150, 0, 150))
    cp.play_tone(1500, 0.5)
    cp.pixels.fill((0, 0, 0))
    time.sleep(0.2)
    timer_is_set = False
    timer_running = False

def start_timer():
    global timer_running, timer_is_set
    if not timer_is_set:
        print("Timer not set. Press Button A first.")
        return

    print("TIMER STARTED.")
    timer_running = True

    for i in range(TOTAL_TIME):
        # Check for cancel during countdown
        if cp.button_a and cp.button_b:
            cancel_timer()
            return

        # Update servo angle
        angle = 180 - (STEP_ANGLE * (i + 1))
        my_servo.angle = angle

        # Light up the next NeoPixel
        cp.pixels[i] = (250, 100, 0)  # Green for each second passed

        # Make a beep
        cp.play_tone(1000, 0.2)  # 1 kHz beep for 0.2 seconds

        print(f"Time left: {TOTAL_TIME - (i + 1)}s — Servo: {angle}°")

        # Sleep in small intervals so we can still check for cancel
        for _ in range(10):
            if cp.button_a and cp.button_b:
                cancel_timer()
                return
            time.sleep(0.1)

    # End of countdown
    print("TIMER COMPLETE.")
    cp.pixels.fill((255, 0, 0))
    cp.play_tone(1500, 0.5)
    time.sleep(0.1)  # All pixels red
    cp.pixels.fill((0, 0, 0))
    time.sleep(0.1)
    cp.pixels.fill((255, 0, 0))
    cp.play_tone(1500, 0.5)
    time.sleep(0.1)  # All pixels red
    cp.pixels.fill((0, 0, 0))
    time.sleep(0.1)
    cp.pixels.fill((255, 0, 0))
    cp.play_tone(1500, 0.5)
    time.sleep(0.1)  # All pixels red
    cp.pixels.fill((0, 0, 0))  # Longer beep at the end
    timer_is_set = False
    timer_running = False

# === Main loop ===
print("Ready. Press A to SET, B to START, A+B to CANCEL.")

while True:
    if cp.button_a and cp.button_b:
        cancel_timer()
        while cp.button_a or cp.button_b:
            time.sleep(0.1)  # Debounce

    elif cp.button_a:
        set_timer()
        while cp.button_a:
            time.sleep(0.1)  # Debounce

    elif cp.button_b:
        start_timer()
        while cp.button_b:
            time.sleep(0.1)  # Debounce
