# Write your code here :-)

#libraries for debounced buttons, touchpad, lightstrip

import board, digitalio, adafruit_mpr121, neopixel
import mount_sd
from adafruit_debouncer import Button
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
import time



# set up the speaker
audio = AudioOut(board.GP18)


#setup light
strip = neopixel.NeoPixel(board.GP16, 30, brightness = 0.5, auto_write=True)

#set up touch sensors
i2c = board.STEMMA_I2C()
touch_pad = adafruit_mpr121.MPR121(i2c)
pads = [] # array that will hold our debounced pads (Buttons)
for t_pad in touch_pad:
    pads.append(Button(t_pad, value_when_pressed=True))



button_1_input = digitalio.DigitalInOut(board.GP6) # Wired to GP6
button_1_input.switch_to_input(digitalio.Pull.UP) # Note: Pull.UP for external buttons
button_1 = Button(button_1_input) # NOTE: False for external buttons

button_2_input = digitalio.DigitalInOut(board.GP7) # Wired to GP7
button_2_input.switch_to_input(digitalio.Pull.UP) # Note: Pull.UP for external buttons
button_2 = Button(button_2_input) # NOTE: False for external buttons

button_3_input = digitalio.DigitalInOut(board.GP8) # Wired to GP15
button_3_input.switch_to_input(digitalio.Pull.UP) # Note: Pull.UP for external buttons
button_3 = Button(button_3_input) # NOTE: False for external buttons

button_4_input = digitalio.DigitalInOut(board.GP9) # Wired to GP15
button_4_input.switch_to_input(digitalio.Pull.UP) # Note: Pull.UP for external buttons
button_4 = Button(button_4_input) # NOTE: False for external buttons

button_next_input = digitalio.DigitalInOut(board.GP17) # Wired to GP15
button_next_input.switch_to_input(digitalio.Pull.UP) # Note: Pull.UP for external buttons
button_next = Button(button_next_input) # NOTE: False for external buttons


path = "/sd/final-proj-sounds/"

# play_sound function - pass in the FULL NAME of file to play
def play_sound(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            pass



def play_countdown(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            r = 255
            g = 255
            b = 255
            for i in range(3):
                strip.fill((r,g,b))
                g -= 25
                b -= 25
                time.sleep(1)
        strip.fill((0,0,0))



#questions and answers
questions = ["question1.wav", "question2.wav","question3.wav","question4.wav","question5.wav",
        "question6.wav","question7.wav","question8.wav","question9.wav","question10.wav"]

answers = ["answer1.wav", "answer2.wav","answer3.wav","answer4.wav","answer5.wav",
        "answer6.wav","answer7.wav","answer8.wav","answer9.wav","answer10.wav"]


player1_score = 0
player2_score = 0
player3_score = 0
player4_score= 0

def score_count(player_tracker):
    global player1_score
    global player2_score
    global player3_score
    global player4_score

    if player_tracker == "player1":
        player1_score +=1
    elif player_tracker == "player2":
        player2_score +=1
    elif player_tracker == "player3":
        player3_score +=1
    else:
        player4_score +=1



player_list = ["player1", "player2", "player3", "player4"]
def score_read():

    for player in player_list:
        play = player + ".wav"
        play_sound(play)
        print("player", player)
        if player == "player1":
            find_score(player1_score)
        elif player == "player2":
            find_score(player2_score)
        elif player == "player3":
            find_score(player3_score)
        else:
            find_score(player4_score)

def find_score(score):
    print("score: ", score)
    if score == 0:
        play_sound("0.wav")
    if score == 1:
        play_sound("1.wav")
    elif score == 2:
        play_sound("2.wav")
    elif score == 3:
        play_sound("3.wav")
    elif score == 4:
        play_sound("4.wav")
    elif score == 5:
        play_sound("5.wav")
    elif score == 6:
        play_sound("6.wav")
    elif score == 7:
        play_sound("7.wav")
    elif score == 8:
        play_sound("8.wav")
    elif score == 9:
        play_sound("9.wav")
    if score == 10:
        play_sound("10.wav")

def find_winner():
    potential_winners = [player1_score, player2_score, player3_score, player4_score]
    potential_winners.sort(reverse=True)

    if potential_winners[0] == player1_score:
        print("PLAYER 1 WINS")
        play_sound("player1wins.wav")
    if potential_winners[0] == player2_score:
        print("PLAYER 2 WINS")
        play_sound("player2wins.wav")
    if potential_winners[0] == player3_score:
        print("PLAYER 3 WINS")
        play_sound("player3wins.wav")
    if potential_winners[0] == player4_score:
        print("PLAYER 4 WINS")
        play_sound("player4wins.wav")

def instructions():
    play_sound("instructions.wav")


def correct():
    print("CORRECT")
    play_sound("correct.wav")


def wrong():
    print("WRONG")
    play_sound("wrong.wav")


WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
ORANGE = (255,128,0)
PINK = (255,51,153)
BLUE = (0,0,255)

def player_pressed(player):
    filename = player + ".wav"

    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            for i in range(30):
                if player == "player1":
                    strip[i] = GREEN
                elif player == "player2":
                    strip[i] = ORANGE
                elif player == "player3":
                    strip[i] = BLUE
                if player == "player4":
                    strip[i] = PINK
            strip.fill(BLACK)



def next_question():
    play_sound(questions[0])

    #wait til player presses -- time.monotonic==
    not_pressed = True
    while not_pressed:
        button_1.update()
        button_2.update()
        button_3.update()
        button_4.update()

        player_tracker = ""
        if button_1.pressed:
            print("player1 pressed")
            player_pressed("player1")
            player_tracker = "player1"
            not_pressed = not not_pressed
        elif button_2.pressed:
            print("player2 pressed")
            player_pressed("player2")
            player_tracker = "player2"
            not_pressed = not not_pressed
        elif button_3.pressed:
            print("player3 pressed")
            player_pressed("player3")
            player_tracker = "player3"
            not_pressed = not not_pressed
        elif button_4.pressed:
            print("player4 pressed")
            player_pressed("player4")
            player_tracker = "player4"
            not_pressed = not not_pressed



    play_countdown("countdown.wav") #should be countdown but just testing
    play_sound("theansweris.wav")
    play_sound(answers[0])
    questions.pop(0)
    answers.pop(0)
    play_sound("correctorwrong.wav")

    correct_or_wrong_not_pressed = True
    while correct_or_wrong_not_pressed:
        pads[2].update()
        pads[3].update()
        if pads[2].pressed:
            correct()
            score_count(player_tracker)
            correct_or_wrong_not_pressed = not correct_or_wrong_not_pressed
        if pads[3].pressed:
            wrong()
            correct_or_wrong_not_pressed = not correct_or_wrong_not_pressed

    if len(questions) == 0:
        find_winner()



#instructions()

while True:
    for i in range(len(pads)):
        pads[i].update() # gets current state of each debounced pad
    button_next.update()


    if pads[4].pressed:
        score_read()


    if button_next.pressed:
        print("next question button")
        next_question()

