package controller.console.administrateur;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

import model.*;
import model.list.HandlerUtilisateurs;

import view.console.administrateur.Utilisateur_Creation;

public class Utilisateur_CreationControle {
	private Scanner sc;
	private ArrayList<Module> choice_list;
	
	private String nom;
	private String prenom;
	private int role;
	private ArrayList<Module> ens_modules;
	
	private class InputDone extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	public Utilisateur_CreationControle(ArrayList<Module> liste_modules) {
		choice_list = new ArrayList<Module>();
		choice_list.addAll(liste_modules);
		boolean done = false;
		sc = new Scanner(System.in);
		
		Utilisateur_Creation.trigger();
		Utilisateur_Creation.askNom();
		this.askNom();
		Utilisateur_Creation.askPrenom();
		this.askPrenom();
		while (!done) {
			try {
				Utilisateur_Creation.askRole();
				this.askRole();
				done = true;
			} catch (InputMismatchException e) {
				sc.nextLine();
				Utilisateur_Creation.fail(1,3);
			}
		}
		if (this.role == 2) {
			done = false;
			ens_modules = new ArrayList<Module>();
			while (!done) {
				try {
					Utilisateur_Creation.askModules(choice_list);
					Utilisateur_Creation.currentModules(ens_modules);
					this.askModule();
				} catch (NumberFormatException e) {
					Utilisateur_Creation.fail((-1)*ens_modules.size(),choice_list.size());
				} catch (InputDone e) {
					done = true;
				}
			}
		}
		
	}
	
	private void askNom() {
		this.nom = sc.nextLine();
	}
	
	private void askPrenom() {
		this.prenom = sc.nextLine();
	}
	
	private void askRole() throws InputMismatchException {
		this.role = sc.nextInt();
		sc.nextLine();
		if ((this.role < 1) || (this.role > 3)) {
			throw new InputMismatchException();
		}
	}
	
	private void askModule() throws NumberFormatException, InputDone {
		int input;
		String str_input;
		str_input = sc.nextLine();
		input = Integer.parseInt(str_input);
		if ((input < (-1)*ens_modules.size()) || (input > choice_list.size())) {
			throw new NumberFormatException();
		} else if (input > 0) {
			input--;
			ens_modules.add(choice_list.get(input));
			Utilisateur_Creation.addModule(choice_list.get(input));
			choice_list.remove(input);
		} else if (input < 0) {
			input++;
			input = input*(-1);
			choice_list.add(ens_modules.get(input));
			Utilisateur_Creation.removeModule(ens_modules.get(input));
			ens_modules.remove(input);
		} else {
			throw new InputDone();
		}
	}
	
	public void addToDatabase(HandlerUtilisateurs hUtil) {
		String motDePasse = generateRandomPassword();
		Utilisateur u;
		
		if (this.role == 1) {
			u = new Administrateur(this.nom, this.prenom, motDePasse);
		} else if (this.role == 2) {
			u = new Enseignant(this.nom, this.prenom, motDePasse, this.ens_modules);
		} else {
			u = new Eleve(this.nom, this.prenom, motDePasse);
		}
		
		hUtil.addListe(u);
		Utilisateur_Creation.addUtilisateur(u, motDePasse);
	}
	
	/**
	 * Génération d'un mot de passe alpha-numérique au hasard
	 * @return Mot de passe au hasard
	 */
	private String generateRandomPassword() {
		char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < 10; i++) {
		    char c = chars[random.nextInt(chars.length)];
		    sb.append(c);
		}
		String output = sb.toString();
		return output;
	}
}