# -----------------------------------------------------------------------------
# Name:        wordstats
# Purpose:
#
# Author:
# Date:
# -----------------------------------------------------------------------------
"""
Docstring: Enter your one-line summary here

and your detailed description
"""
import string
# The following imports are needed for the draw_cloud function.
import tkinter
import tkinter.font
import random

# The draw_cloud function is only needed for the optional part:
# to generate a word cloud.
# You don't need to change it.
def draw_cloud(input_count, min_length = 0):
    """
    Generate a word cloud based on the input count dictionary specified.

    Parameters:
    input_count (dict): represents words and their corresponding counts.
    min_length (int):  optional - defaults to 0.
         minimum length of the words that will appear
         in the cloud representation.
    Only the 20 most common words (that satisfy the minimum length criteria)
    are included in the generated cloud.
    """
    root = tkinter.Tk()
    root.title("Word Cloud Fun")
    # filter the dictionary by word length
    filter_count = {
            word: input_count[word] for word in input_count
            if len(word) >= min_length}
    max_count = max(filter_count.values())
    ratio = 100 / max_count
    frame=tkinter.Frame(root)
    frame.grid()
    my_row = 0
    for word in sorted(filter_count, key=filter_count.get, reverse=True)[0:20]:
        color = '#' + str(hex(random.randint(256,4095)))[2:]
        word_font = tkinter.font.Font(size=int(filter_count[word]* ratio))
        label = tkinter.Label(frame, text = word, font = word_font, fg = color)
        label.grid(row = my_row % 5, column = my_row // 5 )
        my_row +=  1
    root.mainloop()

# Enter your own helper function definitions here

def count_words (filename):
    # build and return the dictionary for the given filename
    count_dict = {}
    with open(filename, 'r', encoding='utf-8') as my_file:
        for line in my_file:
            line = line.lower()
            for word in line.split():
                word = word.strip(string.punctuation + string.digits)
                if word in count_dict:
                    count_dict[word] += 1
                else:
                    count_dict[word] = 1
    return count_dict

def report(word_dict):
    # report on various statistics based on the given word count dictionary
    print("The longest word is: {}".format(max(word_dict, key=len)))

    print("The 5 most common words are:")
    sort_by_freq = sorted(word_dict, key=word_dict.get, reverse=True)
    for word in sort_by_freq[0:5]:
        if word in word_dict:
            print(word + ': ' + str(word_dict[word]))

    with open('out.txt', 'w', encoding='utf-8') as my_file:
        alpha_list = sorted(word_dict)
        for word in alpha_list:
            my_file.write('{}: {}'.format(word, word_dict[word]))



def main():
    # get the input filename and save it in a variable
    filename = input("Please enter your file name: ")
    # call count_words to build the dictionary for the given file
    # save the dictionary in the variable word_count
    word_count = count_words(filename)
    # call report to report on the contents of the dictionary
    report(word_count)
    # If you want to generate a word cloud, uncomment the line below.
    # draw_cloud(word_count)


if __name__ == '__main__':
    main()