package org.example.petshopdesktop;

public class Validator {

    /**
     * Checks if string is not blank
     * @param value string to check
     * @param name name of the input
     * @return error msg if string is blank, otherwise empty
     */
    public static String isPresent(String value, String name){
        String msg = "";
        if (value == null || value.isBlank()){
            msg += name + " is required. \n";
        }

        return msg;
    }

    /**
     * Checks if the input is a non-negative double
     * @param value input of string
     * @param name name of input
     * @return error msg if input is not a number or negative, otherwise empty
     */
    public static String isNonNegativeDouble(String value, String name){
        String msg ="";
        if (value == null) {
            msg += name + " must be a number.\n";

            return msg;
        }
        double result;
        try {
            result = Double.parseDouble(value);
            if (result < 0){
                msg += name + " must be greater than or equal 0. \n";
            }
        }
        catch (NumberFormatException e){
            msg += name + " must be a number.\n";
        }

        return msg;
    }

    /**
     * Checks if the input is a positive double
     * @param value input of string
     * @param name name of input
     * @return error msg if input is not a number or not positive, otherwise empty
     */
    public static String isPositiveDouble(String value, String name){
        String msg ="";
        if (value == null) {
            msg += name + " must be a number.\n";

            return msg;
        }
        double result;
        try {
            result = Double.parseDouble(value);
            if (result <= 0){
                msg += name + " must be greater than 0. \n";
            }
        }
        catch (NumberFormatException e){
            msg += name + " must be a number.\n";
        }

        return msg;
    }

    /**
     * Checks if the input is a double in 2 different range
     * @param value input of string
     * @param name name of input
     * @param minValue min value of range
     * @param maxValue max value of range
     * @return error msg if input is out of range, otherwise empty
     */
    public static String isDoubleInRange(String value, String name, double minValue, double maxValue){
        String msg ="";
        if (value == null) {
            msg += name + " must be a number.\n";

            return msg;
        }
        double result;
        try {
            result = Double.parseDouble(value);
            if (result < minValue || result > maxValue){
                msg += name + " must be between " + minValue + " and " + maxValue + "\n";
            }
        }
        catch (NumberFormatException e){
            msg += name + " must be a number.\n";
        }

        return msg;
    }

    /**
     * Checks if the input is a non-negative integer
     * @param value input of string
     * @param name name of input
     * @return error msg if input is not a number or negative, otherwise empty
     */
    public static String isNonNegativeInteger(String value, String name){
        String msg ="";
        int result;
        try {
            result = Integer.parseInt(value);
            if (result < 0){
                msg += name + " must be greater than or equal 0. \n";
            }
        }
        catch (NumberFormatException e){
            msg += name + " must be a whole number.\n";
        }

        return msg;
    }

    /**
     * Checks if the input is a positive integer
     * @param value input of string
     * @param name name of input
     * @return error msg if input is not a number or not positive, otherwise empty
     */
    public static String isPositiveInteger(String value, String name){
        String msg ="";
        int result;
        try {
            result = Integer.parseInt(value);
            if (result <= 0){
                msg += name + " must be greater than 0. \n";
            }
        }
        catch (NumberFormatException e){
            msg += name + " must be a whole number.\n";
        }

        return msg;
    }

    /**
     * check if the string is a given amount of characters or fewer
     * @param value input of string
     * @param name name of input
     * @param length max allowed length
     * @return error msg if input is more than given characters length
     */
    public static String isLessThanVarChars(String value, String name, int length){
        String msg ="";
        if (value == null || value.length() > length){
            msg += name + " must be less than " + length + " characters. \n";
        }

        return msg;
    }

    /**
     * Checks if the input is a valid email format
     * @param value input of string
     * @param name name of input
     * @return error msg if input is not a valid email format, otherwise empty
     */
    public static String isValidEmail(String value, String name){
        String msg = "";
        if (value == null) {
            msg += name + " is not in a valid format. \n";

            return msg;
        }
        String regex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";

        if (!value.matches(regex)){
            msg += name + " is not in a valid format. \n";
        }

        return msg;
    }

    /**
     * Checks if the input is a valid phone number in format (XXX) XXX-XXXX
     * @param value input of string
     * @param name name of input
     * @return error msg if input is not in valid phone format, otherwise empty
     */
    public static String isValidPhoneNumber(String value, String name){
        String msg = "";
        if (value == null) {
            msg += name + " must be in format (XXX) XXX-XXXX. \n";

            return msg;
        }
        String regex = "^\\(\\d{3}\\) \\d{3}-\\d{4}$";

        if (!value.matches(regex)){
            msg += name + " must be in format (XXX) XXX-XXXX. \n";
        }

        return msg;
    }
}
