package com.renrenliao.im.utils;

import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;

import com.renrenliao.im.config.Const;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串操作工具包
 *
 * @author linkai
 * @version 1.0
 * @created 2016-7-12
 */
public class StringUtils {
    private final static Pattern emailer = Pattern
            .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

    private final static String PATTERN_CHAR_NUM_EN = "^[a-zA-Z0-9\\u4e00-\\u9fa5]+$";//数字/汉字/英文的正则

    /**
     * 判断给定字符串是否空白串。 空白串是指由空
     * 格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input)) {
            return true;
        }

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是不是一个合法的电子邮件地址
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        if (email == null || email.trim().length() == 0) {
            return false;
        }
        return emailer.matcher(email).matches();
    }

    /**
     * 字符串转整数
     *
     * @param str
     * @param defValue
     * @return
     */
    public static int toInt(String str, int defValue) {
        try {
            if(TextUtils.isEmpty(str)){
                return  defValue;
            }
            return Integer.parseInt(str.trim());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defValue;
    }

    /**
     * 字符串转float
     * @param str
     * @param defValue
     * @return
     */
    public static float toFloat(String str, float defValue) {
        try {
            if(TextUtils.isEmpty(str)){
                return  defValue;
            }

            return Float.parseFloat(str.trim());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defValue;
    }


    /**
     *
     * 字符串转整数
     *
     * @param str
     * @return 转换异常返回 0
     */
    public static long toLong(String str, long defValue) {
        try {
            if(TextUtils.isEmpty(str)){
                return  defValue;
            }
            return Long.parseLong(str.trim());
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 字符串转布尔值
     *
     * @param b
     * @return 转换异常返回 false
     */
    public static boolean toBool(String b) {
        try {
            return Boolean.parseBoolean(b);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * HTML化字符串
     *
     * @param str
     * @return
     */
    public static Spanned fromHtml(String str) {
        if (!isEmpty(str)) {
            return Html.fromHtml(Html.fromHtml(str).toString());
        } else {
            return Html.fromHtml("");
        }
    }


    /**
     * 检查字符串是否只支持数字汉子和英文
     * @param str
     * @return
     */
    public static boolean checkNickName(String str){
        Pattern pattern = Pattern.compile(PATTERN_CHAR_NUM_EN);
        Matcher matcher = pattern.matcher(str);
        boolean match = matcher.matches();

        return match;
    }


    /**
     * 匹配正常的url(去除url的参数)
     * @param url
     * @return
     */
    public static String macthUrl(String url){
        if(TextUtils.isEmpty(url)){
            return url;
        }

        if(url.length() > 18){
            String substring = url.substring(0, url.length() - 18);

            return substring;
        }

        return url;
    }

    public static CharSequence editTextHtmlErrorTip(String text) {
        CharSequence html = Html.fromHtml("<font color='red'>" + text + "</font>");
        return html;
    }

    //根据头像地址分割得到的头像版本
    public static long getAvatarVersion(String avatarUrl){
        try {
            int start = avatarUrl.lastIndexOf("/")+1;
            int end = avatarUrl.lastIndexOf(".");
            String s = avatarUrl.substring(start,end);
            return Long.parseLong(s);
        }catch (Exception e){
            return 0;
        }
    }



}