"""Generating a random story: solution plan.
"""

from typing import TextIO
import random


def generate_random_story(data_file: TextIO, context_size: int,
                          num_words: int) -> str:
    """Return a randomly generated story with num_words words based on a
    context of context_size words from the text in data_file.

    Preconditions: data_file is open for reading
                   context_size > 0
                   num_words >= 0
    """

    all_words = read_words(data_file)
    context_to_words = build_context_to_words(all_words, context_size)
    story = generate_story(context_to_words, num_words)
    return story


def read_words(data_file: TextIO) -> list[str]:
    """Return a list of words from data_file. Words are defined as all
    strings delimited by whitespace.
    """

    return data_file.read().split()


def build_context_to_words(word_list: list[str],
                           context_size: int) -> dict[tuple, list[str]]:
    """Return a dictionary where each key is a tuple of context_size
    adjacent words from word_list, and each corresponding value is a
    list of words that immediately follow these adjacent words in
    word_list.

    Preconditions: context_size > 0

    >>> context_to_words = build_context_to_words(
    ...    ['to', 'be', 'or', 'not', 'to', 'be', 'that'], 2)
    >>> context_to_words == {('to', 'be'): ['or', 'that'],
    ...                      ('be', 'or'):['not'],
    ...                      ('or', 'not'):['to'],
    ...                      ('not', 'to'): ['be']}
    True
    >>> context_to_words = build_context_to_words(
    ...    ['to', 'be', 'or', 'not', 'to', 'be', 'or'], 2)
    >>> context_to_words == {('to', 'be'): ['or', 'or'],
    ...                      ('be', 'or'):['not'],
    ...                      ('or', 'not'):['to'],
    ...                      ('not', 'to'): ['be']}
    True
    >>> build_context_to_words([], 2)
    {}
    >>> build_context_to_words(['foo'], 1)
    {}
    >>> build_context_to_words(['foo', 'bar'], 1)
    {('foo',): ['bar']}
    >>> build_context_to_words(['foo', 'bar'], 2)
    {}
    >>> expected = {('foo', 'bar'): ['foobar']}
    >>> build_context_to_words(['foo', 'bar', 'foobar'], 2) == expected
    True
    """

    pass


def generate_story(context_to_words: dict[tuple, list[str]],
                   num_words: int) -> str:
    """Return a randomly generated story with num_words words based on a
    context_to_words.

    Preconditions: num_words >= 0
    """

    pass


# A helper function
def get_random_context(context_to_words: dict[tuple, list[str]]) -> tuple:
    """Return a random context (key) from context_to_words."""

    return random.choice(list(context_to_words.keys()))


if __name__ == '__main__':

    import doctest
    doctest.testmod()

    with open('small.txt') as small:
        print(generate_random_story(small, 2, 10))

    # filename = input('Enter a filename: ')
    # with open(filename) as training_file:
    #     print(generate_random_story(training_file, 2, 50))
