package com.usoftchina.uas.office.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Pattern;

/**
 * @author yingp
 * @date 2019/5/8
 */
public abstract class StringUtils extends org.springframework.util.StringUtils {

    private static Logger logger = LoggerFactory.getLogger(StringUtils.class);

    public static String nvl(Object object, String nvlValue) {
        return isEmpty(object) ? nvlValue : object.toString();
    }

    public static String nvl(Object object) {
        return nvl(object, null);
    }

    /**
     * 截取字符串
     *
     * @param str
     * @param beginIndex
     * @param endIndex
     * @return
     */
    public static String substr(String str, int beginIndex, int endIndex) {
        if (null == str) {
            return null;
        }
        int maxIndex = str.length();
        if (endIndex > maxIndex) {
            endIndex = maxIndex;
        }
        return str.substring(beginIndex, endIndex);
    }

    private final static Pattern BLANK_PATTERN = Pattern.compile("\\s*|\t|\r|\n");

    private final static Pattern MOBILE_PATTERN = Pattern.compile("^[1|8][3-9]\\d{9}$|^([6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]([8|6])\\d{5}$|^(886|0)[9]\\d{8}$");

    private final static Pattern EMAIL_PATTERN = Pattern.compile("^([\\w-])+(\\.\\w+)*@([\\w-])+((\\.\\w{2,3}){1,3})$");

    private final static Pattern TEL_PATTERN = Pattern.compile("^(\\+\\d{2}-)?(\\d{3,4}-)?\\d{7,8}$");

    /**
     * 清除换行空格等
     *
     * @param str
     * @return
     */
    public static String trimBlankChars(String str) {
        if (null != str) {
            return BLANK_PATTERN.matcher(str).replaceAll("").replace("\u00A0", "");
        }
        return null;
    }

    /**
     * 获取有效手机号
     *
     * @param mobile
     * @return
     */
    public static String getValidMobile(String mobile) {
        if (null != mobile) {
            mobile = trimBlankChars(mobile);
            if (mobile.contains("/")) {
                mobile = mobile.substring(0, mobile.indexOf("/"));
            }
            if (!MOBILE_PATTERN.matcher(mobile).matches()) {
                logger.warn("ignore invalid mobile " + mobile);
                mobile = null;
            } else {
                if (mobile.startsWith("852") || mobile.startsWith("853") || mobile.startsWith("886")) {
                    // 香港区号、澳门地区、台湾地区
                    mobile = "00" + mobile;
                }
            }
        }
        return mobile;
    }

    /**
     * 获取有效邮箱地址
     *
     * @param email
     * @return
     */
    public static String getValidEmail(String email) {
        if (null != email) {
            email = trimBlankChars(email);
            if (email.contains("/")) {
                email = email.substring(0, email.indexOf("/"));
            }
            if (!EMAIL_PATTERN.matcher(email).matches()) {
                logger.warn("ignore invalid email " + email);
                email = null;
            }
        }
        return email;
    }

    /**
     * 获取有效电话号码
     *
     * @param telephone
     * @return
     */
    public static String getValidTelephone(String telephone) {
        if (null != telephone) {
            telephone = trimBlankChars(telephone);
            if (telephone.contains("/")) {
                telephone = telephone.substring(0, telephone.indexOf("/"));
            }
            if (!TEL_PATTERN.matcher(telephone).matches()) {
                logger.warn("ignore invalid telephone " + telephone);
                telephone = null;
            }
        }

        return telephone;
    }

    public static boolean equals(String o1, String o2) {
        if (o1 == o2) {
            return true;
        } else if (o1 != null && o2 != null) {
            return o1.equals(o2);
        } else {
            return isEmpty(o1) && isEmpty(o2);
        }
    }
}
