package com.zcxk.core.mongo.utils;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author Andy
 * @date 2021-07-22 11:03
 */
public class StringUtil {

    private StringUtil() {
        super();
    }

 
    public static String formatNull(String src) {
        return (src == null || "null".equals(src)) ? "" : src;
    }

 
    private static final String EMPTY_REGEX = "[\\s\\u00a0\\u2007\\u202f\\u0009-\\u000d\\u001c-\\u001f]+";

 
    public static boolean isEmpty(String input) {
        return input == null || "".equals(input) || input.matches(EMPTY_REGEX);
    }

    public static boolean isNotEmpty(String input) {
        return !isEmpty(input);
    }

    private static final String NUM_REG = "(\\+|\\-)?\\s*\\d+(\\.\\d+)?";

    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        }
        return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

 
    public static String toUpperCaseFirstOne(String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        }
        return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }


 
        public static boolean isNumber (String str){
            if (isEmpty(str)) {
                return false;
            }

            if (str.trim().matches(NUM_REG)) {
                return true;
            }

            return false;
        }

 
        public static boolean containUnreadableCode (String str){
            return contain(str, "\\ufffd");
        }

 
        public static boolean containNumber (String str){
            return contain(str, "\\d");
        }

   
        public static boolean containWord (String str){
            return contain(str, "\\w");
        }

 
        public static boolean containPunct (String str){
            return contain(str, PUNCT_REG);
        }

        public static boolean contain (String str, String regex){
            if (isEmpty(str) || isEmpty(regex)) {
                return false;
            }

            if (str.trim().matches(regex)) {
                return true;
            }

            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                return true;
            }

            return false;
        }

   
        public static String replaceAll (String input, String regex,
                String replacement){
            return Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(input)
                    .replaceAll(replacement);
        }

  
        public static String removeAllSpace (String text){
            if (isEmpty(text)) {
                return text;
            }

            return text.replaceAll("[ ]+", "");
        }

        private static final String PUNCT_REG = "[^a-zA-Z0-9\\u4e00-\\u9fa5]";

  
        public static String removeAllPunct (String str){
            if (isEmpty(str)) {
                return str;
            }

            return str.replaceAll(PUNCT_REG, "");
        }

  
        public static int countMatches (String str, String sub){
            if (isEmpty(str) || isEmpty(sub)) {
                return 0;
            }

            int count = 0;
            int idx = 0;
            while ((idx = str.indexOf(sub, idx)) != -1) {
                count++;
                idx += sub.length();
            }

            return count;
        }

  
        public static String substring (String str,int beginIndex, int endIndex){
            if (isEmpty(str)) {
                return str;
            }

            int length = str.length();

            if (beginIndex >= length || endIndex <= 0 || beginIndex >= endIndex) {
                return null;
            }

            if (beginIndex < 0) {
                beginIndex = 0;
            }
            if (endIndex > length) {
                endIndex = length;
            }

            return str.substring(beginIndex, endIndex);
        }

   
        public static Set<String> substring (String str, String sub){
            if (isEmpty(str) || isEmpty(sub)) {
                return null;
            }

            Set<String> result = new HashSet<String>();
            int idx = 0;
            while ((idx = str.indexOf(sub, idx)) != -1) {
                String temp = substring(str, idx - 1, idx + sub.length());
                if (!isEmpty(temp)) {
                    temp = removeAllPunct(temp);
                    if (!sub.equalsIgnoreCase(temp) && !containWord(temp)) {
                        result.add(temp);
                    }

                }

                temp = substring(str, idx, idx + sub.length() + 1);
                if (!isEmpty(temp)) {
                    temp = removeAllPunct(temp);
                    if (!sub.equalsIgnoreCase(temp) && !containWord(temp)) {
                        result.add(temp);
                    }
                }

                idx += sub.length();
            }

            return result;
        }
 

        public static boolean overLength (String str){
            if (isEmpty(str)) {
                return false;
            }

            return str.length() > 1 ? true : false;
        }

   
        public static String specialStr (String str){
            str = str.replaceAll("[^\\u4e00-\\u9fa5 | 0-9| a-zA-Z | \\.]+", " ")
                    .replaceAll("[\\.]{2,}", " ").trim();
            return str;
        }

  
        public static String replaceInValidateChar (String str){
            return str.replaceAll("[^a-zA-Z0-9\\u4e00-\\u9fa5\\s+]", " ");
        }

  
        public static String[] toHexString (String str){
            char[] chars = str.toCharArray();

            String[] result = new String[chars.length];

            for (int i = 0; i < chars.length; i++) {
                result[i] = Integer.toHexString((int) chars[i]);
            }

            return result;
        }

        public static String getUuid () {
            return UUID.randomUUID().toString();
        }

        public static boolean isUrl (String src){
            String regex = "http[s]?:\\/\\/([\\w-]+\\.[\\w-]+)(\\.[\\w-])+(:\\d{2,10})?.*";
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(src);
            return matcher.matches();
        }

 
        public static String escapeSql (String str){
            if (StringUtil.isNotEmpty(str)) {
                StringBuffer strbuff = new StringBuffer();
                for (String s : str.split("")) {
                    if ("%".equals(s) || "_".equals(s)|| "\\".equals(s)) {
                        strbuff.append("\\");
                    }
                    strbuff.append(s);
                }
                return strbuff.toString();
            }
            return str;
        }
    }
