package me.blackphreak.CommandHandling.Handlers;

import me.blackphreak.CommandHandling.AbstractCommandHandler;
import me.blackphreak.Debug.Debug;
import me.blackphreak.Student.AbstractStudent;
import me.blackphreak.Student.Types.*;

import static me.blackphreak.Lib.*;

public class CreateCommand extends AbstractCommandHandler {
	public CreateCommand(String desc) {
		super(desc);
	}
	
	@Override
	public void handle() {
		AbstractStudent stu = null;
		
		do {
			var inp = promptQuestion("Enter type of the student [local | oversea]: ");
			
			if (inp.equalsIgnoreCase("local")) {
				stu = new LocalStudent();
			}
			else if (inp.equalsIgnoreCase("oversea")) {
				stu = new OverseaStudent();
			}
			else {
				System.out.println("Invalid type. Please try again.");
			}
		} while (stu == null);
		
		System.out.println("  Please provide information about the student:");
		do {
			var inp = promptQuestion("    Chinese Name: ");
			
			if (stu instanceof LocalStudent)
			{
				if (inp.isEmpty() || inp.isBlank())
					System.out.println("Invalid. Please try again.");
				else
					stu.setChtName(inp);
			}
			else
				stu.setChtName("");
		} while (stu.getChtName() == null);
		
		do {
			var inp = promptQuestion("    English Name: ");
			
			if (inp.isEmpty() || inp.isBlank())
				System.out.println("Invalid. Please try again.");
			else
				stu.setEngName(inp);
		} while (stu.getEngName() == null);
		
		do {
			var inp = promptQuestion("    Home Address: ");
			
			if (inp.isEmpty() || inp.isBlank())
				System.out.println("Invalid. Please try again.");
			else
				stu.setHomeAddress(inp);
		} while (stu.getHomeAddress() == null);
		
		do {
			var inp = promptQuestion("    Mobile Number: ");
			
			if (inp.isEmpty()
					|| inp.isBlank()
					|| !inp.matches("\\d+"))
				System.out.println("Invalid. Please try again.");
			else
				stu.setMobileNumber(inp);
		} while (stu.getMobileNumber() == null);
		
		do {
			var inp = promptQuestion("    Nationality: ")
					.toUpperCase();
			
			if (inp.isEmpty()
					|| inp.isBlank())
				System.out.println("Invalid nationality. Please try again.");
			else
				stu.setNationality(inp);
		} while (stu.getNationality() == null);
		
		do {
			var inp = promptQuestion("    Current Semester: ");
			
			if (inp.isEmpty()
					|| inp.isBlank()
					|| !inp.matches("\\d{1,1}"))
				System.out.println("Invalid. Please try again.");
			else
				stu.setSemester(Integer.parseInt(inp));
		} while (stu.getSemester() == 0);
		
		String tryAgain;
		do {
			if (!stu.allocateStudentID()) {
				Debug.err("Failed to allocate studentID!");
				do {
					tryAgain = promptQuestion(
							"** n: will discard all unsaved changes! **\n" +
									"Try to allocate again? [Y/n]: "
					);
					
					// check for tryAgain option
					// if not empty (not using the default answer)
					// AND not a valid option (not either Y nor n)
					//      prompt the question again
				} while (!tryAgain.isEmpty() && (
						!tryAgain.equalsIgnoreCase("Y")
								|| !tryAgain.equalsIgnoreCase("n")
				));
			}
			else
				break;
		} while (tryAgain.isEmpty() || tryAgain.equalsIgnoreCase("Y"));
		
		if (stu.getStudentID().isEmpty()) {
			Debug.err("Failed to allocate studentID. Exiting..");
			System.exit(1);
			return;
		}
		
		Debug.info("Student["+stu.getStudentID()+"] created. Saving to database...");
		
		// TODO: save to db
		
		Debug.info("Success. Exiting entry mode.");
	}
	
	@Override
	protected void doneAndReset() {
	
	}
}
