package me.blackphreak.CommandHandling.Handlers;

import me.blackphreak.CommandHandling.AbstractEditCommandHandler;
import me.blackphreak.Database;

import static me.blackphreak.Lib.promptQuestion;
import static me.blackphreak.Lib.tryParseUInt;

public class EditStudentCommand extends AbstractEditCommandHandler {
	private String selectedColumn;
	
	public EditStudentCommand(String desc) {
		super(desc);
	}
	
	@Override
	public boolean handle() {
		boolean nextFlag;
		selectedColumn = null;
		
		do
		{
			nextFlag = selectStudentByID(false);
			
			// if lastCmd is set.
			if (lastCmd != null) {
				// in this case, lastCmd should be either "exit" nor "end".
				return lastCmd.equalsIgnoreCase("exit");
			}
		} while (!nextFlag);
		
		do {
			nextFlag = selectColumnToBeModified();
			
			if (lastCmd != null && lastCmd.equalsIgnoreCase("exit")) {
				return true;
			}
		} while (!nextFlag);
		
		doEdit();
		
		return true;
	}
	
	private boolean selectColumnToBeModified() {
		do {
			System.out.println("\nColumns:");
			for (int i = 0; i < SearchStudentCommand.unmodColNames.size(); i++) {
				System.out.println(String.format(
						"  %2s. %-15s",
						(i+1) + "",
						SearchStudentCommand.unmodColNames.get(i)
				));
			}
			
			// delete option
			System.out.println(String.format(
					"  %2s. %-15s",
					"98",
					"!! Delete Selected Student(s) !!"
			));
			
			// exit option
			System.out.println(String.format(
					"  %2s. %-15s",
					"99",
					"Back to main menu"
			));
			
			var inp = promptQuestion("Please select one column to edit: ");
			var selected = tryParseUInt(inp, "Invalid column index");
			if (selected == -1)
				continue;
			
			if (selected == 98)
			{
				System.out.println("Are you sure to DELETE selected student(s)? This process is IRREVERSIBLE!");
				System.out.println("** DEFAULT action is back to main menu **");
				System.out.println("** type \"confirm\" to delete **");
				var confirmation = promptQuestion("confirm deletion? > ", false);
				if (confirmation.equalsIgnoreCase("confirm")) {
					var updated = Database.getInstance().update("DELETE FROM Student WHERE StudentID IN (\""
							+ selectedIDs() + "\");");
					
					System.out.println("Deleted " + updated + " student(s).");
					System.out.println("Deleted student(s): " + String.join(", ", selectedStudentID));
				}
				
				lastCmd = "exit";  // back to main menu
				return false;
			}
			
			if (selected == 99)
			{
				lastCmd = "exit";
				return false;
			}
			
			if (selected <= 0
					|| selected > SearchStudentCommand.unmodColNames.size())
			{
				System.out.println("Invalid column index. [Out of range]");
				continue;
			}
			
			selectedColumn = SearchStudentCommand.unmodColNames.get(selected-1);
			return true;
			
		} while (true);
	}
	
	private void doEdit() {
		var newValue = promptQuestion("The value that you want it to be: ");
		var updated = Database.getInstance().update("UPDATE Student SET " + selectedColumn
				+ " = \""+newValue+"\" WHERE StudentID IN (\"" + selectedIDs() + "\");");
		
		if (updated <= 0)
			System.out.println("No changes have been made.");
		else
			System.out.println("Update successfully made to "+updated+" student(s).");
	}
	
	private String selectedIDs() {
		return String.join("\",\"", selectedStudentID);
	}
}
