'''
    DS2000
    Spring 2022
    Creating movies from a JSON file
'''

import json
import matplotlib.pyplot as plt
from movie import Movie

MOVIE = "movie.json"

def read_json(filename):
    ''' Function: read_json
        Parameter: filename, a string
        Return: list of dictionaries, one per movie
    '''
    with open(filename, "r") as infile:
        data = json.load(infile)
    
    ############################################################
    #
    # In real life, we would delete everything here once we
    # figured out the structure of the file. Left in for
    # reference in case you're lookinat JSON files now or
    # in the future :)
    #
    ###########################################################
  
    # figuring out the structure of the file
    print("Data is type...", type(data))
    for key, value in data.items():
        print(key)
    # What's in the value for key results?
    results = data["results"]
    print("results is type...", type(results))
    
    # What is inthe results list?
    print("results is a list of...", type(results[0]))
    
    # Take a look at results...
    print("The keys in results are...")
    for d in results:
        for key, value in d.items():
            print(key, "--", value)
        print()
    
    # return the list of dictionaries
    return data["results"]
    
def make_movie_objs(list_of_dict):
    ''' Function: make_movie_objs
        Parameters: list of dictionaries (one dict. per movie)
        Returns: list of movie objects
    '''
    movies = []
    for d in list_of_dict:
        title = d["title"]
        date = d["release_date"]
        vote = d["vote_average"]
        m = Movie(title, vote, date)
        movies.append(m)
    return movies
        


def main():
    # Step one -- gather data from the JSON file
    data = read_json(MOVIE)
    
    # step two -- computation (make movie objects)
    movies = make_movie_objs(data)
    for movie in movies:
        print(movie)
        
    # step three --- plot year vs vote
    years = [m.year for m in movies]
    votes = [m.vote for m in movies]
    plt.scatter(years, votes, color = "magenta")
    
main()