package com.fzm.mall.server.front.constant;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 检验工具
 *
 * @author tdz
 * @date 2021/02/22 11:26
 */
public class CommonValid {
    private static final String FORMAT_TIME = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\\s+(?:[01][0-9]|2[0-3])(?::[0-5][0-9]){2}$";
    private static final String FORMAT_PHONE = "^(?:13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
    private static final String FORMAT_URL = "^(?:https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]$";

    /**
     * 校验用户编号
     */
    public static boolean uid(String uid) {
        return StringUtils.isNotBlank(uid);
    }

    /**
     * 校验手机号
     */
    public static boolean phone(String phone) {
        return StringUtils.isNotBlank(phone) && Pattern.matches(FORMAT_PHONE, phone);
    }

    /**
     * 校验验证码
     */
    public static boolean captcha(String captcha) {
        return StringUtils.isNotBlank(captcha) && Pattern.matches("^\\d{4,8}$", captcha);
    }

    /**
     * 校验交易密码
     */
    public static boolean password(String password) {
        return StringUtils.isNotBlank(password) && Pattern.matches("^\\d{6}$", password);
    }

    /**
     * 校验哈希
     */
    public static boolean hash32(String hash) {
        return StringUtils.isNotBlank(hash) && Pattern.matches("^[0-9a-f]{32}$", hash);
    }

    /**
     * 校验哈希
     */
    public static boolean hash64(String hash) {
        return StringUtils.isNotBlank(hash) && Pattern.matches("^[0-9a-f]{64}$", hash);
    }

    /**
     * 校验时间
     */
    public static boolean formatTime(String formatTime) {
        return StringUtils.isNotBlank(formatTime) && Pattern.matches(FORMAT_TIME, formatTime);
    }

    /**
     * 校验时间戳
     */
    public static boolean timestamp(String timestamp) {
        return StringUtils.isNotBlank(timestamp) && Pattern.matches("^[1-9]\\d{9,11}$", timestamp);
    }

    /**
     * 校验URL
     */
    public static boolean url(String url) {
        return StringUtils.isNotBlank(url) && Pattern.matches(FORMAT_URL, url);
    }

    /**
     * 校验文件是否符合条件
     *
     * @param fileName  文件名
     * @param typeAllow 允许的类型
     * @return
     */
    public static boolean fileName(String fileName, String typeAllow) {
        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        fileName = fileName.toLowerCase();
        int index = fileName.indexOf("?");
        fileName = index > 0 ? fileName.substring(0, index) : fileName;
        String[] split = StringUtils.split(fileName, "\\.");
        if (split == null || split.length < 2) {
            return false;
        }
        String fileType = split[split.length - 1];
        List<String> typeList = Arrays.asList(typeAllow.replace(" ", "").split(","));
        return typeList.contains(fileType);
    }

    /**
     * 校验文件是否符合条件
     *
     * @param file      文件
     * @param typeAllow 允许的类型
     * @param maxSize   最大大小
     * @return
     */
    public static boolean file(MultipartFile file, String typeAllow, long maxSize) {
        if (file.isEmpty()) {
            return false;
        }
        String fileName = file.getOriginalFilename();
        if (!fileName(fileName, typeAllow)) {
            return false;
        }
        long size = file.getSize();
        return size != 0 && size <= maxSize;
    }

}