package data;

import java.util.ArrayList;
import java.util.HashMap;

public class DataBase {

	private static DataBase instance = new DataBase();
		
	private ArrayList<Movie> movies = new ArrayList();
	private ArrayList<Actor> actors = new ArrayList();
	private ArrayList<Director> directors = new ArrayList();
	private ArrayList<User> users = new ArrayList();
	
	private DataBase() {}
	
	
		public void addActing(int actorId,int movieId) {
			getMovieById(movieId).addActor(getActorById(actorId));
			getActorById(actorId).addMovie(getMovieById(movieId));
		}
		
		
		public Actor getActorById(int id) {
			for(Actor a : actors) {
				if(a.getId() == id)
					return a;
			}
			return null;
		}
		
		public Movie getMovieById(int id) {
			for(Movie m : movies) {
				if(m.getId() == id)
					return m;
			}
			return null;
		}
		
		public Director getDirectorById(int id) {
			for(Director d : directors) {
				if(d.getId() == id)
					return d;
			}
			return null;
		}
		
		public void addDirecting(int dierctorID, int movieId) {
			getDirectorById(dierctorID).addMovie(getMovieById(movieId));
			getMovieById(movieId).addDirector(getDirectorById(dierctorID));
		}
	
		public void addMovie(Movie m) {
			if(movies.contains(m.getId()))
			return;
			movies.add(m);
		}
		
		public ArrayList<Movie> getMovies() {
			return movies;
		}


		public ArrayList<Actor> getActors() {
			return actors;
		}


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


		public ArrayList<User> getUsers() {
			return users;
		}

		public void addUser(String userName, float rating, int movieId) {
			for(User u : users) {
				if(u.getName().equals(userName)) {
					u.addRating(movieId, rating);
					return;
				}
			}
			User u = new User(userName);
			u.addRating(movieId, rating);
			users.add(u);
		}
		
		public void addActor(Actor a) {
			actors.add(a);
		}
		
		public Actor getActorByName(String name) {
			for(Actor a : actors) {
				if(a.getName().equals(name))
					return a;
			}
			return null;
		}
		
		public Movie getMovieByName(String name) {
			for(Movie m : movies) {
				if(m.getTitle().contains(name))
					return m;
			}
			return null;
		}
		
		public Director getDirectorByName(String name) {
			for(Director d : directors) {
				if(d.getName().equals(name))
					return d;
			}
			return null;
		}
		public void addDirector(Director d) {
			directors.add(d);
		}
		
		public void addUserRating(String name,int movie, int rating) {
			for (User u : users) {
				if(u.getName() == name) {
					u.addRating(movie,rating);
					return;
				}
			}
			User nu = new User(name);
			nu.addRating(movie, rating);
			users.add(nu);
		}
		
		public void printMovies() {
			for( Movie m : movies) {
				System.out.println(m);
			}
		}
		
		public static DataBase getInstance() {
			return  instance;
		}
}
