package api.common.util;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class StringUtil {

    //* -------------------------------- D --------------------------------

    /**
     * 根据换行符分割
     *
     * @param string 字符串
     * @return
     */
    public static String[] splitByLineSeparator(String string) {
        return string.split(System.lineSeparator());
    }

    public static String doubleToString(double doubleNumber, int bit) {
        return String.format("%." + bit + "f", doubleNumber);
    }

    /**
     * 复制一个字符串
     * @param charArray 字符数组
     * @return 字符串
     */
    public static String charArrayToString(char[] charArray) {
        // 字符串存放在 java 字符串常量池，具有不变性
        return String.valueOf(charArray);
    }

    /**
     * 复制一个字符串
     * @param string 字符串
     * @return 字符串
     */
    public static String copy(String string) {
        // 字符串存放在 java 字符串常量池，具有不变性
        return string;
    }


    public static String[] splitByBlank(String string) {
        return string.split("\\s+");
    }

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




    public static String getRandomEightBitUUID() {
        String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };
        StringBuilder stringBuilder = new StringBuilder();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            stringBuilder.append(chars[x % 0x3E]);
        }
        return stringBuilder.toString().toLowerCase();
    }

    public static String getRandomCode() {
        return TimeUtil.getNow() + "";
    }

    public static boolean isEmpty(Object string) {
        return string == null || "".equals(string);
    }

    public static boolean isNotEmpty(Object string) {
        return !isEmpty(string);
    }


    /**
     * @param source 长字符串
     * @param target 子字符串
     * @return 包含个数
     */
    public static int countSubString(String source, String target) {
        if (isEmpty(source)|| isEmpty(target)) {
            return 0;
        }
        int count = 0;
        int index = 0;
        while ((index = source.indexOf(target, index)) != -1) {
            index = index + target.length();
            count++;
        }
        return count;
    }


    public static String replace(String string, String target, String replacement) {
        return string.replace(target, replacement);
    }

    /**
     * 从 begin 开始截取字符串
     *
     * @param string 原始字符串
     * @param begin  开始字符串
     * @return 截取后的字符串
     */
    public static String cut(String string, String begin) {
        int index = string.indexOf(begin);
        return string.substring(index);
    }


    public static String getUTF8BytesFromGBKString(String gbkStr) {
        int n = gbkStr.length();
        byte[] utfBytes = new byte[3 * n];
        int k = 0;
        for (int i = 0; i < n; i++) {
            int m = gbkStr.charAt(i);
            if (m < 128) {
                utfBytes[k++] = (byte) m;
                continue;
            }
            utfBytes[k++] = (byte) (0xe0 | (m >> 12));
            utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
            utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
        }
        if (k < utfBytes.length) {
            byte[] tmp = new byte[k];
            System.arraycopy(utfBytes, 0, tmp, 0, k);
            return new String(tmp, StandardCharsets.UTF_8);
        }
        return new String(utfBytes, StandardCharsets.UTF_8);
    }


}
