package com.crm.rely.backend.util;

import com.crm.rely.backend.core.constant.RegexpConstants;

import java.util.regex.Pattern;

import static java.util.regex.Pattern.matches;

public class ValidateUtil {
    public static boolean vLong(Long p) {
        if (p == null || p == 0) {
            return false;
        }
        return true;
    }

    /**
     * 判断integer是否不为空或者0 如果为空或者0 放回false
     *
     * @param p
     * @return
     */
    public static boolean vInteger(Integer p) {
        if (p == null || p == 0) {
            return false;
        }
        return true;
    }

    /**
     * 判断string是否为空 不为空返回true 为空返回false
     *
     * @param p
     * @return
     */
    public static boolean vStringNull(String p) {

        if (p != null && p.trim().length() != 0) {
            return true;
        }
        return false;
    }

    public static boolean vStringMin(String p, int length) {
        if (p.trim().length() < length) {
            return false;
        }
        return true;
    }

    public static boolean vStringMax(String p, int length) {
        if (p.trim().length() > length) {
            return false;
        }
        return true;
    }

    public static boolean vUserName(String p) {
        String regex = "[a-zA-Z]+[0-9a-zA-Z]*";
        boolean result = Pattern.matches(regex, p);
        return result;
    }

    /***
     *  身份证号码验证
     */
    public static boolean isIDCard(String idCard) {
        return matches(RegexpConstants.REGEXP_IDEN, idCard);
    }

    /**
     * 邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {

        return matches(RegexpConstants.REGEXP_IDEN, email);
    }

    /***
     * 手机号码
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {

        return matches(RegexpConstants.REGEXP_PHONE, phone);
    }

    /**
     * 电话号码
     *
     * @param telephone
     * @return
     */
    public static boolean isTelePhone(String telephone) {

        return matches(RegexpConstants.REGEXP_TELEPHONE, telephone);
    }

    /**
     * 校验银行卡卡号
     */
    public static boolean isBankCard(String bankCard) {

        return matches(RegexpConstants.REGEXP_BANK_CARD, bankCard);
    }

    /**
     * 邮政编码
     *
     * @param pCode
     * @return
     */
    public static boolean isPcode(String pCode) {

        return matches(RegexpConstants.REGEXP_PCODE, pCode);
    }

    /**
     * 时间格式
     *
     * @param date
     * @return
     */
    public static boolean isDate(String date) {

        return matches(RegexpConstants.REGEXP_DATE, date);
    }

    /**
     * 密码复杂度验证
     *
     * @param password
     * @return
     */
    public static boolean isPassword(String password) {

        return matches(RegexpConstants.REGEXP_PWD, password);
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeBankCard
     * @return
     */
    private static char getBankCardCheckCode(String nonCheckCodeBankCard) {
        if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
                || !nonCheckCodeBankCard.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeBankCard.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }
}