/********************************************************************/
/* This class represents a MovieStore object.  It maintains three   */
/* HashMaps:        					                            */
/* 	A Movielist where the keys are movie title and the              */
/* 	values are the movie with that title.  			                */
/*								                                    */
/*	An ActorList where the keys are the actor names and the	        */
/*	values are ArrayLists of movies with that actor in it.	        */
/*								                                    */
/*	A TypeList where the keys are the types of movies and the       */
/*	values are ArrayLists of movies of that type.		            */
/********************************************************************/
import java.util.*;

public class MovieStore {

    //These are the instance variables
    private HashMap<String,Movie>			  movieList;
    private HashMap<String,ArrayList<Movie>>  actorList;
    private HashMap<String,ArrayList<Movie>>  typeList;


    //These are the get methods, not set methods are needed
    public HashMap<String,Movie> getMovieList() { return movieList; }
    public HashMap<String,ArrayList<Movie>> getActorList() { return actorList; }
    public HashMap<String,ArrayList<Movie>> getTypeList() { return typeList; }


    //This is the constructor
    public MovieStore() {
		movieList = new HashMap<String,Movie>();
        actorList = new HashMap<String,ArrayList<Movie>>();
        typeList  = new HashMap<String,ArrayList<Movie>>();
    }


    //This method returns a String representation of the Movie
    public String toString() {
		return("MovieStore with " + movieList.size() + " movies.");
    }


	//This method adds a movie to the movieStore.
	public void addMovie(Movie aMovie) {
		//Add to the movieList
        movieList.put(aMovie.getTitle(), aMovie);

        //If there is no category yet matching this movie's type, make a new category
        if (!typeList.containsKey(aMovie.getType()))
            typeList.put(aMovie.getType(), new ArrayList<Movie>());

        //Add the movie to the proper category.
        typeList.get(aMovie.getType()).add(aMovie);

        //Now add all of the actors
        for (String anActor: aMovie.getActors()) {

            //If there is no actor yet matching this actor, make a new actor key
            if (!actorList.containsKey(anActor))
                actorList.put(anActor, new ArrayList<Movie>());

            //Add the movie for this actor
            actorList.get(anActor).add(aMovie);
        }
    }


	//This private method removes a movie from the movie store
    private void removeMovie(Movie aMovie) {
        //Remove from the movieList
        movieList.remove(aMovie.getTitle());

        //Remove from the type list vector.  If last one, remove the type.
        typeList.get(aMovie.getType()).remove(aMovie);
        if (typeList.get(aMovie.getType()).isEmpty())
            typeList.remove(aMovie.getType());

        //Now Remove from the actors list.  If actor has no more, remove him.
        for (String anActor: aMovie.getActors()) {
            actorList.get(anActor).remove(aMovie);
            if (actorList.get(anActor).isEmpty())
                actorList.remove(anActor);
        }
    }


    //This method removes a movie (given its title) from the movie store
    public void removeMovieWithTitle(String aTitle) {
		if (movieList.get(aTitle) == null)
	    	System.out.println("No movie with that title");	
		else
	    	removeMovie(movieList.get(aTitle));
    }


    //This method lists all movie titles that are in the store
    public void listMovies() {
    	for (String s: movieList.keySet())
			System.out.println(s);
    }


    //This method lists all movies that have the given type
    public void listMoviesOfType(String aType) {
    	for (Movie m: typeList.get(aType))
			System.out.println(m);
    }


    //This method lists all movies that star the given actor
    public void listMoviesWithActor(String anActor) {
    	for (Movie m: actorList.get(anActor))
			System.out.println(m);
    }
}