import time
import  Ressources_simon
import Gestion_controles
import random

Simon = Ressources_simon.simon()
Controles = Gestion_controles.controles()


#Variable
objects_boutons = [Controles.bouton_haut, Controles.bouton_bas,
                 Controles.bouton_gauche, Controles.bouton_droite,
                 Controles.bouton_start]
sequence =[]
initialisation = 0
sequence_joueur = []
sequence_int = 1
Audio = [523, 659, 784, 392]
nombre_de_couleur_sequence_joueur = 0
nombre_de_couleur_sequence = 0
start_bp = 0
score = 0
end_game = 0

Simon.zone_affichage.append(Simon.ecran_fin)
Simon.zone_affichage.append(Simon.label_regles)
Simon.zone_affichage.append(Simon.label_rejouer)
Simon.zone_affichage.append(Simon.ecran_jeu)
Simon.zone_affichage.append(Simon.label_score)
Simon.zone_affichage.append(Simon.label_score_final)

Simon.label_start.text = "Start"
Simon.label_start.x = 4
Simon.label_start.y = 120

Simon.label_score_final.hidden = True
Simon.label_score.hidden = True
Simon.label_rejouer.hidden = True
Simon.ecran_jeu.hidden = True
Simon.ecran_fin.hidden = True
Simon.label_regles.hidden = True
for pad in Simon.illuminations:
    Simon.zone_affichage.append(pad)

#fonction menu de départ
def etape_1(active,start):
    if active ==1:
        #Menu jeux
        Simon.zone_affichage.append(Simon.ecran_titre)

        #importation du text
        Simon.zone_affichage.append(Simon.label_start)
        Simon.label_start.text = "Start"
        Simon.label_start.x = 65
    if start == 1:
        Simon.ecran_jeu.hidden = False
        Simon.ecran_titre.hidden = True
        Simon.ecran_fin.hidden = True
        Simon.label_score.hidden = True
               

#fonction fin du jeux
def game_over():
    Simon.ecran_jeu.hidden = True
    Simon.ecran_titre.hidden = True
    Simon.label_regles.hidden = True 
    Simon.ecran_fin.hidden = False
    Simon.label_score.hidden = True
    Simon.label_rejouer.hidden = False

        
etape_1(1,0)
  
  
#Boucle de jeux
while True:
    while initialisation ==0:
        Simon.label_rejouer.hidden = True
        Simon.ecran_fin.hidden = True
        Simon.label_start.hidden = True
        time.sleep(0.2)
        Simon.label_start.hidden = False
        time.sleep(0.2)
        for indice, bouton in enumerate(objects_boutons):
            if bouton.value == Controles.appuye:
                #bouton de start
                if indice == 4:
                    start_bp += 1
        if start_bp == 1:
            Simon.ecran_jeu.hidden = True
            Simon.ecran_titre.hidden = True
            Simon.ecran_fin.hidden = True
            Simon.label_regles.hidden = False
        if start_bp == 2:
             etape_1(0,1)
             initialisation = 1
             Simon.label_start.hidden = True
             Simon.label_score.hidden = False
             time.sleep(0.1)
    find = True
    #création séquence aléatoire
    while sequence_int == 1:
        sequence.append(random.randint(0,3))
        time.sleep(0.4)
        for number in sequence:
            Simon.allume_pad(number, Audio[number])
            time.sleep(0.3)

        sequence_int = 0

    sequence_joueur = []

    # Tour joueur
    for couleur in sequence:
        if end_game == 1:
            break   
        couleur_actif = 1
        
        while couleur_actif == 1:
            index_bouton_appuye = Controles.attente_appui_bouton(timeout=3.0)

            #Timeout
            if index_bouton_appuye is None:
                print("aucun bouton appuyé")
                end_game = 1
                find = False
                break

            Simon.allume_pad(index_bouton_appuye, Audio[index_bouton_appuye])
            sequence_joueur.append(index_bouton_appuye)

            if index_bouton_appuye != couleur:
                print("faux")
                end_game = 1
                find = False
                break
                

            couleur_actif = 0

    if len(sequence_joueur) == len(sequence) and end_game==0:
        print("sequence suivante")
        score += 1
        Simon.label_score.text = str(score)
        sequence_int = 1

    if find ==False:
        Simon.label_score_final.hidden = False
        while end_game == 1:
            Simon.label_score_final.text = str(f"Score: {score}")
            game_over()
            print("GAME OVER")

            Simon.label_rejouer.hidden = False
            time.sleep(0.2)
            Simon.label_rejouer.hidden = True
            time.sleep(0.2)

            for indice, bouton in enumerate(objects_boutons):
                if bouton.value == Controles.appuye:
                    if indice == 4:  # bouton start
                        print("Restart")
                        Simon.ecran_titre.hidden = False
                        Simon.label_score_final.hidden = True
                        initialisation = 0
                        sequence = []
                        sequence_joueur = []
                        score = -1
                        start_bp = 0
                        end_game = 0
                        break
