package me.chyxion.tigon.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Oct 31, 2014 1:27:57 PM
 */
public class WordUtils {

    /**
     * split Camel Case
     * <pre>
     * nice - [nice]
     * World - [World]
     * MySQL - [My SQL]
     * HTML - [HTML]
     * JSONObject - [JSON Object]
     * JPanel - [J Panel]
     * toJSONString - [to JSON String]
     * Log4j - [Log4j]
     * 99Roses - [99 Roses]
     * DO178 - [DO178]
     * Do178 - [Do178]
     * </pre>
     * @param str word
     * @return split result
     */
    public static String[] splitToWords(String str) {
        return StringUtils.isNotBlank(str) ? 
                    // JSONObject - JSON Object
                str.split(new StringBuilder("(?<=[A-Z])(?=[A-Z][a-z])")
                    // MySQL - My SQL
                    .append("|(?<=[a-z])(?=[A-Z])")
                    // 99Roses -> 99 Roses
                    .append("|(?<=[^a-zA-Z])(?=[A-Z])")
                    // .append("|(?<=[^A-Z])(?=[A-Z])")
                    // 5s - 5 s
                    // .append("|(?<=[^a-zA-Z])(?=[a-z])")
                    // A3 - A 3 | a3 - a 3
                    // .append("|(?<=[A-Za-z])(?=[^A-Za-z])")
                    .toString()) : 
                new String[]{};
    }

    /**
     * Join Camel Case Word
     * <pre>
     * #convertCamelCase("nice", "_") - [nice]
     * #convertCamelCase("World", "_") - [World]
     * #convertCamelCase("MySQL", "_") - [My_SQL]
     * #convertCamelCase("HTML", "_") - [HTML]
     * #convertCamelCase("JSONObject", "_") - [JSON_Object]
     * #convertCamelCase("JPanel", "_") - [J_Panel]
     * #convertCamelCase("toJSONString", "_") - [to_JSON_String]
     * #convertCamelCase("Log4J", "_") - [Log_4_J]
     * #convertCamelCase("Log4j", "_") - [Log_4_j]
     * #convertCamelCase("99Roses", "_") - [99_Roses]
     * #convertCamelCase("Varchar2", "_") - [Varchar_2]
     * #convertCamelCase("DO178", "_") - [DO_178]
     * #convertCamelCase("Do178", "_") - [Do_178]
     * #convertCamelCase("Do178", "-") - [Do-178]
     * ...
     * </pre>
     * @param str word
     * @param separator separator
     * @return split result
     */
    public static String convertCamelCase(String str, String separator) {
        return StringUtils.join(splitToWords(str), separator);
    }

    /**
     * <pre>
     * foo_bar - fooBar
     * foo,bar - fooBar
     * </pre>
     * @param str word
     * @param separator separator
     * @return split result
     */
    public static String convertToCamelCase(String str, String separator) {
        String strRtn = null;
        if (StringUtils.isNotBlank(str)) {
            StringBuilder sbRtn = new StringBuilder();
            for (String s : str.split(separator)) {
                sbRtn.append(StringUtils.capitalize(s));
            }
            strRtn = sbRtn.toString();
        }
        else {
            strRtn = str;
        }
        return strRtn;
    }

    /**
     * convert underline word to
     * @param word
     * @return
     */
    public static String toCamel(final String word) {
        return StringUtils.uncapitalize(convertToCamelCase(word.toLowerCase(), "_"));
    }
}
