package controller.console.enseignant;

import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.enseignant.QCM_Gestion;

import model.Enseignant;
import model.list.HandlerQCM;

import controller.Database;

public class QCM_GestionControle {
	private HandlerQCM hQCM;
	private Scanner sc;
	private int input;
	
	public QCM_GestionControle(Database d, Enseignant e_connected) {
		this.hQCM = d.getQcms();
		boolean done = false;
		sc = new Scanner(System.in);
		
		while (!done) {
			try {
				QCM_Gestion.trigger();
				this.askInput();
			} catch (InputMismatchException e) {
				QCM_Gestion.fail();
			}
			if (input == 0) { done = true;
			} else if (input == 1) {
				QCM_CreationControle qcc = new QCM_CreationControle(e_connected);
				qcc.addToDatabase(this.hQCM);
			} else if (input == 2) {
				QCM_ModificationControle qmc = new QCM_ModificationControle(hQCM.getListe(), e_connected);
				qmc.updateDatabase();
			} else {
				QCM_SuppressionControle qsc = new QCM_SuppressionControle(hQCM.getListe(), e_connected);
				qsc.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.hQCM.updateDatabase();
		QCM_Gestion.updateDatabase();
	}
}
