package org.smartframework.common.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ExStringUtils {
    public ExStringUtils() {
    }

    public static boolean isEmail(String email) {
        if(email == null) {
            return false;
        } else {
            String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
            Pattern p = Pattern.compile(str);
            Matcher m = p.matcher(email);
            return m.matches();
        }
    }

    public static boolean isMobile(String mobile) {
        if(mobile == null) {
            return false;
        } else {
            Pattern p = null;
            Matcher m = null;
            boolean b = false;
            p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$");
            m = p.matcher(mobile);
            b = m.matches();
            return b;
        }
    }

    public static String concat(String[] values) {
        if(values == null) {
            return null;
        } else {
            StringBuffer buffer = new StringBuffer();
            String[] var2 = values;
            int var3 = values.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String value = var2[var4];
                if(buffer.length() > 0) {
                    buffer.append(",");
                }

                buffer.append(value);
            }

            return buffer.toString();
        }
    }

    public static String trimAllSpace(String str) {
        return str == null?str:str.replaceAll("^[\\s　]*|[\\s　]*$", "");
    }

    public static String[] split(String aStr, String split) {
        String[] columnNames = StringUtils.split(aStr, split);
        if(columnNames != null) {
            for(int i = 0; i < columnNames.length; ++i) {
                if(columnNames[i] != null) {
                    columnNames[i] = columnNames[i].trim();
                } else {
                    columnNames[i] = "";
                }
            }
        }

        return columnNames;
    }

    public static boolean isInclude(String item, String[] items) {
        if(items != null && item != null) {
            for(int i = 0; i < items.length; ++i) {
                if(item.equalsIgnoreCase(items[i])) {
                    return true;
                }
            }
        }

        return false;
    }

    public static boolean isBlank(String argStr) {
        return StringUtils.isBlank(argStr);
    }

    public static boolean isNotBlank(String argStr) {
        return StringUtils.isNotBlank(argStr);
    }

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

    public static boolean isNotEmpty(String argStr) {
        return StringUtils.isNotEmpty(argStr);
    }

    public static String getDocDesc(String docValue) {
        if(docValue != null) {
            int beginIndex = docValue.indexOf("[");
            return docValue.substring(0, beginIndex);
        } else {
            return null;
        }
    }

    public static String getDocNum(String docValue) {
        if(docValue != null) {
            int beginIndex = docValue.indexOf("[");
            int endIndex = docValue.indexOf("]");
            if(endIndex > beginIndex) {
                return docValue.substring(beginIndex + 1, endIndex);
            }
        }

        return null;
    }

    public static int parseInt(String str) {
        return parseInt(str, -1);
    }

    public static int parseInt(String str, int defaultValue) {
        int intValue = defaultValue;

        try {
            if("null".equalsIgnoreCase(str)) {
                intValue = defaultValue;
            } else {
                intValue = Integer.parseInt(str);
            }
        } catch (Exception var4) {
            ;
        }

        return intValue;
    }

    public static long parseLong(String str, long defaultValue) {
        long longValue = defaultValue;

        try {
            if("null".equalsIgnoreCase(str)) {
                longValue = defaultValue;
            } else {
                longValue = Long.parseLong(str);
            }
        } catch (Exception var6) {
            ;
        }

        return longValue;
    }

    public static int parseVerNum(String str) {
        int verNum = 200;

        try {
            str = str.replaceAll("\\.", "");
            verNum = Integer.parseInt(str);
        } catch (Exception var3) {
            ;
        }

        return verNum;
    }

    public static String getOption(String optionsStr, String keyName, String defValue) {
        if(optionsStr != null) {
            String[] options = split(optionsStr, ",");

            for(int i = 0; i < options.length; ++i) {
                if(options[i] != null && options[i].startsWith(keyName)) {
                    String value = options[i].substring(keyName.length() + 1);
                    return value;
                }
            }
        }

        return defValue;
    }

    public static String getVerifyCode(int codeLen) {
        int count = 0;
        char[] str = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer verifyCode = new StringBuffer("");
        Random r = new Random();

        while(count < codeLen) {
            int i = Math.abs(r.nextInt(10));
            if(i >= 0 && i < str.length) {
                verifyCode.append(str[i]);
                ++count;
            }
        }

        return verifyCode.toString();
    }

    public static String firstCharToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }

    public static String replaceUnderlineAndfirstToUpper(String srcStr) {
        String newString = "";
        String org = "_";
        String ob = "";
        boolean var4 = false;

        while(srcStr.indexOf(org) != -1) {
            int first = srcStr.indexOf(org);
            if(first != srcStr.length()) {
                newString = newString + srcStr.substring(0, first) + ob;
                srcStr = srcStr.substring(first + org.length(), srcStr.length());
                srcStr = firstCharToUpper(srcStr);
            }
        }

        newString = newString + srcStr;
        return newString;
    }

    public static String parseTableAlias(String tableName) {
        int beginIndex = tableName.indexOf("_");
        int endIndex = tableName.lastIndexOf("_");
        if(beginIndex < 0) {
            return tableName;
        } else if(beginIndex == endIndex) {
            return tableName.substring(beginIndex + 1);
        } else {
            int tmpInt = -1;
            String suffix = tableName.substring(endIndex);
            if(suffix != null && suffix.length() > 1) {
                try {
                    tmpInt = Integer.parseInt(suffix.substring(1));
                } catch (Exception var6) {
                    tmpInt = -1;
                }
            }

            return tmpInt > 0?tableName.substring(beginIndex + 1, endIndex):tableName.substring(beginIndex + 1);
        }
    }

    public static String getTableAlias(String tableName) {
        String aliasName = parseTableAlias(tableName);
        if("user".equalsIgnoreCase(aliasName)) {
            aliasName = "app" + aliasName;
        }

        return aliasName;
    }

    public static String[] mergeStrArray(String[] array1, String[] array2) {
        List<String> list = new ArrayList();
        String[] var3;
        int var4;
        int var5;
        String s;
        if(array1 != null) {
            var3 = array1;
            var4 = array1.length;

            for(var5 = 0; var5 < var4; ++var5) {
                s = var3[var5];
                list.add(s);
            }
        }

        if(array2 != null) {
            var3 = array2;
            var4 = array2.length;

            for(var5 = 0; var5 < var4; ++var5) {
                s = var3[var5];
                list.add(s);
            }
        }

        return (String[])list.toArray(new String[0]);
    }

    public static String[] mergeStrArray(String[] array1, String[] array2, String[] array3) {
        String[] tmpArray = mergeStrArray(array1, array2);
        return mergeStrArray(tmpArray, array3);
    }

    public static boolean isBoolean(Object value, boolean defaultVal) {
        if(value != null && value instanceof Integer) {
            if(((Integer)value).intValue() == 1) {
                return true;
            }

            if(((Integer)value).intValue() == 0) {
                return false;
            }
        } else {
            if(value != null && value instanceof Boolean) {
                return ((Boolean)value).booleanValue();
            }

            if(value != null) {
                return getBoolean(value.toString(), defaultVal);
            }
        }

        return defaultVal;
    }

    public static boolean getBoolean(String val, boolean defaultVal) {
        return "No".equalsIgnoreCase(val)?false:("N".equalsIgnoreCase(val)?false:("false".equalsIgnoreCase(val)?false:("0".equalsIgnoreCase(val)?false:("Yes".equalsIgnoreCase(val)?true:("Y".equalsIgnoreCase(val)?true:("true".equalsIgnoreCase(val)?true:("1".equalsIgnoreCase(val)?true:defaultVal)))))));
    }

    public static boolean containIgnoreCase(String s1, String s2) {
        if(s1 == null && s2 == null) {
            return true;
        } else {
            if(s1 != null && s2 != null) {
                s1 = s1.toLowerCase();
                s2 = s2.toLowerCase();
                if(s1 != null && s1.contains(s2)) {
                    return true;
                }
            }

            return false;
        }
    }

    public static long parseFileSize(String sizeDesc) {
        if(sizeDesc != null) {
            long maxSize = 1024000L;
            int size = 2;
            if(sizeDesc.toUpperCase().endsWith("M")) {
                try {
                    size = Integer.parseInt(sizeDesc.substring(0, sizeDesc.length() - 1));
                } catch (Exception var5) {
                    size = 2;
                }
            }

            return maxSize * (long)size;
        } else {
            return 0L;
        }
    }

    public static String parseFileName(String fullPath) {
        String fileName = null;
        if(fullPath == null) {
            return null;
        } else {
            int index = fullPath.lastIndexOf("/");
            if(index >= 0) {
                fileName = fullPath.substring(index + 1);
            }

            return fileName;
        }
    }

    public static String genIndexCode(int codeLen) {
        StringBuffer tmpBuff = new StringBuffer("a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
        tmpBuff.append(",A,B,C,D,E,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        tmpBuff.append(",1,2,3,4,5,6,7,8,9,0");
        Random r = new Random();
        String[] strArray = tmpBuff.toString().split(",");
        StringBuffer resultBuff = new StringBuffer();

        for(int i = 0; i < codeLen; ++i) {
            int k = r.nextInt();
            resultBuff.append(String.valueOf(strArray[Math.abs(k % 62)]));
        }

        return resultBuff.toString();
    }

    public static long genRandNum(int codeLen) {
        StringBuffer tmpBuff = new StringBuffer("1,2,3,4,5,6,7,8,9,0");
        Random r = new Random();
        String[] strArray = tmpBuff.toString().split(",");
        StringBuffer resultBuff = new StringBuffer();

        for(int i = 0; i < codeLen; ++i) {
            int k = r.nextInt();
            resultBuff.append(String.valueOf(strArray[Math.abs(k % 10)]));
        }

        return Long.parseLong(resultBuff.toString());
    }

    public static String genSerialNo(int seqNum, int length) {
        String seqStr;
        for(seqStr = String.valueOf(seqNum); seqStr.length() < length; seqStr = "0" + seqStr) {
            ;
        }

        return seqStr;
    }

    public static Map parseUrlParameters(String inputStr) {
        Map dataMap = new HashMap();
        if(inputStr == null) {
            return null;
        } else {
            int charCount = countChar(inputStr, '%');
            if(inputStr.contains("%26") || inputStr.contains("%23") || inputStr.contains("%40") || inputStr.contains("%3D") || charCount > 2) {
                try {
                    inputStr = URLDecoder.decode(inputStr, "UTF-8");
                } catch (UnsupportedEncodingException var6) {
                    ;
                }
            }

            String[] segments = split(inputStr, "&");

            for(int i = 0; i < segments.length; ++i) {
                int begin = segments[i].indexOf("=");
                if(begin > 0) {
                    dataMap.put(segments[i].substring(0, begin), segments[i].substring(begin + 1));
                }
            }

            return dataMap;
        }
    }

    public static Map parseJsonParameters(String inputStr) {
        if(inputStr == null) {
            return null;
        } else {
            try {
                Map dataMap = null;
                JSONObject jsonData = JSON.parseObject(inputStr);
                if(jsonData != null) {
                    dataMap = new HashMap();
                    Iterator var3 = jsonData.keySet().iterator();

                    while(var3.hasNext()) {
                        String key = (String)var3.next();
                        dataMap.put(key, jsonData.get(key));
                    }
                }

                return dataMap;
            } catch (Exception var5) {
                return null;
            }
        }
    }

    public static int countChar(String input, char ch) {
        int count = 0;
        if(input == null) {
            return 0;
        } else {
            for(int i = 0; i < input.length(); ++i) {
                if(input.charAt(i) == ch) {
                    ++count;
                }
            }

            return count;
        }
    }

    public static Map buildResultMessage(int resultCode, String resultMsg) {
        Map resultMap = new HashMap();
        resultMap.put("resultCode", Integer.valueOf(resultCode));
        resultMap.put("resultMsg", resultMsg);
        return resultMap;
    }

    public static Long getLongValue(Object value) {
        return value != null?(value instanceof BigDecimal?Long.valueOf(((BigDecimal)value).longValue()):(value instanceof Integer?Long.valueOf(((Integer)value).longValue()):(value instanceof Double?Long.valueOf(((Double)value).longValue()):(value instanceof String?Long.valueOf(Long.parseLong((String)value)):(Long)value)))):null;
    }

    public static Integer getIntValue(Object value) {
        return value != null?(value instanceof BigDecimal?Integer.valueOf(((BigDecimal)value).intValue()):(value instanceof Long?Integer.valueOf(((Long)value).intValue()):(value instanceof Double?Integer.valueOf(((Double)value).intValue()):(value instanceof String?(isBlank((String)value)?Integer.valueOf(-1):Integer.valueOf(Integer.parseInt((String)value))):(Integer)value)))):null;
    }
}
