package Testate.Stream;
import java.util.*;
import java.util.stream.Collectors;

public class Movie {
    private int id;
    private String title;
    private int year;
    private String imdb;
    private ArrayList<Genre> genres;
    private ArrayList<Director> directors;

    {
        genres = new ArrayList<>();
        directors = new ArrayList<>();
    }

    public Movie(int id, String title, int year, String imdb) {
        this.id = id;
        this.title = title;
        this.year = year;
        this.imdb = imdb;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public int getYear() {
        return year;
    }

    public String getImdb() {
        return imdb;
    }

    public ArrayList<Genre> getGenres() {
        return genres;
    }

    public ArrayList<Director> getDirectors() {
        return directors;
    }

    @Override
    public String toString() {
        return "Movie [title=" + title + ", year=" + year +  " ,Directors= " + directors + "Genres= " +genres +  " ]";
    }
    
    
	public static void main(String[] args) {

		// Genres
		Genre genre1 = new Genre(1, "Action");
		Genre genre2 = new Genre(2, "Drama");
		Genre genre3 = new Genre(3, "Comedy");

		// Directors
		Director director1 = new Director(1, "Christopher Nolan", "imdb.com/nolan");
		Director director2 = new Director(2, "Quentin Tarantino", "imdb.com/tarantino");
		Director director3 = new Director(3, "Steven Spielberg", "imdb.com/spielberg");

		// Movies
		Movie movie1 = new Movie(1, "Inception", 2010, "imdb.com/inception");
		movie1.getGenres().add(genre1);
		movie1.getGenres().add(genre2);
		movie1.getDirectors().add(director1);

		Movie movie2 = new Movie(2, "Pulp Fiction", 1993, "imdb.com/pulpfiction");
		movie2.getGenres().add(genre2);
		movie2.getGenres().add(genre3);
		movie2.getDirectors().add(director2);

		Movie movie3 = new Movie(3, "Pulp Fiction", 1993, "imdb.com/jurassicpark");
		movie3.getGenres().add(genre1);
		movie3.getDirectors().add(director1);
		
		ArrayList<Movie> movies = new ArrayList<>();
		movies.add(movie1);
		movies.add(movie2);
		movies.add(movie3);
		
		//1. Finde die Anzahl der Filme jedes Regisseurs.
		Map<String,Long> a1 = movies.stream()
				.flatMap(m -> m.getDirectors().stream())
				.collect(Collectors.groupingBy(name -> name.getName(),Collectors.counting()));
		a1.forEach((a,b) -> System.out.println(a + " " + b));
	}
}

class Genre {
    private int id;
    private String name;

    public Genre() {
    }

    public Genre(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Genre [id=" + id + ", name=" + name + "]";
    }
}

class Director {
    private int id;
    private String name;
    private String imdb;

    public Director() {
    }

    public Director(int id, String name, String imdb) {
        this.id = id;
        this.name = name;
        this.imdb = imdb;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getImdb() {
        return imdb;
    }

    @Override
    public String toString() {
        return "Director [id=" + id + ", name=" + name + ", imdb=" + imdb + "]";
    }
}