package com.tzy.common.utils;

import com.tzy.common.enums.DiallingCodeEnums;
import com.tzy.common.enums.IDCardTypeEnums;
import com.tzy.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.Period;
import java.util.Objects;

import static com.tzy.common.enums.IDCardTypeEnums.*;

/**
 *  证书ID工具类
 */
@Slf4j
public class CertIdUtil {
    //居民身份证 正则规则
    private static String ID_CARD_PATTERN = "^[1-9]\\d{5}(19|20)\\d{2}(0[1-9]|1[0-2])([0-2][1-9]|3[0-1])(\\d{3})([0-9Xx])$";
    //香港身份证 正则规则
    private static String HK_ID_CARD_PATTERN = "^[A-Z]{1}[0-9]{6}\\([0-9A]{1}\\)$";
    //港澳通行证 正则规则
    private static String HK_PASS_PATTERN = "^[H|M]\\d{8}(?:\\w)?$";
    //台胞证 正则规则
    private static String TW_ID_CARD_PATTERN = "^[A-Z]{1,2}\\d{8}(?:\\d{2})?$";
    //86 手机号正则
    private static String MOBILE_PATTERN = "^1[3-9]\\d{9}$";
    /**
     * 校验身份证类型
     * @param idCard
     * @return
     */
    public static String checkIdCardType(String idCard){
        //
        if(StringUtils.isBlank(idCard)){
            throw new ServiceException("身份证不能为空");
        }
        if(idCard.matches(ID_CARD_PATTERN)){
            //18位身份证
            return ID_CARD.getType();
        } else if(idCard.matches(HK_ID_CARD_PATTERN)) {
            //香港身份证
            return HK_CARD.getType();
        } else if(idCard.matches(HK_PASS_PATTERN)) {
            //香港身份证
            return HK_AM_PASSPORT.getType();
        } else if(idCard.matches(TW_ID_CARD_PATTERN)) {
            //台胞证
            return TW_PASSPORT.getType();
        } else {
            //其他默认为未知
            return UNKNOWN.getType();
        }
    }

    public static boolean checkZhMobile(String phone){
        return phone.matches(MOBILE_PATTERN);
    }

    /**
     * 根据身份证号判断是否未成年
     * @param certType 证件类型
     * @param diallingCode 手机号区号
     * @param certNo 身份证号
     * @return
     */
    public static boolean isMinor(String certType,String diallingCode, String certNo) {
        //如果非居民身份证 验证手机区号
        if(!Objects.equals(ID_CARD.getType(), certType)
                && (StringUtils.isBlank(diallingCode) ||
                !DiallingCodeEnums.checkEquals(certType, DiallingCodeEnums.CHINA))
//                !Objects.equals(DiallingCodeEnums.CHINA.getCode(), diallingCode))
        ){
            //对证件做规则校验
            String checkType = checkIdCardType(certNo);
            if(!Objects.equals(certType, checkType)) {
                throw new ServiceException(500, "初步校验证件号【"+ IDCardTypeEnums.getDesc(checkType) +"】与校验证件类型【"+IDCardTypeEnums.getDesc(certType)+"】不符");
            }
            return true;
        }
        if (certNo.length() != 18) {
            throw new ServiceException(500, "您好,暂时仅支持二代身份证校验!");
        }

        int year = Integer.valueOf(certNo.substring(6).substring(0, 4));// 得到年份
        int month = Integer.valueOf(certNo.substring(10).substring(0, 2));// 得到月份
        int day = Integer.valueOf(certNo.substring(12).substring(0, 2));//得到日

        LocalDate today = LocalDate.now();
        //System.out.println("Today : " + today);
        LocalDate birthDate = LocalDate.of(year, month, day);
        // System.out.println("BirthDate : " + birthDate);
        Period p = Period.between(birthDate, today);
        //System.out.printf("年龄 : %d 年 %d 月 %d 日", p.getYears(), p.getMonths(), p.getDays());

        return p.getYears() >= 18 && p.getYears() < 70;
    }

}
