import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;

public class Popcorn {

    private Scanner scanner;
    private String k;
    private int v;

    public Popcorn() {
        this.scanner = new Scanner(System.in);
        this.k = "";
        this.v = 0;
    }

	public static void main(String args[]) {

		Popcorn app = new Popcorn();
        List<String> listePersonnalisee = new ArrayList<String>();
        List<String> listeFinale = new ArrayList<String>();
        int i=0, personne=3;
        String tmp="";

		System.out.println("\nBienvenue sur Popcorn !");

        // CHOIX D'UNE PLATEFORME DE STREAMING
        System.out.println("\nVeuillez choisir une plateforme de streaming:");
        String plateforme = app.display(new String[]{"Netflix", "Amazon Prime Video"});

        // CHOIX D'UN TYPE DE VIDEO
		System.out.println("\nVeuillez choisir un type de vidéo:");
        String type = app.display(new String[]{"Un Film", "Une Série"});

        // CHOIX D'UN GENRE
		System.out.println("\nVeuillez choisir un genre:");
        String genre = app.display(new String[]{"Action", "Horreur", "Romance"});

        // CREATION DU SALON & GENERATION D'UN LIEN
        System.out.println("\nEnvoyez ce lien à vos amis: https://.../serfsr654v6rsvs46rev84se6f6s54v6sr65srv65v4s654v6s5e4");

        // LIENS ENVOYES
        
        // CHOIX DE 2 FILMS/SERIES PAR PERSONNES
        for(i=0 ; i<personne*2 ; i++) {
            if(i%2 == 0)
                System.out.println("\n### Choix de la Personne " + ((i/2)+1) + " ###");

            System.out.println("\nVeuillez choisir un film:");
            tmp = app.show(plateforme, type, genre);
            
            if(listePersonnalisee.indexOf(tmp) == -1) {
                listePersonnalisee.add(tmp);
            }
        }

        // FILMS SELECTIONNES

        // CHOIX FINAL DU FILM / DE LA SERIE PAR PERSONNES
        for(i=1 ; i<=personne ; i++) {
            System.out.println("\n### Choix de la Personne " + i + " ###");
            System.out.println("\nVeuillez choisir un film parmi ces films présélectionnés par vous et vos amis:");
            listeFinale.add(app.display(listePersonnalisee.toArray(new String[listePersonnalisee.size()])));
        }

        System.out.print("\nLa majorité l'a remporté ! Et le film sélectionné est... ");
        System.out.println("\"" + app.selectOne(listeFinale) + "\"");

        app.scanner.close();
    }

    /**
     * Retourne l'intitulé du film ayant reçu le plus de votes
     */
    public String selectOne(List<String> elements) {
        Hashtable<String, Integer> compteur = new Hashtable<String, Integer>();
        int i;

        for(i=0 ; i<elements.size() ; i++) {
            if(!compteur.containsKey(elements.get(i))) {
                compteur.put(elements.get(i), 1);
            }
            else {
                compteur.replace(elements.get(i), compteur.get(elements.get(i))+1 );
            }
        }

        compteur.forEach((k, v) -> {
            if(this.v < v) {
                this.k = k;
                this.v = v;
            }
        });
        
        return this.k;
    }

    /**
     * Affiche un tableau sous forme de liste et retourne le choix sélectionné
     */
    public String display(String elements[]) {
        int i;

        for(i=0 ; i<elements.length ; i++) {
            System.out.println( (i+1) + ". " + elements[i] );
        }
        System.out.print("Votre choix: ");
        i = this.scanner.nextInt();

        return elements[i-1];
    }

    /**
     * En fonction des sélections précédentes, affiche une liste correspondante et retourne le film ou la série sélectionnée
     */
    public String show(String plateforme, String type, String genre) {
        String tmp = "";
        switch(plateforme) {
            case "Netflix":
                switch(type) {
                    case "Un Film":
                        switch(genre) {
                            case "Action": tmp = this.display(new String[]{"Barry Seal", "Fast & Furious 8", "Transformers 3", "Batman Begins", "Matrix"}); break;
                            case "Horreur": tmp = this.display(new String[]{"Dark Shadows", "Peur Bleue", "Dernier Train pour Busan", "The Babysitter", "World War Z"}); break;
                            case "Romance": tmp = this.display(new String[]{"Orgueil et Préjugé", "Le Fabuleux Destin d'Amélie Poulain", "The Perfect Date", "Save the Last Dance", "Coup de foudre garantie"}); break;
                        }
                        break;
                    case "Une Série":
                        switch(genre) {
                            case "Action": tmp = this.display(new String[]{"Teen Wolf", "Prison Break", "Vikings", "Scorpion", "Outlander"}); break;
                            case "Horreur": tmp = this.display(new String[]{"L'irréel", "The Order", "Ratched", "Stanger Things", "Haunting of hill house"}); break;
                            case "Romance": tmp = this.display(new String[]{"Jane the Virgin", "Gilmore Girls", "Les Demoiselles du Téléphone", "Plan Coeur", "Chasepeake Shores"}); break;
                        }
                        break;
                }
                break;
            case "Amazon Prime Video":
                switch(type) {
                    case "Un Film":
                        switch(genre) {
                            case "Action": tmp = this.display(new String[]{"Indiana Jones et le Temple Maudit", "Total Recall", "xXx: Reactivated", "Inglourious Basterds", "Kingsman: Services secrets"}); break;
                            case "Horreur": tmp = this.display(new String[]{"Les Dents de la mer", "Color out of Space", "The Human Centipede", "Le Cas 39", "Annabelle"}); break;
                            case "Romance": tmp = this.display(new String[]{"Chambre avec vue", "Les Désaccords du coeur", "Forrest Gump", "Rab Ne Bana Di Jodi", "Royal Affair"}); break;
                        }
                        break;
                    case "Une Série":
                        switch(genre) {
                            case "Action": tmp = this.display(new String[]{"Vikings", "Pennyworth", "The Boys", "Buffy the Vampire Slayer", "American Gods"}); break;
                            case "Horreur": tmp = this.display(new String[]{"Into The Dark", "The Terror", "Supernatural", "Fear the Walking Dead", "The Purge"}); break;
                            case "Romance": tmp = this.display(new String[]{"How I Met Your Mother", "Modern Love", "The Vampires Diaries", "Grey's Anatomy", "The Baker and the Beauty"}); break;
                        }
                        break;
                }
                break;
        }
        return tmp;
    }

}