import random
from pathlib import Path

NUM_INCORRECT = 6
HANGMANPICS = [
    """
  +---+
  |   |
      |
      |
      |
      |
=========""",
    """
  +---+
  |   |
  O   |
      |
      |
      |
=========""",
    """
  +---+
  |   |
  O   |
  |   |
      |
      |
=========""",
    """
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========""",
    """
  +---+
  |   |
  O   |
 /|\\  |
      |
      |
=========""",
    """
  +---+
  |   |
  O   |
 /|\\  |
 /    |
      |
=========""",
    """
  +---+
  |   |
  O   |
 /|\\  |
 / \\  |
      |
=========""",
]


def print_hangman(num: int) -> None:
    if num >= 0 and num < len(HANGMANPICS):
        print(HANGMANPICS[num])


def load_words(filename: Path | str = Path("./clean.txt")) -> list[str]:
    if isinstance(filename, str):
        filename = Path(filename)
    with filename.open() as file:
        return file.read().splitlines()


def get_word(words: list[str]) -> str:
    pass


def check_guess(word: str, guess: str, guessing: list[str]) -> bool:
    pass


def play() -> None:
    # load the dictionary of words and pick a random word
    words = load_words()
    word = get_word(words)

    # initialize the empty word to fill in
    guessing = ["_"] * len(word)

    # initialize incorrect guess tracking
    incorrect_guesses = set()
    num_incorrect_guesses = 0

    # print out the start of the game
    print_hangman(num_incorrect_guesses)
    print(f"The word is {len(word)} letters long")
    print()
    print(" ".join(guessing))

    # Game loop
    while True:
        # get player guess (word or letter)
        print()
        guess = input("Guess a letter or the whole word: ").lower()
        print()

        # make sure the input is only letters
        if not guess.isalpha():
            print("You can only use letters.")
            continue
        # if the guess is as long as the word,
        # check if player guessed the word correctly
        if len(guess) == len(word):
            if guess == word:
                print(f"That's correct! The word was {word}.")
                break
            print("Incorrect.")
            num_incorrect_guesses += 1
        # otherwise, make sure the guess is only 1 letter
        elif len(guess) != 1:
            print("You must either guess the word or a single letter.")
            continue
        else:
            # check if the guess is correct
            correct = check_guess(word, guess, guessing)

            # if the letter is not correct and it isn't one of the letters
            # we have already guessed, track the incorrect letter and
            # increment the number of incorrect guesses
            if not correct and guess not in incorrect_guesses:
                incorrect_guesses.add(guess)
                num_incorrect_guesses += 1
                print(f"There is no {guess}.")
            # otherwise, the guess was correct so we should
            # check if they have guessed the whole word yet
            elif "".join(guessing) == word:
                print(f"Good job! The word was {word}.")
                break
        # if we have reached or exceeded our allotted number of
        # incorrect guesses, show what the word was and exit the game
        if num_incorrect_guesses >= NUM_INCORRECT:
            print_hangman(num_incorrect_guesses)
            print(f"Sorry, the word was {word}.")
            break

        # if we are still playing, print the current state of the game
        print(f"Incorrect Letters: {' '.join(sorted(incorrect_guesses))}")
        print_hangman(num_incorrect_guesses)
        print(f"Guesses: {num_incorrect_guesses}/{NUM_INCORRECT}")
        print()
        print(" ".join(guessing))


if __name__ == "__main__":
    play()
