package controller.console.administrateur;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import model.*;
import model.list.HandlerUtilisateurs;

import view.console.administrateur.Utilisateur_Modification;

public class Utilisateur_ModificationControle {
	private Scanner sc;
	private ArrayList<Module> choice_list;
	
	private ArrayList<Utilisateur> liste_utilisateurs;
	private Utilisateur u;
	
	private class InputDone extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	private class UserNotFound extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	public Utilisateur_ModificationControle(ArrayList<Utilisateur> liste_utilisateurs, ArrayList<Module> liste_modules) {
		this.choice_list = new ArrayList<Module>();
		this.liste_utilisateurs = liste_utilisateurs;
		boolean done = false;
		sc = new Scanner(System.in);
		
		Utilisateur_Modification.trigger();
		while (!done) {
			this.choice_list.clear();
			this.choice_list.addAll(liste_modules);
			Utilisateur_Modification.showUtilisateurs(this.liste_utilisateurs);
			try {
				this.getUtilisateur();
				this.editUtilisateur();
			} catch (InputMismatchException e) {
				Utilisateur_Modification.fail(0,this.liste_utilisateurs.size());
			} catch (UserNotFound e) {
				Utilisateur_Modification.failLogin();
			} catch (InputDone e) {
				done = true;
			}
		}
		
	}
	
	private void getUtilisateur() throws InputMismatchException, UserNotFound, InputDone {
		String str_input;
		int input;

		str_input = sc.nextLine();
		try {
			input = Integer.parseInt(str_input);
			if ((input < 0) || (input > this.liste_utilisateurs.size())) {
				throw new InputMismatchException();
			} else if (input == 0) {
				throw new InputDone();
			} else {
				this.u = this.liste_utilisateurs.get(input-1);
			}
		} catch (NumberFormatException e) {
			this.u = HandlerUtilisateurs.getUtilisateur(str_input, this.liste_utilisateurs);
			if (this.u == null) {
				throw new UserNotFound();
			}
		}
	}
	
	private void editUtilisateur() {
		boolean done = false;
		int i;
		
		if (this.u instanceof Enseignant) {
			ArrayList<Module> ens_modules = ((Enseignant) this.u).getListeModules();
			for (i = 0 ; i < ens_modules.size() ; i++) {
				this.choice_list.remove(ens_modules.get(i));
			}
		}
		
		while (!done) {
			try {
				Utilisateur_Modification.askField(this.u);
				this.askField();
			} catch (InputDone e) {
				done = true;
			} catch (InputMismatchException e) {
				sc.nextLine();
				if (this.u instanceof Administrateur) {
					Utilisateur_Modification.fail(0,4);
				} else {
					Utilisateur_Modification.fail(0,5);
				}
			}
		}
	}
	
	private void askField() throws InputDone, InputMismatchException {
		int input;
		int cap;
		input = sc.nextInt();
		sc.nextLine();
		if (this.u instanceof Enseignant) {
			cap = 5;
		} else {
			cap = 4;
		}
		if (input < 0 || input > cap) {
			throw new InputMismatchException();
		} else {
			if (input == 0) { throw new InputDone(); }
			else if (input == 1) {
				Utilisateur_Modification.askNom();
				this.editNom();
			} else if (input == 2) {
				Utilisateur_Modification.askPrenom();
				this.editPrenom();
			} else if (input == 3) {
				Utilisateur_Modification.askLogin();
				this.editLogin();
			} else if (input == 4) {
				Utilisateur_Modification.askMotDePasse();
				this.editMotDePasse();
			} else {
				if (this.u instanceof Enseignant) {
					boolean done = false;
					while (!done) {
						try {
							Utilisateur_Modification.askModules(this.choice_list);
							Utilisateur_Modification.currentModules(((Enseignant) this.u).getListeModules());
							this.editModules();
						} catch (InputDone e) {
							done = true;
						} catch (NumberFormatException e) {
							Utilisateur_Modification.fail(((Enseignant) this.u).getListeModules().size()*(-1), this.choice_list.size());
						}
					}
				}
			}
		}
	}
	
	private void editNom() {
		String input = sc.nextLine();
		this.u.setNom(input);
	}
	
	private void editPrenom() {
		String input = sc.nextLine();
		this.u.setPrenom(input);
	}
	
	private void editLogin() {
		String input = sc.nextLine();
		this.u.setLogin(input);
	}
	
	private void editMotDePasse() {
		String input = sc.nextLine();
		this.u.encryptMotDePasse(input);
	}
	
	private void editModules() throws InputDone, NumberFormatException {
		int input;
		String str_input;
		str_input = sc.nextLine();
		ArrayList<Module> ens_modules = ((Enseignant) this.u).getListeModules();
		
		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_Modification.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_Modification.removeModule(ens_modules.get(input));
			ens_modules.remove(input);
		} else {
			throw new InputDone();
		}
		
		((Enseignant) this.u).setListeModules(ens_modules);
	}
	
	public void updateDatabase() {
		Utilisateur_Modification.updateDatabase();
	}
}
