package controller.console.enseignant;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.enseignant.QCM_Suppression;

import model.Enseignant;
import model.QCM;
import model.list.HandlerQCM;

public class QCM_SuppressionControle {
	private Scanner sc;
	
	private ArrayList<QCM> liste_qcm;
	private QCM qcm;
	
	private class InputDone extends Exception {
		private static final long serialVersionUID = 1L;
	}
	
	public QCM_SuppressionControle(ArrayList<QCM> liste_qcm, Enseignant e_connected) {
		boolean done = false;
		this.liste_qcm = HandlerQCM.getOwnAndPublic(e_connected, liste_qcm);
		sc = new Scanner(System.in);
		
		QCM_Suppression.trigger();
		while (!done) {
			QCM_Suppression.showQCM(this.liste_qcm);
			try {
				this.getQCM();
				QCM_Suppression.areYouSure(this.qcm);
				if (this.getAreYouSure()) {
					this.removeQCM();
				}
			} catch (InputMismatchException e) {
				QCM_Suppression.fail(0,this.liste_qcm.size());
			} catch (InputDone e) {
				done = true;
			}
		}
	}
	
	private void getQCM() throws InputMismatchException, InputDone {
		String str_input;
		int input;

		str_input = sc.nextLine();
		try {
			input = Integer.parseInt(str_input);
			if ((input < 0) || (input > this.liste_qcm.size())) {
				throw new InputMismatchException();
			} else if (input == 0) {
				throw new InputDone();
			} else {
				this.qcm = this.liste_qcm.get(input-1);
			}
		} catch (NumberFormatException e) {
			throw new InputMismatchException();
		}
	}
	
	private boolean getAreYouSure() {
		String input = sc.nextLine();
		return (input.startsWith("o") || input.startsWith("O"));
	}
	
	private void removeQCM() {
		this.liste_qcm.remove(this.qcm);
	}
	
	public void updateDatabase() {
		QCM_Suppression.updateDatabase();
	}
}
