package com.njyunzhi.pet_identity.common;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    public static boolean isEmpty(String str) {
        return null == str || "".equals(str.trim()) || "null".equals(str) || "undefined".equals(str);
    }

    public static String trim(String src, String...st) {
        if (null == src) return src;
        if (null == st || st.length == 0) return src.trim();

        String start = st[0];
        if (!src.startsWith(start)) {
            return src;
        }

        return src.substring(start.length());
    }

    public static String trimEnd(String src, String str) {
        if (null == src || null == str) {
            return src;
        }

        if (src.endsWith(str)) {
            return src.substring(0, src.length() - str.length());
        }

        return src;
    }

    public static String ifNull(String src, String defaultVal) {
        return isEmpty(src) ? defaultVal : src;
    }

    public static String random(int length) {
        String str="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String urlEncode(String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return str;
        }
    }

    public static String humpToLine(String str) {
        if (null == str || "".equals(str)) return "";

        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String strToUnicode(String str) {
        char[] chars = str.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
            returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        return returnStr;
    }

    public static String repeat(String src, int len) {
        if (null == src) {
            return null;
        }

        if (len <= 0) {
            return src;
        }

        String res = "";
        for (int i = 0; i < len; i ++) {
            res += src;
        }

        return res;
    }

    public static String lpad(String src, String padStr, int len) {
        if (null == src) {
            return null;
        }

        if (len <= 0 || src.length() >= len) {
            return src;
        }

        if (null == padStr) {
            padStr = "";
        }

        int padLen = len - src.length();
        String prefix = repeat(padStr, padLen);
        prefix = prefix.substring(0, padLen);
        return prefix + src;
    }
}
