package com.phlx.wool.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.github.promeg.pinyinhelper.Pinyin;

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

    public static String getSimplePinyin(String s){

        String pinyin = Pinyin.toPinyin(s, ",");
        String[] arrayStr = pinyin.split(",");
        String result="";
        for (String item : arrayStr){
            if (!TextUtils.isEmpty(item)){
                result = result+item.substring(0,1);
            }
        }
        Log.e("lei", "getSimplePinyin: "+result );
        return result;
    }
    public static String getPinyin(String s){

        return Pinyin.toPinyin(s,"");
    }

    public static String byteToHexString(byte[] b, int length) {
        String ret = "";
        for (int i = 0; i < length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

    /**
     * 判断字符串是否为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 packageName(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    /**
     * 将一个字符串中的所有字母均转换为大写
     *
     * @param devicesAddress
     * @return
     */
    public static String formatTheString(String devicesAddress) {
        StringBuffer newDevicesAddress = new StringBuffer("");
        for (int i = 0; i < devicesAddress.length(); i++) {
            char chars = devicesAddress.charAt(i);
            String childString = "" + chars;
            childString = childString.toUpperCase();
            newDevicesAddress.append(childString);
        }
        return newDevicesAddress.toString().substring(0, 17);
        // return newDevicesAddress.toString();
    }

}
