from machine import Pin, I2C, freq
from ws2812 import WS2812
from steptime import STEPTIME
from ssd1306 import SSD1306_I2C
import utime

freq(250_000_000)

# initialize pin
Pin(26, Pin.IN, Pin.PULL_UP)

#initialize Neopixel pin
power = machine.Pin(11,machine.Pin.OUT)
power.value(1)

# OLED
i2c = I2C(1, scl=Pin(7), sda=Pin(6), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)

# Dino
dino_x = 20
ground = 50          # lower ground (closer to bottom)
dino_y = ground
velocity = 0.0
gravity = 1.2

# Cactus
cactus_x = 128
cactus_y = ground

# Touch jump
jump_touch = STEPTIME(0, 26)
min_val = 1e6
loop = 200
settle = 20000
threshold = 8000

# NeoPixel
led = WS2812(12, 1, 0.1, 6)  # pin 12, 1 LED, brightness 0.5, SM 6

GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)

def set_led(color):
    led.pixels_fill(color)
    led.pixels_show()

#  week
week = 0
was_in_air = False

while True:   
    # --- read touch ---
    jump_touch.put(loop)
    jump_touch.put(settle)
    result = 4294967296 - jump_touch.get()

    if result < min_val:
        min_val = result

    if (result - min_val > threshold) and (dino_y >= ground):
        velocity = -12  # jump

    # --- physics ---
    velocity += gravity
    dino_y += velocity

    # Check if in air BEFORE clamping
    currently_in_air = dino_y < ground

    if dino_y >= ground:
        dino_y = ground
        velocity = 0

    # Landing event: was in air, now not
    if was_in_air and not currently_in_air:
        set_led((0, 80, 0))   # softer green
        utime.sleep(0.08)
        set_led(BLACK)
        week += 1

    # Update state
    was_in_air = currently_in_air
        
    # --- collision detection ---
    dino_w = 10
    dino_h = 10
    cactus_w = 6
    cactus_h = 10

    if (dino_x < cactus_x + cactus_w and
        dino_x + dino_w > cactus_x and
        dino_y < cactus_y + cactus_h and
        dino_y + dino_h > cactus_y):
        
        # Flash red 3 times
        for _ in range(3):
            set_led(RED)
            utime.sleep(0.1)
            set_led(BLACK)
            utime.sleep(0.1)

        # Game Over screen
        oled.fill(0)
        oled.text("GAME OVER", 30, 25)
        oled.show()
        utime.sleep(2)

        cactus_x = 128
        velocity = 0
        week = 0
        dino_y = ground
        
        
    if week  > 3:
        
        # Flash blue 4 times
        for _ in range(5):
            set_led(BLUE)
            utime.sleep(0.1)
            set_led(BLACK)
            utime.sleep(0.1)

        # Game Over screen
        oled.fill(0)
        oled.text("Conglatulations",  0, 20)
        oled.text("You survive",  15, 30)
        oled.text("Fab Academy!",  15, 40)
        oled.show()
        utime.sleep(2)

        cactus_x = 128
        velocity = 0
        week = 0
        dino_y = ground

    # --- draw ---
    oled.fill(0)

    # draw ground line (optional)
    oled.hline(0, ground + 10, 128, 1)  # +10 because dino height is 10

    oled.rect(dino_x, int(dino_y), 10, 10, 1)
    oled.rect(cactus_x, int(cactus_y), 6, 10, 1)
    oled.text("Week "+str(week), 0, 0)
    oled.show()

    # --- move cactus ---
    cactus_x -= 2
    if cactus_x < -6:
        cactus_x = 128
        

    utime.sleep(0.05)