package controller.console.administrateur;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.administrateur.Module_Modification;
import model.Module;
import model.list.HandlerModules;

public class Module_ModificationControle {
	private Scanner sc;
	private ArrayList<Module> choice_list;
	
	private ArrayList<Module> liste_modules;
	private Module m;
	
	private class InputDone extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	private class ModuleNotFound extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	public Module_ModificationControle(ArrayList<Module> liste_modules) {
		this.liste_modules = liste_modules;
		this.choice_list = new ArrayList<Module>();
		boolean done = false;
		sc = new Scanner(System.in);
		
		Module_Modification.trigger();
		while (!done) {
			this.choice_list.clear();
			this.choice_list.addAll(this.liste_modules);
			Module_Modification.showModules(this.liste_modules);
			try {
				this.getModule();
				this.editModule();
			} catch (InputMismatchException e) {
				Module_Modification.fail(0,this.liste_modules.size());
			} catch (ModuleNotFound e) {
				Module_Modification.failLibelle();
			} catch (InputDone e) {
				done = true;
			}
		}
		
	}
	
	private void getModule() throws InputMismatchException, ModuleNotFound, InputDone {
		String str_input;
		int input;
		
		str_input = sc.nextLine();
		try {
			input = Integer.parseInt(str_input);
			if ((input < 0) || (input > this.liste_modules.size())) {
				throw new InputMismatchException();
			} else if (input == 0) {
				throw new InputDone();
			} else {
				this.m = this.liste_modules.get(input-1);
			}
		} catch (NumberFormatException e) {
			this.m = HandlerModules.getModule(str_input, this.liste_modules);
			if (this.m == null) {
				throw new ModuleNotFound();
			}
		}
	}
	
	private void editModule() {
		boolean done = false;
		
		while (!done) {
			try {
				Module_Modification.askField(this.m);
				this.askField();
			} catch (InputDone e) {
				done = true;
			} catch (InputMismatchException e) {
				sc.nextLine();
				Module_Modification.fail(0,4);
			}
		}
	}
	
	private void askField() throws InputDone {
		int input;
		int i;
		input = sc.nextInt();
		sc.nextLine();
		if (input < 0 || input > 4) {
			throw new InputMismatchException();
		} else {
			if (input == 0) { throw new InputDone(); }
			else if (input == 1) {
				Module_Modification.askLibelle();
				this.editLibelle();
			} else if (input == 2) {
				Module_Modification.askSyllabus();
				this.editSyllabus();
			} else if (input == 3) {
				this.choice_list.remove(m);
				for (i = 0 ; i < this.m.getPere().size() ; i++) {
					this.choice_list.remove(this.m.getPere().get(i));
				}
				Module_Modification.askPere(choice_list);
				this.editPere();
			}
		}
	}
	
	private void editLibelle() {
		String input = this.sc.nextLine();
		this.m.setLibelle(input);
	}
	
	private void editSyllabus() {
		String input = this.sc.nextLine();
		this.m.setSyllabus(input);
	}
	
	private void editPere() {
		ArrayList<Module> ens_modules = this.m.getPere();
		boolean done = false;
		while (!done) {
			try {
				Module_Modification.askPere(this.choice_list);
				Module_Modification.currentModules(ens_modules);
				this.editModules(ens_modules);
			} catch (InputDone e) {
				done = true;
			} catch (NumberFormatException e) {
				Module_Modification.fail(ens_modules.size()*(-1), this.choice_list.size());
			}
		}
		this.m.setPere(ens_modules);
	}
	
	private void editModules(ArrayList<Module> ens_modules) 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));
			Module_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));
			Module_Modification.removeModule(ens_modules.get(input));
			ens_modules.remove(input);
		} else {
			throw new InputDone();
		}
	}
	
	public void updateDatabase() {
		Module_Modification.updateDatabase();
	}
}
