package it.micegroup.voila2.cartridge.library.javaservice;

import it.micegroup.voila2.cartridge.library.formatters.PrettierFormatter;
import it.micegroup.voila2.cartridge.library.formatters.SimpleXmlFormatterPostProcessor;

public class StringUtils {
	private JavaBeautifier javaFormatter;
	private SimpleXmlFormatterPostProcessor xmlFormatter;
	private PrettierFormatter prettierFormatter;

	public StringUtils() {
		super();

		javaFormatter = new JavaBeautifier();
		xmlFormatter = new SimpleXmlFormatterPostProcessor();
		prettierFormatter = new PrettierFormatter();
	}

	/**
	 * Take a string in input returning it in lowercase and with an underscore for
	 * each camel case
	 * 
	 * @author Vittorio Niespolo <vittorio.niespolo@micegroup.it>
	 * @param input
	 * @return Underscored version of input string
	 * 
	 */
	public static String camelToUnderscore(String input) {
		if (input == null) {
			return "";
		}
		String regex = "([a-z])([A-Z]+)";
		String replacement = "$1_$2";
		String result = input.replaceAll(regex, replacement);//.toLowerCase();
		return result;
	}

	/**
	 * Take a string in input returning it in lowercase and with a dash for each
	 * camel case
	 * 
	 * @param input
	 * @return Dashed version of input string
	 * @author Vittorio Niespolo <vittorio.niespolo@micegroup.it>
	 */
	public String camelToDash(String input) {
		if (input == null) {
			return "";
		}
		String regex = "([a-z])([A-Z]+)";
		String replacement = "$1-$2";
		String result = input.replaceAll(regex, replacement).toLowerCase();
		return result;
	}

	/**
	 * Method to convert a dash string into camelCase
	 * 
	 * @author Simone Punzo <simone.punzo@micegroup.it>
	 * @param value
	 * @return
	 */

	public static String dashToCamelCase(String value) {
		StringBuilder sb = new StringBuilder(value.length());
		boolean upper = false;

		for (char c : value.toCharArray()) {
			if (c == '-') {
				upper = true;
				continue;
			}
			if (upper) {
				sb.append(Character.toUpperCase(c));
			} else {
				sb.append(c);
			}
			upper = false;
		}
		return sb.toString();
	}

	/**
	 * Take a string in input returning it splitted and capitalized camel case
	 * 
	 * @param input
	 * @return
	 */
	public static String splitCamel(String input) {
		if (input == null) {
			return "";
		}
		String regex = "([a-z])([A-Z]+)";
		String replacement = "$1 $2";
		String result = input.replaceAll(regex, replacement);

		result = result.substring(0, 1).toUpperCase() + result.substring(1);
		return result;
	}

	/**
	 * Take code in input and return it formatted using eclipse javaFormatter. This
	 * method can be extended adding a parameter to choose CodeFormatter.
	 * 
	 * @author Vittorio Niespolo <vittorio.niespolo@micegroup.it>
	 * @param input
	 * @return formattedOutput
	 */
	public String formatter(String input, String fileName) {
		String formattedOutput = input;
		if (fileName != null) {
			if (fileName.endsWith(".java")) {
				formattedOutput = javaFormatter.format(input);
			} else if (fileName.endsWith(".ts")) {
				// formattedOutput = prettierFormatter.format(input, fileName);
			} else if (fileName.endsWith(".xml")) {
				formattedOutput = xmlFormatter.format(input);
			}
		}
		return formattedOutput;
	}

	/**
	 * Return the string with first letter capitalized
	 * 
	 * @author Vittorio Niespolo <vittorio.niespolo@micegroup.it>
	 * @param value
	 * @return
	 */
	public static String toUpperFirst(String value) {
		// Convert String to char array.
		char[] array = value.toCharArray();
		if (array == null || array.length == 0) {
			return "";
		}
		// Modify first element in array.
		array[0] = Character.toUpperCase(array[0]);
		// Return string.
		return new String(array);
	}

	public static String encodeDDL(String value) {
		return value.replaceAll("'", "''").replaceAll("&quot;", "''");
	}

	public static String removeHtmlTags(String value) {
		return value.replaceAll("<p>", "").replaceAll("</p>", "");
	}

	/**
	 * Return a number formatted on N digits
	 * 
	 * @param value          number to format
	 * @param numberOfDigits number o digits
	 * @return
	 */
	public static String getFormattedNumber(Integer value, Integer numberOfDigits) {
		return String.format("%0" + numberOfDigits + "d", value);
	}
	
	
}
