"""
Exercise 10.1.7

Discovering Computer Science, Third Edition
Jessen Havill
"""

class Movie:
    # fill this in

def read_movies(filename):
    """Create a list of Movie objects from a file."""
    
    cast_file = open(filename, 'r', encoding = 'utf-8')
    movies = []
    for line in cast_file:
        line = line.strip()
        row = line.split('\t')
        movie = row[0][:-7]
        year = row[0][-5:-1]
        actors = row[1:]
        movie = Movie(movie, int(year), actors)
        movies.append(movie)
    cast_file.close()
    return movies
    
def search(movies, title):
    """Search for a movie title in a list of movies."""
    
    for movie in movies:
        if movie.get_title() == title:
            return movie
    return None

def main():
    movies = read_movies('movies2013.txt')
    title = input('Movie title (or q to quit): ')
    while title not in 'qQ':
        movie = search(movies, title)
        if movie is None:
            print('That movie was not found.')
        else:
            print('\n' + movie.get_title() + ' (' + str(movie.get_year()) + ')')
            response = input('(P)rint actors, (A)dd actor, show (C)ommon actors, or (E)xit? ')
            while response not in 'eE':
                if response in 'aA':
                    name = input('Actor name: ')
                    movie.add_actor(name)
                elif response in 'cC':
                    other_title = input('Movie title: ')
                    other_movie = search(movies, other_title)
                    if other_movie == None:
                        print('That movie was not found.')
                    else:
                        if movie.common_actors(other_movie):
                            print(f'Yes, "{movie.get_title()}" and "{other_title}" have common actors.')
                        else:
                            print(f'No, "{movie.get_title()}" and "{other_title}" do not have common actors.')
                elif response in 'pP':
                    for actor in movie.get_actors():
                        print('    ' + actor)
                response = input('(P)rint actors, (A)dd actor, show (C)ommon actors, or (E)xit? ')
        title = input('\nMovie title (or q to quit): ')

main()