//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.chushang.common.core.text;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson2.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ToolUtils {
    private static final Logger log = LoggerFactory.getLogger(ToolUtils.class);

    public ToolUtils() {
    }

    public static boolean isBlank(Object obj) {
        if (obj != null && !"".equals(obj) && !StringUtils.isBlank(obj.toString())) {
            if (obj instanceof JSONArray) {
                return ((JSONArray) obj).isEmpty();
            } else if (obj instanceof CharSequence) {
                return ((CharSequence) obj).isEmpty();
            } else if (obj instanceof Collection) {
                return ((Collection<?>)obj).isEmpty();
            } else if (obj instanceof Map) {
                return ((Map<?, ?>)obj).isEmpty();
            } else if (!(obj instanceof Object[] object)) {
                return false;
            } else {
                if (object.length == 0) {
                    return true;
                } else {
                    boolean empty = true;
                    for (Object o : object) {
                        if (!isBlank(o)) {
                            empty = false;
                            break;
                        }
                    }
                    return empty;
                }
            }
        } else {
            return true;
        }
    }

    public static String dencodeURI(String beforeStr) {
        return beforeStr != null ? URLDecoder.decode(beforeStr, StandardCharsets.UTF_8) : null;
    }

    public static String dencodeURI(String beforeStr, String charset) {
        try {
            return beforeStr != null ? URLDecoder.decode(beforeStr, charset) : null;
        } catch (UnsupportedEncodingException var3) {
            log.error("dencode Exception", var3);
            var3.printStackTrace();
            return "";
        }
    }

    public static String encodeString(String beforeStr, String charset) {
        try {
            return beforeStr != null ? new String(beforeStr.getBytes(StandardCharsets.ISO_8859_1), charset) : null;
        } catch (UnsupportedEncodingException var3) {
            log.error(" ISO-8859-1 to Byte convert to  " + charset + " exception", var3);
            var3.printStackTrace();
            return "";
        }
    }

    public static String encodeURI(String beforeStr) {
        return beforeStr != null ? URLEncoder.encode(beforeStr, StandardCharsets.UTF_8) : null;
    }

    public static String encodeURI(String beforeStr, String charset) {
        try {
            return beforeStr != null ? URLEncoder.encode(beforeStr, charset) : null;
        } catch (UnsupportedEncodingException var3) {
            log.error("URLEncoder encode by " + charset + " Exception", var3);
            var3.printStackTrace();
            return "";
        }
    }

    public static boolean isCharacter(String str) {
        boolean temp = false;
        temp = str.matches("[\\u4e00-\\u9fa5]");
        return temp;
    }

    public static boolean isDate(String str) {
        String eL = "^((\\d{2}(([02468][048])|([13579][26]))[\\-/\\s]?((((0?[13578])|(1[02]))[\\-/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-/\\s]?((((0?[13578])|(1[02]))[\\-/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        Pattern pattern = Pattern.compile(eL);
        return pattern.matcher(str).matches();
    }

    public static boolean isEmpty(List<?> list) {
        return isNull(list) || list.isEmpty();
    }

    public static boolean isEmpty(Object[] obj) {
        return isNull(obj) || obj.length == 0;
    }

    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str);
    }

    public static boolean isNull(Object object) {
        return object == null;
    }

    public static boolean isLetter(String inputString) {
        return inputString != null && inputString.matches("[a-zA-Z]+");
    }

    public static boolean isMail(String smail) {
        Pattern pattern = Pattern.compile("[\\w.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+", 2);
        return pattern.matcher(smail).matches();
    }

    public static boolean isMobileNumber(String num) {
        String regex = "^((13[0-9])|(14[5,79])|(15([0-3]|[5-9]))|(166)|(17[0,135678])|(18[0-9])|(19[0-9]))\\d{8}$";
        if (num.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(num);
            return m.matches();
        }
    }

    public static boolean isNotEmpty(String str) {
        return str != null && !str.trim().isEmpty();
    }

    public static boolean isNumber(String inputString) {
        return inputString != null && inputString.matches("[0-9]+");
    }

    public static double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        } else {
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            return b1.divide(b2, scale, 4).doubleValue();
        }
    }

    public static boolean isZipCodeNumber(String num) {
        return Pattern.matches("\\d{6}", num);
    }

    public static String random(int n) {
        StringBuilder s = new StringBuilder();

        for(int i = 0; i < n; ++i) {
            int a = (int)(Math.random() * 10.0);
            s.append(a);
        }

        return s.toString();
    }

    public static String randomChar(int n) {
        StringBuilder str = new StringBuilder();
        char[] cha = new char[52];

        int i;
        for(i = 0; i < 26; ++i) {
            cha[i] = (char)(65 + i);
        }

        while(i < 52) {
            cha[i] = (char)(71 + i);
            ++i;
        }

        Random random = new Random();

        for(i = 0; i < n; ++i) {
            int rand = random.nextInt(52);
            str.append(cha[rand]);
        }

        return str.toString();
    }

    public static String randomString(int n) {
        String str = "";
        char[] cha = new char[62];

        int i;
        for(i = 0; i < 26; ++i) {
            cha[i] = (char)(65 + i);
        }

        while(i < 52) {
            cha[i] = (char)(71 + i);
            ++i;
        }

        while(i < 62) {
            cha[i] = (char)(-4 + i);
            ++i;
        }

        Random random = new Random();

        for(i = 0; i < n; ++i) {
            int rand = random.nextInt(62);
            str = str + cha[rand];
        }

        return str;
    }

    public static Serializable[] split(String str, String expression) {
        if (str != null && str != "") {
            StringTokenizer a = new StringTokenizer(str, expression);
            int i = 0;
            Serializable[] strs = new Serializable[a.countTokens()];
            if (a.countTokens() > 0) {
                try {
                    while(a.hasMoreTokens()) {
                        strs[i] = a.nextToken();
                        ++i;
                    }
                } catch (ArrayIndexOutOfBoundsException var6) {
                    ArrayIndexOutOfBoundsException e = var6;
                    log.error("数组越界", e);
                    e.printStackTrace();
                }
            }

            return strs;
        } else {
            Serializable[] o = new Serializable[0];
            return o;
        }
    }

    public static String arrayToStr(Serializable[] array, String insertString) {
        String strs = "";
        if (array != null) {
            for(int i = 0; i < array.length; ++i) {
                if (i < array.length - 1) {
                    strs = strs + array[i] + insertString;
                } else {
                    strs = strs + array[i];
                }
            }
        }

        return strs;
    }

    public static String[] strSplit(String str, String expression) {
        if (str != null && str != "") {
            StringTokenizer a = new StringTokenizer(str, expression);
            int i = 0;
            String[] strs = new String[a.countTokens()];
            if (a.countTokens() > 0) {
                try {
                    while(a.hasMoreTokens()) {
                        String token = a.nextToken();
                        if (!isEmpty(token) && !isEmpty(token = token.trim())) {
                            strs[i] = token;
                            ++i;
                        }
                    }
                } catch (ArrayIndexOutOfBoundsException var6) {
                    ArrayIndexOutOfBoundsException e = var6;
                    log.error("数组越界", e);
                    e.printStackTrace();
                }
            }

            return strs;
        } else {
            String[] o = new String[0];
            return o;
        }
    }

    public static String unicodeEncoding(String str) {
        StringBuffer unicodeBytes = new StringBuffer();

        for(int byteIndex = 0; byteIndex < str.length(); ++byteIndex) {
            String hexB = Integer.toHexString(str.charAt(byteIndex));
            unicodeBytes.append("\\u");
            if (hexB.length() <= 2) {
                unicodeBytes.append("00");
            }

            unicodeBytes.append(hexB);
        }

        return unicodeBytes.toString();
    }
}
