package controller.console.enseignant;

import java.util.ArrayList;
import java.util.Scanner;

import view.console.enseignant.QCM_Creation;

import model.Enseignant;
import model.QCM;
import model.Question;
import model.Reponse;
import model.list.HandlerQCM;

public class QCM_CreationControle {
	private Scanner sc;
	
	private String libelle;
	private Enseignant createur;
	private boolean prive;
	private ArrayList<Question> lQuestions;
	
	public QCM_CreationControle(Enseignant e_connected) {
		this.lQuestions = new ArrayList<Question>();
		this.createur = e_connected;
		sc = new Scanner(System.in);
		boolean done = false;
		int count_qu = 0;
		
		QCM_Creation.trigger();
		QCM_Creation.askLibelle();
		this.askLibelle();
		QCM_Creation.askPrive();
		this.prive = this.askBoolean();
		while (!done) {
			count_qu++;
			QCM_Creation.showQuestions(this.libelle, this.lQuestions);
			QCM_Creation.askQuestion(count_qu);
			this.lQuestions.add(this.askQuestion());
			QCM_Creation.askDoneQuestion();
			done = !this.askBoolean();
		}
	}
	
	private void askLibelle() {
		String input;
		input = sc.nextLine();
		this.libelle = input;
	}
	
	private boolean askBoolean() {
		String input = sc.nextLine();
		return (input.startsWith("o") || input.startsWith("O"));
	}
	
	private Question askQuestion() {
		String libelle_qu;
		int count_r = 0;
		boolean done = false;
		ArrayList<Reponse> lReponses = new ArrayList<Reponse>();
		
		libelle_qu = sc.nextLine();
		while (!done) {
			count_r++;
			QCM_Creation.showReponses(libelle_qu, lReponses);
			QCM_Creation.askReponse(count_r);
			lReponses.add(this.askReponse());
			QCM_Creation.askDoneReponse();
			done = !this.askBoolean();
		}
		
		return (new Question(libelle_qu, lReponses));
	}
	
	private Reponse askReponse() {
		String libelle_r;
		boolean vraie = false;
		
		libelle_r = sc.nextLine();
		QCM_Creation.askVraie();
		vraie = this.askBoolean();
		
		return (new Reponse(libelle_r, vraie));
	}
	
	public void addToDatabase(HandlerQCM hQCM) {
		QCM qcm = new QCM(this.libelle, this.prive, this.createur, this.lQuestions);
		hQCM.getListe().add(qcm);
		QCM_Creation.addQCM(qcm.getLibelle());
	}
}
