package me.blackphreak;

import com.google.gson.Gson;
import me.blackphreak.Debug.Debug;
import me.blackphreak.Debug.DebugLevel;
import me.blackphreak.Student.AbstractStudent;
import me.blackphreak.Student.Types.LocalStudent;
import me.blackphreak.Student.Types.OverseaStudent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Lib {
	private static BufferedReader reader = new BufferedReader(
			new InputStreamReader(System.in)
	);
	
	public static AbstractStudent jsonToStudent(String json) {
		try {
			if (json.contains("HKID"))
				return new Gson().fromJson(json, LocalStudent.class);
			else if (json.contains("VISA"))
				return new Gson().fromJson(json, OverseaStudent.class);
			else
				return null;  // unknown
		}
		catch (Exception ex) {
			Debug.err(ex.getMessage());
		}
		
		return null;
	}
	
	public static String promptQuestion(String question) {
		return promptQuestion(question, true);
	}
	
	public static String promptQuestion(String question, boolean newLine) {
		try {
			System.out.print((newLine ? "\n" : "") + question);
			return reader.readLine();
		} catch (IOException ex) {
			Debug.err("Failed to prompt for question ["+question+"]");
			return "";
		}
	}
	
	public static int tryParseUInt(String inp) {
		return tryParseUInt(inp, "The number you provided is invalid. Please try again.");
	}
	public static int tryParseUInt(String inp, String failMsg) {
		int retn = -1;
		try {
			
			if (!inp.isEmpty() && !inp.isBlank())
			{
				retn = Integer.parseUnsignedInt(inp);
			}
			else
			{
				System.out.println(failMsg);
			}
		} catch (NumberFormatException ex) {
			Debug.log(DebugLevel.ERR.getLevel(), 2, failMsg);
		}
		return retn;
	}
}
