package controller.console.administrateur;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.administrateur.Promo_Modification;

import model.Eleve;
import model.Promotion;
import model.list.HandlerPromotions;

public class Promo_ModificationControle {
	private Scanner sc;
	private ArrayList<Eleve> choice_list;
	
	private ArrayList<Promotion> liste_promotions;
	private Promotion p;
	
	private class InputDone extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	private class PromoNotFound extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	public Promo_ModificationControle(ArrayList<Promotion> liste_promotions, ArrayList<Eleve> liste_eleves) {
		this.choice_list = new ArrayList<Eleve>();
		this.liste_promotions = liste_promotions;
		boolean done = false;
		sc = new Scanner(System.in);
		
		Promo_Modification.trigger();
		while (!done) {
			this.choice_list.clear();
			this.choice_list.addAll(liste_eleves);
			Promo_Modification.showPromotions(this.liste_promotions);
			try {
				this.getPromotion();
				this.editPromotion();
			} catch (InputMismatchException e) {
				Promo_Modification.fail(0,this.liste_promotions.size());
			} catch (PromoNotFound e) {
				Promo_Modification.failLibelle();
			} catch (InputDone e) {
				done = true;
			}
		}
		
	}
	
	private void getPromotion() throws InputMismatchException, PromoNotFound, InputDone {
		String str_input;
		int input;

		str_input = sc.nextLine();
		try {
			input = Integer.parseInt(str_input);
			if ((input < 0) || (input > this.liste_promotions.size())) {
				throw new InputMismatchException();
			} else if (input == 0) {
				throw new InputDone();
			} else {
				this.p = this.liste_promotions.get(input-1);
			}
		} catch (NumberFormatException e) {
			this.p = HandlerPromotions.getPromotion(str_input, this.liste_promotions);
			if (this.p == null) {
				throw new PromoNotFound();
			}
		}
	}
	
	private void editPromotion() {
		boolean done = false;
		int i;
		
		for (i = 0 ; i < this.p.getListeEleves().size() ; i++) {
			this.choice_list.remove(this.p.getListeEleves().get(i));
		}
		
		while (!done) {
			try {
				Promo_Modification.askField(this.p);
				this.askField();
			} catch (InputDone e) {
				done = true;
			} catch (InputMismatchException e) {
				sc.nextLine();
				Promo_Modification.fail(0,2);
			}
		}
	}
	
	private void askField() throws InputDone, InputMismatchException {
		int input;
		input = sc.nextInt();
		sc.nextLine();
		
		if (input < 0 || input > 2) {
			throw new InputMismatchException();
		} else {
			if (input == 0) { throw new InputDone(); }
			else if (input == 1) {
				Promo_Modification.askLibelle();
				this.editLibelle();
			} else if (input == 2) {
				boolean done = false;
				while (!done) {
					try {
						Promo_Modification.askEleves(this.choice_list);
						Promo_Modification.currentEleves(this.p.getListeEleves());
						this.editEleves();
					} catch (InputDone e) {
						done = true;
					} catch (NumberFormatException e) {
						Promo_Modification.fail(this.p.getListeEleves().size()*(-1), this.choice_list.size());
					}
				}
			}
		}
	}
	
	private void editLibelle() {
		String input = sc.nextLine();
		this.p.setLibelle(input);
	}
	
	private void editEleves() throws InputDone, NumberFormatException {
		int input;
		String str_input;
		str_input = sc.nextLine();
		ArrayList<Eleve> ens_eleves = this.p.getListeEleves();
		
		input = Integer.parseInt(str_input);
		if ((input < (-1)*ens_eleves.size()) || (input > choice_list.size())) {
			throw new NumberFormatException();
		} else if (input > 0) {
			input--;
			ens_eleves.add(choice_list.get(input));
			Promo_Modification.addEleve(choice_list.get(input));
			choice_list.remove(input);
		} else if (input < 0) {
			input++;
			input = input*(-1);
			choice_list.add(ens_eleves.get(input));
			Promo_Modification.removeEleve(ens_eleves.get(input));
			ens_eleves.remove(input);
		} else {
			throw new InputDone();
		}
	}
	
	public void updateDatabase() {
		Promo_Modification.updateDatabase();
	}
}
