package com.cm.common.utils;

import java.util.regex.Pattern;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RegexUtil
 * @Description: 正则校验工具类
 * @Author: WangGeng
 * @Date: 2019/12/4 18:13
 * @Version: 1.0
 **/
public class RegexUtil {

    /**
     * 特殊字符
     */
    public static final String SPECIAL_CHARACTERS = "#?!@$%^&*-_";
    /**
     * 密码最小长度
     */
    public static final int PASSWORD_LENGTH_MIN = 6;
    /**
     * 密码最大长度
     */
    public static final int PASSWORD_LENGTH_MAX = 16;
    /**
     * 手机
     */
    private static final Pattern PATTERN_PHONE = Pattern.compile("^1\\d{10}$");
    /**
     * 邮箱
     */
    private static final Pattern PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
    /**
     * 邮箱
     */
    private static final Pattern PATTERN_URL = Pattern.compile("(^#)|(^http(s*):\\/\\/[^\\s]+)");
    /**
     * 日期格式
     */
    private static final Pattern PATTERN_DATE = Pattern.compile("^(\\d{4})[-\\/](\\d{1}|0\\d{1}|1[0-2])([-\\/](\\d{1}|0\\d{1}|[1-2][0-9]|3[0-1]))*$");
    /**
     * 时间戳格式
     */
    private static final Pattern PATTERN_DATETIME = Pattern.compile("^(\\d{4})[-\\/](\\d{1}|0\\d{1}|1[0-2])([-\\/](\\d{1}|0\\d{1}|[1-2][0-9]|3[0-1]))*(\\s+)([0-1][0-9]|(2[0-3])):([0-5][0-9])(:([0-5][0-9]))*$");
    /**
     * 时间戳
     */
    private static final Pattern PATTERN_YYYY_MM_DD = Pattern.compile("^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[1-2][0-9]|3[0-1])$");
    /**
     * 时间戳
     */
    private static final Pattern PATTERN_YYYY_MM_DD_HH_MM_SS = Pattern.compile("^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[1-2][0-9]|3[0-1])([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])$");
    /**
     * 时间戳（毫秒）
     */
    private static final Pattern PATTERN_YYYY_MM_DD_HH_MM_SS_ZZZ = Pattern.compile("^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[1-2][0-9]|3[0-1])([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])(\\d{3})$");
    /**
     * 时间戳
     */
    private static final Pattern PATTERN_HH_MM_SS = Pattern.compile("^([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])$");
    /**
     * 时间戳（毫秒）
     */
    private static final Pattern PATTERN_HH_MM_SS_ZZZ = Pattern.compile("^([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])(\\d{3})$");
    /**
     * 身份证
     */
    private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)");
    /**
     * 用户名
     */
    private static final Pattern PATTERN_USERNAME = Pattern.compile("^[a-zA-Z0-9_\\s]+$");
    /**
     * 字母
     */
    private static final Pattern PATTERN_LETTER = Pattern.compile("[a-zA-Z]+");
    /**
     * 中文
     */
    private static final Pattern PATTERN_CHINESE = Pattern.compile("[\\u4e00-\\u9fa5]+");
    /**
     * 数字
     */
    private static final Pattern PATTERN_NUMBER = Pattern.compile("\\d+");
    /**
     * 弱密码
     */
    private static final Pattern PASSWORD_WEEK = Pattern.compile(String.format("(?=.*[A-Za-z0-9%s]).{%d,%d}", SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));
    /**
     * 中密码
     */
    private static final Pattern PASSWORD_MIDDLE = Pattern.compile(String.format("((?=.*[A-Za-z])(?=.*[0-9]))|((?=.*[A-Za-z])(?=.*[%s]))|((?=.*[0-9])(?=.*[%s])).{%d,%d}", SPECIAL_CHARACTERS, SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));
    /**
     * 强密码
     */
    private static final Pattern PASSWORD_STRONG = Pattern.compile(String.format("(?=.*[A-Za-z])(?=.*[0-9])(?=.*[%s]).{%d,%d}", SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));

    /**
     * 判断弱密码强度
     *
     * @param input
     * @return
     */
    public static boolean isPasswordWeek(String input) {
        return PASSWORD_WEEK.matcher(input).matches();
    }

    /**
     * 判断中密码强度
     *
     * @param input
     * @return
     */
    public static boolean isPasswordMiddle(String input) {
        return PASSWORD_MIDDLE.matcher(input).matches();
    }

    /**
     * 判断强密码强度
     *
     * @param input
     * @return
     */
    public static boolean isPasswordStrong(String input) {
        return PASSWORD_STRONG.matcher(input).matches();
    }

    /**
     * 判断电话
     *
     * @param input
     * @return
     */
    public static boolean isPhone(String input) {
        return PATTERN_PHONE.matcher(input).matches();
    }

    /**
     * 判断邮箱
     *
     * @param input
     * @return
     */
    public static boolean isEmail(String input) {
        return PATTERN_EMAIL.matcher(input).matches();
    }

    /**
     * 判断URL
     *
     * @param input
     * @return
     */
    public static boolean isUrl(String input) {
        return PATTERN_URL.matcher(input).matches();
    }

    /**
     * 判断日期
     *
     * @param input
     * @return
     */
    public static boolean isDate(String input) {
        return PATTERN_DATE.matcher(input).matches();
    }

    /**
     * 判断时间戳
     *
     * @param input
     * @return
     */
    public static boolean isDatetime(String input) {
        return PATTERN_DATETIME.matcher(input).matches();
    }
    /**
     * 判断时间时间戳
     *
     * @param input
     * @return
     */
    public static boolean isYyyyMmDd(String input) {
        return PATTERN_YYYY_MM_DD.matcher(input).matches();
    }

    /**
     * 判断时间时间戳
     *
     * @param input
     * @return
     */
    public static boolean isYyyyMmDdHhMmSs(String input) {
        return PATTERN_YYYY_MM_DD_HH_MM_SS.matcher(input).matches();
    }

    /**
     * 判断时间时间戳
     *
     * @param input
     * @return
     */
    public static boolean isYyyyMmDdHhMmSsZzz(String input) {
        return PATTERN_YYYY_MM_DD_HH_MM_SS_ZZZ.matcher(input).matches();
    }

    /**
     * 判断时间时间戳
     *
     * @param input
     * @return
     */
    public static boolean isHhMmSs(String input) {
        return PATTERN_HH_MM_SS.matcher(input).matches();
    }

    /**
     * 判断时间时间戳
     *
     * @param input
     * @return
     */
    public static boolean isHhMmSsZzz(String input) {
        return PATTERN_HH_MM_SS_ZZZ.matcher(input).matches();
    }

    /**
     * 判断身份证
     *
     * @param input
     * @return
     */
    public static boolean isIdentity(String input) {
        return PATTERN_IDENTITY.matcher(input).matches();
    }

    /**
     * 判断用户名
     *
     * @param input
     * @return
     */
    public static boolean isUsername(String input) {
        return PATTERN_USERNAME.matcher(input).matches();
    }

    /**
     * 判断是字母
     *
     * @param input
     * @return
     */
    public static boolean isLetter(String input) {
        return PATTERN_LETTER.matcher(input).matches();
    }

    /**
     * 判断是中文
     *
     * @param input
     * @return
     */
    public static boolean isChinese(String input) {
        return PATTERN_CHINESE.matcher(input).matches();
    }

    /**
     * 判断是数字
     *
     * @param input
     * @return
     */
    public static boolean isNumber(String input) {
        return PATTERN_NUMBER.matcher(input).matches();
    }

    /**
     * 自定义判断
     *
     * @param customPattern
     * @param input
     * @return
     */
    public static boolean isMatch(Pattern customPattern, String input) {
        return customPattern.matcher(input).matches();
    }

}
