package org.codiki.core.utils;

import org.mindrot.jbcrypt.BCrypt;

/**
 * Generic methods about {@link String} class.
 * 
 * @author takiguchi
 *
 */
public final class StringUtils {

	/**
	 * Indicate if {@code pString} is null or just composed of spaces.
	 * 
	 * @param pString
	 *            The string to test.
	 * @return {@code true} if {@code pString} is null or just composed of
	 *         spaces, {@code false} otherwise.
	 */
	public static boolean isNull(final String chaine) {
		return chaine == null || chaine.trim().length() == 0;
	}
	
	/**
	 * Hash the password given into parameters.
	 * 
	 * @param pPassword The password to hash.
	 * @return The password hashed.
	 */
	public static String hashPassword(final String pPassword) {
		return hashString(pPassword, 10);
	}
	
	public static String hashString(final String pString, final int pSalt) {
		return BCrypt.hashpw(pString, BCrypt.gensalt(pSalt));
	}
	
	/**
	 * Compare the password and the hashed string given into parameters.
	 * 
	 * @param pPassword
	 *            The password to compare to the hashed string.
	 * @param pHashToCompare
	 *            The hashed string to compare to the password.
	 * @return {@code true} if the password matches to the hashed string.
	 */
	public static boolean compareHash(final String pPassword, final String pHashToCompare) {
		return BCrypt.checkpw(pPassword, pHashToCompare);
	}
	
	/**
	 * Concatenate the parameters to form just one single string.
	 * 
	 * @param pArgs
	 *            The strings to concatenate.
	 * @return The parameters concatenated.
	 */
	public static String concat(final Object... pArgs) {
		final StringBuilder result = new StringBuilder();
		for (final Object arg : pArgs) {
			result.append(arg);
		}
		return result.toString();
	}
	
	public static String printStrings(final String... pStrings) {
		final StringBuilder result = new StringBuilder();
		for (int i = 0 ; i < pStrings.length ; i++) {
			result.append(pStrings[i]);
			if(i < pStrings.length - 1) {
				result.append(",");
			}
		}
		return result.toString();
	}
	
	public static boolean containLowercase(final String pString) {
		return RegexUtils.LOWER_LETTERS_PATTERN.matcher(pString).find();
	}
	
	public static boolean containUppercase(final String pString) {
		return RegexUtils.UPPER_LETTERS_PATTERN.matcher(pString).find();
	}
	
	public static boolean containNumber(final String pString) {
		return RegexUtils.NUMBER_PATTERN.matcher(pString).find();
	}
	
	public static boolean containSpecialChar(final String pString) {
		return RegexUtils.SPECIAL_CHAR_PATTERN.matcher(pString).find();
	}
}
