package controller.console.enseignant;

import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.enseignant.Session_Gestion;

import controller.Database;

import model.Enseignant;
import model.list.HandlerSessions;

public class Session_GestionControle {
	private HandlerSessions hSess;
	private Scanner sc;
	private int input;
	
	public Session_GestionControle(Database d, Enseignant e_connected) {
		this.hSess = d.getSessions();
		boolean done = false;
		sc = new Scanner(System.in);
		
		while (!done) {
			try {
				Session_Gestion.trigger();
				this.askInput();
			} catch (InputMismatchException e) {
				Session_Gestion.fail();
			}
			if (input == 0) { done = true;
			} else if (input == 1) {
				Session_CreationControle qcc = new Session_CreationControle(d.getListePromotions(),
																			d.getListeModules(),
																			d.getListeQCM(),
																			e_connected);
				qcc.addToDatabase(this.hSess);
			} else if (input == 2) {
				Session_ModificationControle qmc = new Session_ModificationControle(this.hSess.getListe(),
																					d.getListePromotions(),
																					d.getListeModules(),
																					d.getListeQCM(),
																					e_connected);
				qmc.updateDatabase();
			} else {
				Session_SuppressionControle qsc = new Session_SuppressionControle(this.hSess.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.hSess.updateDatabase();
		Session_Gestion.updateDatabase();
	}
}
