package controller.console.administrateur;

import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.administrateur.Promo_Gestion;

import model.list.HandlerPromotions;

import controller.Database;

public class Promo_GestionControle {
	private HandlerPromotions hPromo;
	private Scanner sc;
	private int input;
	
	public Promo_GestionControle(Database d) {
		this.hPromo = d.getPromotions();
		boolean done = false;
		sc = new Scanner(System.in);
		
		while (!done) {
			try {
				Promo_Gestion.trigger();
				this.askInput();
			} catch (InputMismatchException e) {
				sc.nextLine();
				Promo_Gestion.fail();
			}
			if (input == 0) { done = true;
			} else if (input == 1) {
				Promo_CreationControle pcc = new Promo_CreationControle(d.getUtilisateurs().getListeEleves());
				pcc.addToDatabase(this.hPromo);
			} else if (input == 2) {
				Promo_ModificationControle pmc = new Promo_ModificationControle(d.getListePromotions(), d.getUtilisateurs().getListeEleves());
				pmc.updateDatabase();
			} else {
				Promo_SuppressionControle psc = new Promo_SuppressionControle(d.getListePromotions());
				psc.updateDatabase();
			}
		}
		
	}
	
	private void askInput() throws InputMismatchException {
		input = sc.nextInt();
		sc.nextLine();
		if ((input < 0) || (input > 3)) {
			throw new InputMismatchException();
		}
	}
	
	public void updateDatabase(Database d) {
		this.hPromo.updateDatabase();
		Promo_Gestion.updateDatabase();
	}
}
