from board import *
import time

# Error handling that I took from my guess-the-number-game but didn't need to use
def get_input_int(string):
    for i in range(5):
        for attempt in range(10):
            try:
                return int(input(string))
            except:
                print('Invalid Input')
            else:
                break
        else:
            print('Too many failed attempts.')
            exit()
            
def play_game(guesses, user_guess, rand, choice, letters, misses, word_length, show, under, space):
    start_game()
    t.pu()
    t.goto(0,-275)
    t.pd()
    t.write(' '.join(under), font=("calibri", 25, "normal"))
    # Loop which occurs the entirety of the game
    while guesses > 0 and user_guess != rand:
        # User enters if they would like to guess letter or word
        choice = window.textinput('Hangman', f"Please enter 'l' if you would like to guess a letter or 'w' if you would like to guess the {word_length} letter word: ")
        if choice == "l":
            user_guess = window.textinput('Hangman', 'Please guess a letter: ')
            # Checks if letter is in the word
            if user_guess[0] in rand:
                letters.append(user_guess)
                # t.goto(0,100)
                # t.write('Correct', align="center", font=("calibri", 20, "bold"))
                # print('Correct')
                # print(b[6-guesses])
                # Creates a list of idices that point to the the guessed letter in the word
                hold = [i for i, x in enumerate(rand) if x==user_guess[0]]
                for x in hold:
                    show[x] = user_guess
                    space[x] = user_guess
                # print(' '.join(show))
                t.pu()
                t.goto(0,-273)
                t.pd()
                t.write(' '.join(space), font=("calibri", 25, "normal"))

            # Checks if letter is not in the word
            elif user_guess[0] not in rand:
                misses.append(user_guess)
                # print('That letter is not in the word.')
                guesses -= 1
                # print(b[6-guesses])
                draw_hang(guesses)
                # print("Guesses: " + ', '.join(misses))
                t.pu()
                t.goto(0,-320)
                t.pd()
                t.write("Guesses: " + ', '.join(misses), font=("calibri", 25, "normal"))
                # print(f'You have {guesses} guesses left.')
                if guesses == 0:
                    # print(f"You lose...\nThe word was {rand}.")
                    t.pu()
                    t.goto(0,-273)
                    t.pd()
                    t.write(' '.join(rand), font=("calibri", 25, "normal"))
                    
                    t.pu()
                    t.goto(0,100)
                    t.pd()
                    t.write("You lose...", font=("calibri", 25, "normal"))
                    window.mainloop()
        elif choice == "w":
            user_guess = window.textinput("Hangman", f'Please guess the {word_length} letter word: ')
            # Checks if guessed word is the word
            if user_guess == rand:
                t.pu()
                t.goto(0,-273)
                t.pd()
                t.write(' '.join(rand), font=("calibri", 25, "normal"))
                
                t.pu()
                t.goto(0,100)
                t.pd()
                t.write("You Win!", font=("calibri", 25, "normal"))
                # print("You Win!")
                window.mainloop()
                # exit()
            # This is for if the guessed word was incorrect
            else:
                t.pu()
                t.goto(0,100)
                t.pd()
                t.write("That is incorrect.", font=("calibri", 25, "normal"))
                time.sleep(0.5)
                t.undo()
                # print("That is incorrect.")
                guesses -= 1
                # print(b[6-guesses])
                draw_hang(guesses)
                # print("Guesses: " + ', '.join(misses))
                # print(f'You have {guesses} guesses left.')
                if guesses == 0:
                    # print(f"You lose...\nThe word was {rand}.")
                    t.pu()
                    t.goto(0,-273)
                    t.pd()
                    t.write(' '.join(rand), font=("calibri", 25, "normal"))
                    
                    t.pu()
                    t.goto(0,100)
                    t.pd()
                    t.write("You lose...", font=("calibri", 25, "normal"))
                    window.mainloop()
        else:
            print('Invalid Input')