"""PA3 Trivia Night.

The program reads trivia questions and their answers from a file, asks a few
of them in random order without repeating one, and gives the player a few
tries at each. Afterward it writes the results to a file, one line per
question.

Name: YOUR NAME
Date: THE DATE
"""

import random


def draw_question(questions: list[str], asked: set[str]) -> str:
    """Draw a question that has not been asked yet.

    Args:
        questions: Every question in the game.
        asked: The questions asked so far.

    Returns:
        A question from questions that is not in asked. Draw it with
        random.choice(questions), and draw again for as long as you get one
        already asked. If every question has been asked, return "" without
        drawing at all, or the loop would never end. Ex:
        draw_question(["Q1", "Q2"], {"Q1"}) returns "Q2", and
        draw_question(["Q1", "Q2"], {"Q1", "Q2"}) returns "".
    """
    pass


def ask(answer: str, tries: int) -> int:
    """Read the player's answers to one question, until right or out of tries.

    Each answer is read with input("Answer: "). A blank answer is rejected by
    printing "Type an answer first." and an answer already tried is rejected
    by printing, Ex, "You already tried mars." Neither one uses up a try. A
    right answer prints "Correct!" and ends the question. A wrong one prints
    "Not quite." and uses up a try, and when the last try is used up, the
    function prints, Ex, "Out of tries. The answer was jupiter."

    Args:
        answer: The right answer.
        tries: How many wrong answers the player may give, at least 1.

    Returns:
        The number of tries the player used, counting the right one, or 0 if
        they never got it. Ex: if the answer is jupiter and the player types
        mars, a blank line, mars, and jupiter, the result is 2.
    """
    pass


def load_questions(filename: str) -> dict[str, str]:
    """Read the questions and answers from a file.

    Each line of the file is a question and its answer separated by "|", such
    as "How many sides does a hexagon have? | 6". Spaces around the question
    and the answer are removed. Blank lines, lines that start with "#", and
    lines with an empty answer are skipped. If a question is listed twice,
    its first answer is kept.

    Args:
        filename: The path of the file to read.

    Returns:
        A new dictionary that maps each question to its answer. Ex: the line
        " Largest planet? |  jupiter " gives {"Largest planet?": "jupiter"}.
    """
    pass


def save_results(filename: str, questions: list[str], scores: list[int],
                 answers: dict[str, str]) -> None:
    """Write how the player did on each question to a file, one line each.

    A line is the question, then " -- ", then the result: "got it in 1 try",
    "got it in 2 tries", and so on, or for a score of 0, "missed" and the
    answer in capital letters, Ex "Largest planet? -- missed (JUPITER)". A
    question longer than 30 characters is cut to its first 27 and "..." is
    added.

    Args:
        filename: The path of the file to write. It is replaced if it exists.
        questions: The questions asked, in order.
        scores: What ask() returned for each question, in the same order.
        answers: Each question's answer, as returned by load_questions().
    """
    pass


if __name__ == "__main__":
    # Do not edit the main block. It runs a game once every part works.
    random.seed(149)
    answers = load_questions("questions.txt")
    pool = list(answers)  # every question, in the order of the file
    print("Welcome to Trivia Night! All answers are lowercase.")
    asked = set()
    questions = []
    scores = []
    for turn in range(3):
        question = draw_question(pool, asked)
        asked.add(question)
        print()
        print(f"Question {turn + 1}: {question}")
        questions.append(question)
        scores.append(ask(answers[question], 2))
    save_results("results.txt", questions, scores, answers)
    print()
    print("Results saved to results.txt.")
