package controller.console.enseignant;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

import view.console.enseignant.Session_Modification;

import model.Enseignant;
import model.Module;
import model.Promotion;
import model.QCM;
import model.Session;
import model.list.HandlerModules;
import model.list.HandlerPromotions;
import model.list.HandlerSessions;

public class Session_ModificationControle {
	private Scanner sc;
	private final DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
	
	private Session session;
	
	private ArrayList<Promotion> lProm;
	private ArrayList<Module> lMod;
	private ArrayList<QCM> lQCM;
	private ArrayList<Session> lSess;
	
	public Session_ModificationControle(ArrayList<Session> lSess, ArrayList<Promotion> lProm, ArrayList<Module> lMod, ArrayList<QCM> lQCM, Enseignant e_connected) {
		this.lProm = lProm;
		this.lMod = lMod;
		this.lQCM = lQCM;
		this.lSess = HandlerSessions.getOwn(e_connected, lSess);
		sc = new Scanner(System.in);
		
		Session_Modification.trigger();
		Session_Modification.showSessions(this.lSess);
		this.askSession();
	}
	
	private void askSession() {
		String str_input;
		int input;
		boolean done = false;

		while (!done) {
			str_input = sc.nextLine();
			try {
				input = Integer.parseInt(str_input);
				if ((input < 0) || (input > this.lSess.size())) {
					Session_Modification.fail(0, this.lSess.size());
				} else if (input == 0) {
					done = true;
				} else {
					this.session = this.lSess.get(input-1);
					this.askField();
				}
			} catch (NumberFormatException e) {
				Session_Modification.fail(0, this.lSess.size());
			}
		}
	}
	
	private void askField() {
		int input;
		boolean done = false;
		
		while (!done) {
			Session_Modification.askField(this.session);
			try {
				input = sc.nextInt();
				sc.nextLine();
				if (input < 0 || input > 6) {
					Session_Modification.fail(0, 6);
				} else {
					if (input == 0) { done = true; }
					else if (input == 1) {
						this.askDateDebut();
					} else if (input == 2) {
						this.askDateFin();
					} else if (input == 3) {
						this.askRepetition();
					} else if (input == 4) {
						this.askPromotion();
					} else if (input == 5) {
						this.askModule();
					} else {
						this.askQCM();
					}
				}
			} catch (InputMismatchException e) {
				Session_Modification.fail(0, 6);
			}
		}
	}
	
	private void askDateDebut() {
		String input;
		Date dateDebut;
		boolean done = false;
		while (!done) {
			try {
				Session_Modification.askDateDebut();
				input = sc.nextLine();
				dateDebut = this.dateFormat.parse(input);
				this.session.setDateDebut(dateDebut);
				done = true;
			} catch (ParseException e) {
				Session_Modification.failDate();
			}
		}
	}
	
	private void askDateFin() {
		String input;
		Date dateFin;
		boolean done = false;
		while (!done) {
			try {
				Session_Modification.askDateFin();
				input = sc.nextLine();
				dateFin = this.dateFormat.parse(input);
				this.session.setDateFin(dateFin);
				done = true;
			} catch (ParseException e) {
				Session_Modification.failDate();
			} catch (Session.InvalidDate e) {
				Session_Modification.failDate(e.getDate(), true);
			}
		}
	}
	
	private void askRepetition() {
		String input;
		int repetition;
		boolean done = false;
		while (!done) {
			try {
				Session_Modification.askRepetition();
				input = sc.nextLine();
				repetition = Integer.parseInt(input);
				if (repetition < 1) {
					throw new NumberFormatException();
				} else {
					done = true;
					this.session.setRepetition(repetition);
				}
			} catch (NumberFormatException e) {
				Session_Modification.fail(1, Integer.MAX_VALUE);
			}
		}
	}
	
	private void askPromotion() {
		String str_input;
		int input;
		Promotion promotion;
		boolean done = false;

		while (!done) {
			Session_Modification.showPromotions(lProm);
			str_input = sc.nextLine();
			try {
				input = Integer.parseInt(str_input);
				if ((input < 1) || (input > this.lProm.size())) {
					Session_Modification.fail(1, this.lProm.size());
				} else {
					promotion = this.lProm.get(input-1);
					done = true;
					this.session.setPromotion(promotion);
				}
			} catch (NumberFormatException e) {
				promotion = HandlerPromotions.getPromotion(str_input, this.lProm);
				if (promotion == null) {
					Session_Modification.failPromotion();
				}
			}
		}
	}
	
	private void askModule() {
		String str_input;
		int input;
		Module module;
		boolean done = false;

		while (!done) {
			Session_Modification.showModules(lMod);
			str_input = sc.nextLine();
			try {
				input = Integer.parseInt(str_input);
				if ((input < 1) || (input > this.lMod.size())) {
					Session_Modification.fail(1, this.lMod.size());
				} else {
					module = this.lMod.get(input-1);
					done = true;
					this.session.setModule(module);
				}
			} catch (NumberFormatException e) {
				module = HandlerModules.getModule(str_input, this.lMod);
				if (module == null) {
					Session_Modification.failModule();
				}
			}
		}
	}
	
	private void askQCM() {
		String str_input;
		QCM qcm;
		int input;
		boolean done = false;

		while (!done) {
			Session_Modification.showQCM(lQCM);
			str_input = sc.nextLine();
			try {
				input = Integer.parseInt(str_input);
				if ((input < 1) || (input > this.lQCM.size())) {
					Session_Modification.fail(1, this.lQCM.size());
				} else {
					qcm = this.lQCM.get(input-1);
					done = true;
					this.session.setQcm(qcm);
				}
			} catch (NumberFormatException e) {
				Session_Modification.fail(1, this.lQCM.size());
			}
		}
	}
	
	public void updateDatabase() {
		Session_Modification.updateSession();
	}
}
