package com.brilliance.isc.common.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

public class StringUtils {

    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0 || " ".equals(str)) {
            return true;
        }
        return false;
    }

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

    public static String trim(String obj) {
        return obj == null ? "" : obj.trim();
    }

    public static String toString(BigDecimal decimal) {
        if (decimal == null) {
            return "";
        }
        return decimal.toPlainString();
    }

    public static String obj2Str(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static String replaceTxtZset(String txt, String ch) {
        if (StringUtils.isEmpty(txt)) {
            return txt;
        }
        if (txt.getBytes().length != txt.length()) {
            char[] chars = txt.toCharArray();
            for (char c : chars) {
                if (c > 127 || c < 0) {
                    txt = txt.replace(c, '\0');
                }
            }
        }
        String[] strs = {"!", "\"", "#", "%", "&", "*", ";", "<", "=", ">", "@", "_", "{"};
        for (String s : strs) {
            if (txt.indexOf(s) > -1) {
                txt = txt.replace(s, ch);
            }
        }
        return txt;
    }

    public static String convertBlock(String str, int rows, int maxLength) {
        if (isEmpty(str)) {
            return str;
        }
//        str=replaceTxtZset(str,"(");
        String[] strs = str.split("\r?\n|\r");
        List<String> list = new ArrayList<>();
        for (String s : strs) {
            //去除空行
            if (s.trim().isEmpty()) {
                continue;
            }
            if (s.length() <= maxLength) {
                list.add(s);
            } else {
                list.addAll(splitStrByLength(s, maxLength));
            }
        }
        StringBuffer sb = new StringBuffer();
        int size = list.size() > rows ? rows : list.size();
        for (int i = 0; i < size; i++) {
            if (i == (size - 1)) {
                sb.append(list.get(i));
            } else {
                sb.append(list.get(i) + "\n");
            }
        }
        return sb.toString();
    }


    public static List<String> splitStrByLength(String str, int maxLength) {
        return convertSingLineByCol(maxLength, str);
    }

    public static int pos(String str, String sub) {
        if (isEmpty(str) || isEmpty(sub)) {
            return 0;
        }
        int index = str.indexOf(sub);
        if (index <= 0) {
            return index + 1;
        }
        String tmp = str.substring(0, index);
        try {
            return tmp.getBytes("GBK").length + 1;
        } catch (UnsupportedEncodingException var5) {
            var5.printStackTrace();
            return 0;
        }
    }

    public static String convertBlock(String argBlock, int i) {
        switch (i) {
            case 11:
                argBlock = argBlock.replace("<", "&lt");
                argBlock = argBlock.replace(">", "&gt");
                argBlock = argBlock.replace("&", "&amp");
                break;
            case 0:
                break;
            case 21:
                break;
            default:
                break;
        }
        return argBlock;
    }

    public static String formatText(String format, Object... args) {
        if (format == null) {
            return "";
        } else if (args != null && args.length != 0) {
            for (int i = 0; i < args.length; ++i) {
                String key = "\\$" + (i + 1);
                String arg = args[i] == null ? "null" : args[i].toString();
                arg = Matcher.quoteReplacement(arg);
                format = format.replaceAll(key, arg);
            }

            return format;
        } else {
            return format;
        }
    }

    public static String convertHtmlToText(String html) {
        return html.replaceAll("<br/?>", "\n")  //br换行
                .replaceAll("</(p|div|h\\d|li)>", "\n") //块级元素替换为换行
                .replaceAll("</\\w+>", "")  //内联元素结尾标签去掉
                .replaceAll("<[^>]+>", "")    //起始会计字符去掉
                .replace("\n{2,}", "\n") //连续换行只保留一行
                .replace("&nbsp;", "").replace("&gt;", ">").replace("&lt;", "<").replace("&quot;", "\"").replace("&amp;", "&")//HTML的部分转义字符
                .trim();
    }

    public static List<String> convertGidHTML(String html, int col) {
        String text = convertHtmlToText(html);
        return convertTextByCol(col, text);
    }

    public static List<String> convertTextByCol(int col, String text) {
        String[] srcArr = text.split("[\\r\\n]]{1,2}");
        List<String> newArr = new ArrayList<>();
        for (int i = 0; i < srcArr.length; i++) {
            List<String> arr = convertSingLineByCol(col, text);
            newArr.addAll(arr);
        }
        return newArr;
    }

    public static List<String> convertSingLineByCol(int col, String text) {
        List<String> arr = new ArrayList<>();
        String left = text;
        int wordBeg = -1;
        while (left.length() > col) {
            for (int i = 0; i < col; i++) {
                String cStr = left.substring(i, i + 1);
                //以空白符分隔 与前端保持一致
                if (!cStr.matches("\\s")) {
                    if (wordBeg == -1) {
                        wordBeg = i;
                    }
                } else {
                    wordBeg = -1;
                }
            }
            //存在单次分开的情况，以当前单词起始位置拆分
            if (wordBeg > 0) {
                String line = left.substring(0, wordBeg).trim();
                arr.add(line);
                //截取余长
                left = left.substring(wordBeg);
                wordBeg = -1;
            } else {
                String line = left.substring(0, col).trim();
                arr.add(line);
                //截取余长
                if (col < left.length()) {
                    left = left.substring(col);
                }
            }
            //折行了，可以去掉头部空格
            left = left.trim();
        }
        //末尾仍有字符
        if (!left.trim().isEmpty()) {
            arr.add(left);
        }
        return arr;
    }
}
