package com.infoepoch.pms.agent.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Iterator;
import java.util.Objects;

public class StringTool {

    public static final String EMPTY = "";

    private static final int STRING_BUILDER_SIZE = 256;

    public static String replaceAll(String str, String regex, String replacement) {
        if(str == null)
            return null;
        return str.replaceAll(regex, replacement);
    }

    /**
     * 查询单个字符数量
     *
     * @param str
     * @param regex
     * @return
     */
    public static int searchCharCount(String str, char regex) {
        char[] chars = str == null ? new char[0] : str.toCharArray();
        int count = 0;
        for (char c : chars) {
            if (c == regex) {
                count++;
            }
        }
        return count;
    }

    /**
     * 获取字符串
     *
     * @param integer
     * @return
     */
    public static String getString(Integer integer) {
        if (integer == null)
            return null;
        else
            return String.valueOf(integer);
    }

    /**
     * 获取字符串
     *
     * @param o
     * @return
     */
    public static String getString(Object o) {
        if (o == null)
            return null;
        else
            return String.valueOf(o);
    }

    /**
     * @param o
     * @param defaultString
     * @return
     */
    public static String getString(Object o, String defaultString) {
        try {
            return String.valueOf(o);
        } catch (Exception e) {
            return defaultString;
        }
    }

    /**
     * @param str
     * @param defaultStr
     * @return
     */
    public static String getString(String str, String defaultStr) {
        if (str == null || str.equals("") || str.equals("null")) {
            return defaultStr;
        } else {
            return str;
        }
    }

    /**
     * 填充字符串左侧0
     *
     * @param num
     * @param targetLength
     * @return
     */
    public static String fillLeftZero(Integer num, int targetLength) {
        StringBuilder numStr = new StringBuilder(String.valueOf(num));
        int length = numStr.length();
        if (length >= targetLength) {
            return numStr.toString();
        } else {
            for (int i = 0; i < targetLength - length; i++) {
                numStr.insert(0, "0");
            }
            return numStr.toString();
        }
    }

    /**
     * 移除字符串中的html标签
     *
     * @param str
     * @return
     */
    public static String removeHtmlTag(String str) {
        if(str == null)
            return "";
        String htmlRegex = "<[^>]+>";
        return str.replaceAll(htmlRegex, "")
                .replaceAll("&gt;", "“")// 大于
                .replaceAll("&lt;", "“")// 小于
                .replaceAll("&amp;", "“")// AND符号
                .replaceAll("&quot;", "“")// 双引号
                .replaceAll("&ldquo;", "“")// 左双引号
                .replaceAll("&rdquo;", "”")// 右双引号
                .replaceAll("&nbsp;", " ")// 空格
                ;
    }

    /**
     * 编码自增
     *
     * @param str
     * @param length
     * @return
     */
    public static String increase(String str, int length) {
        int old = Integer.parseInt(str);
        return fillLeftZero(old + 1, length);
    }

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

    public static String trimToNull(String str) {
        String ts = trim(str);
        return StringUtils.isEmpty(ts) ? null : ts;
    }

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

    public static String join(final Object[] array, final String separator) {
        if (array == null) {
            return null;
        }
        return join(array, separator, 0, array.length);
    }

    public static String join(final Object[] array, String separator, final int startIndex, final int endIndex) {
        if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = EMPTY;
        }
        final int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return EMPTY;
        }

        final StringBuilder buf = newStringBuilder(noOfItems);

        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    private static StringBuilder newStringBuilder(final int noOfItems) {
        return new StringBuilder(noOfItems * 16);
    }

    public static String join(final Iterable<?> iterable, final String separator) {
        if (iterable == null) {
            return null;
        }
        return join(iterable.iterator(), separator);
    }

    public static String join(final Iterator<?> iterator, final String separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return EMPTY;
        }
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return Objects.toString(first, "");
        }

        // two or more elements
        final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }
    /**
     * @Description: 查询字符串中包含指定字符的个数
     * @Param: [str, strRes]
     * @Author: zhangyd
     */
    public static int searchString(String str, String strRes) {
        if (null == str || null == strRes || str.isEmpty() || strRes.isEmpty())
            return 0;
        int n = 0;
        int index = 0;
        index = str.indexOf(strRes);
        while (index != -1) {
            n++;
            index = str.indexOf(strRes, index + 1);
        }
        return n;
    }

    public static String getString(String value){
        if (value == null)
            return "";
        return value;
    }
}
