#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
import pygame
import random

# add cheat prevention - subtract points, make sure not < 0
# extensions: use 3 buttons and 3 lights!
# add cheat prevention
# add messages such as great, good, ... for success
# add message in case you didnt get it - "keep trying" etc
# make max wait time variable!
# give only 30 tries
# decrease time span
# add sound!
# add a nice display of the score!
# use lists instead of button1, led1, etc.

leds = [ 16, 26, 13 ]
buttons = [ 12, 19, 6 ] 

wait_time = 0.3
wait_time2 = 0.5 

def setup():
    GPIO.setmode(GPIO.BCM)
    for led in leds:
        GPIO.setup(led, GPIO.OUT)
    for button in buttons:
        GPIO.setup(button, GPIO.IN)
    all_leds_off()

    
def led_on(led):
    GPIO.output(led, GPIO.HIGH)

def led_off(led):
    GPIO.output(led, GPIO.LOW)

def all_leds_off():
    for led in leds:
        led_off(led)

def button_pressed(button):
    return GPIO.input(button) == GPIO.LOW  

def all_effects_off(effects):
    for effect in effects:
        effect.stop()

def main():

    setup()
    pygame.mixer.pre_init(44100, -16, 1, 512)
    pygame.mixer.init()
    pygame.init()


    effect1 = pygame.mixer.Sound('/home/pi/Desktop/WAV/beep (2).wav')
    effect2 = pygame.mixer.Sound('/home/pi/Desktop/WAV/beep (3).wav')
    effect3 = pygame.mixer.Sound('/home/pi/Desktop/WAV/beep (5).wav')
    effects = [effect1, effect2, effect3]

    #rand = random.choice([led1, led2, led3])
    #print(rand)
    try:
        points = 0
        
        while True:
            for id in range(0, len(buttons)):
                button = buttons[id]
                led = leds[id] 
                if button_pressed(button):
                    led_on(led)
                else:
                    led_off(led)

        steps = 0

        while True:

            steps += 100
            print("Points: ", points) 
            
            wait = random.randint(30, max(40, 5000 - steps ))
            time.sleep(wait/1000)

            choice = random.randint(0, 2)
            led = leds[choice]

            button = buttons[choice]
            other_button1 = buttons[(choice + 1) % len(buttons)]
            other_button2 = buttons[(choice - 1) % len(buttons)] 

            led_on(led)

            # cheat detection - button already pressed?
            if button_pressed(button):
                print("YOU CHEATER!!")
                points = max(0, points-5)
                all_effects_off(effects)
                effect3.play()
                led_off(led)
                continue
            
            ts = time.clock()

            while time.clock() - ts < wait_time : 
                if button_pressed(button):
                    all_effects_off(effects)
                    effect2.play() 
                    print(random.choice(["Great!", "Nice", "Very Good!", "You Rock!"]))
                    time.sleep(wait_time2)
                    points += 1
                    break
                elif button_pressed(other_button1) or button_pressed(other_button2):
                    all_effects_off(effects)
                    effect1.play() 
                    print(random.choice(["Bad!", "Wrong Button", "Not Good!"]))
                    time.sleep(wait_time2)
                    if points > 0:
                        points -= 1
                    break

            led_off(led)

    finally:

        all_leds_off()
        GPIO.cleanup()

main()



