package com.phlx.anchorcollect.util;

import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/16
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class StringUtil {

    private static String s36bit_const = "0123456789abcdefghijklmnopqrstuvwxyz";


    /**
     * 判断字符串是否为null或全为空白字符
     *
     * @param s 待校验字符串
     * @return {@code true}: null或全空白字符<br> {@code false}: 不为null且不全空白字符
     */
    public static boolean isSpace(final String s) {
        if (s == null)
            return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String to32String(long i) {
        BigDecimal divide = new BigDecimal(32);
        BigDecimal decimal = new BigDecimal(i);
        String res = "";
        while (BigDecimal.ZERO.compareTo(decimal) != 0) {
            BigDecimal[] divRes = decimal.divideAndRemainder(divide);
            decimal = divRes[0];
            res = s36bit_const.charAt(divRes[1].intValue()) + res;
        }
        return res;
    }


    /**
     * 16进制的字符串表示转成字节数组 sl
     *
     * @param hexString 16进制格式的字符串
     * @return 转换后的字节数组
     **/
    public static byte[] stringToByte(String hexString) {
        hexString = hexString.toLowerCase();
        int length = hexString.length();
        if (length % 2 != 0) {
            length = length + 1;
        }
        final byte[] byteArray = new byte[length / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {// 因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte low;
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            if ((k + 1) == hexString.length()) {
                low = 0;
            } else {
                low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            }
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static List<String> strToList(String[] strings) {
        List<String> stringArrayList = new ArrayList<>();
        for (String str : strings) {
            stringArrayList.add(str);
        }
        return stringArrayList;
    }

    public static String[] listToStr(List<String> list) {
        if (list == null) return null;
        String[] strings = new String[list.size()];
        int i = 0;
        for (String str : list) {
            strings[i] = str;
            i++;
        }
        return strings;
    }

    public static float[] getFloats(int count) {
        float[] floats = new float[count];
        for(int i =0;i<count; i++){
            floats[i] = 1.0f;
        }
        return floats;
    }

    public static Class[] getClasses(int count) {
        Class[] textViews = new Class[count];
        for(int i =0;i<count; i++){
            textViews[i] = TextView.class;
        }
        return textViews;
    }
}
