package com.bootdo.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class ValidUtil {

    private static final Logger log = LoggerFactory.getLogger(ValidUtil.class);
    private static final String SM = "SM";
    private static final String FSM = "FSM";
    private static final int VILLAGE_CODE_LENGTH = 15;

    /**
     * 校验身份证号
     *
     * @param idCard
     * @return
     */
    public String checkIdCard(String idCard) {
        if (StringUtils.isBlank(idCard)) {
            return "";
        }
        if (idCard.length() != 18) {
            return "";
        }
        return idCard.trim();
    }

    /**
     * 检测是否带眼镜
     *
     * @param glasses
     * @return
     */
    public String checkGlasses(String glasses) {
        if (StringUtils.isBlank(glasses)) {
            return "0";
        }
        int glassInt = 0;
        try {
            glassInt = Integer.parseInt(glasses);
        } catch (Exception e) {
            return "0";
        }
        if (glassInt == 0 || glassInt == 1 || glassInt == 2) {
            glasses = String.valueOf(glassInt);
        } else {
            return "0";
        }
        return glasses;
    }

    /**
     * 是否戴帽子
     *
     * @param hat
     * @return
     */
    public String checkHat(String hat) {
        if (StringUtils.isBlank(hat)) {
            return "0";
        }
        int hatInt = 0;
        try {
            hatInt = Integer.parseInt(hat);
        } catch (Exception e) {
            return "0";
        }
        if (hatInt == 0 || hatInt == 1 || hatInt == 2) {
            hat = String.valueOf(hatInt);
        } else {
            return "0";
        }
        return hat;
    }

    /**
     * 是否戴口罩
     *
     * @param mask
     * @return
     */
    public String checkMask(String mask) {
        if (StringUtils.isBlank(mask)) {
            return "0";
        }
        int maskInt = 0;
        try {
            maskInt = Integer.parseInt(mask);
        } catch (Exception e) {
            return "0";
        }
        if (maskInt == 0 || maskInt == 1 || maskInt == 2) {
            mask = String.valueOf(maskInt);
        } else {
            return "0";
        }
        return mask;
    }

    /**
     * 档案编号
     *
     * @param dossierCode
     * @return
     */
    public String checkDossierCode(String dossierCode) {
        if (StringUtils.isBlank(dossierCode) || dossierCode.equals("0")) {
            return "";
        }
        int dossierCodeInt = 0;
        try {
            dossierCodeInt = Integer.parseInt(dossierCode);
        } catch (Exception e) {
            return "";
        }
        return String.valueOf(dossierCodeInt);
    }

    /**
     * 相似度
     *
     * @param similarity
     * @return
     */
    public String checkSimilarity(String similarity) {
        if (StringUtils.isBlank(similarity) || similarity.equals("0")) {
            return "";
        }
        int similarityInt = 0;
        try {
            similarityInt = Integer.parseInt(similarity);
        } catch (Exception e) {
            return "";
        }
        return String.valueOf(similarityInt);
    }

    public String checkIvsDeviceCode(String isvCode) {
        if (StringUtils.isBlank(isvCode) || isvCode.equals("0")) {
            return "";
        }
        return String.valueOf(isvCode.trim());
    }

    /**
     * 入库档案编号
     * ivsCode ivs服务器SN
     * dossierCode 样本库和陌生人库id
     */
    public String dealDossierCode(String ivsCode, String idCard, String dossierCode) {
        if (StringUtils.isBlank(ivsCode)
                || StringUtils.isBlank(dossierCode)
                || "0".equals(ivsCode)) {
            return "";
        }
        if (StringUtils.isBlank(idCard)) {
            return FSM + "-" + ivsCode + "-" + dossierCode;
        } else {
            return SM + "-" + ivsCode + "-" + dossierCode;
        }
    }


    /**
     * 人脸比对结果
     * 1比对成功 2比对失败 3未比对
     *
     * @param idCard
     * @return
     */
    public int dealCompare(String idCard) {
        if (StringUtils.isBlank(idCard)) {
            return 3;
        }
        if (idCard.length() == 18) {
            return 1;
        }
        return 2;
    }

    public boolean checkTime(String time) {
        try {
            if (StringUtils.isBlank(time)
                    || time.length() != 10) {
                log.error("checkTime time:" + time);
                return false;
            }
            String s = DateTool.yearMonthDay(time);
            if (StringUtils.isBlank(s) || s.contains("-00")) {
                return false;
            }
        } catch (Exception e) {
            log.error("checkTime Exception:" + e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 车牌颜色
     *
     * @param color
     * @return
     */
    public int dealPlateColor(String color) {
        if (StringUtils.isBlank(color)) {
            return 7;
        }
        if ("黄".equals(color) || "黄色".equals(color)) {
            return 1;
        }
        if ("蓝".equals(color) || "蓝色".equals(color)) {
            return 2;
        }
        if ("白".equals(color) || "白色".equals(color)) {
            return 3;
        }
        if ("黑".equals(color) || "黑色".equals(color)) {
            return 4;
        }
        if ("绿".equals(color) || "绿色".equals(color)) {
            return 5;
        }
        if ("黄绿".equals(color) || "黄绿色".equals(color)) {
            return 6;
        }
        return 7;
    }

    /**
     * 车身颜色
     *
     * @param color
     * @return
     */
    public int dealVehicleColor(String color) {
        if (StringUtils.isBlank(color)) {
            return 99;
        }
        if ("黑".equals(color) || "黑色".equals(color)) {
            return 1;
        }
        if ("白".equals(color) || "白色".equals(color)) {
            return 2;
        }
        if ("灰".equals(color) || "灰色".equals(color)) {
            return 3;
        }
        if ("红".equals(color) || "红色".equals(color)) {
            return 4;
        }
        if ("蓝".equals(color) || "蓝色".equals(color)) {
            return 5;
        }
        if ("黄".equals(color) || "黄色".equals(color)) {
            return 6;
        }
        if ("橙".equals(color) || "橙色".equals(color)) {
            return 7;
        }
        if ("棕".equals(color) || "棕色".equals(color)) {
            return 8;
        }
        if ("绿".equals(color) || "绿色".equals(color)) {
            return 9;
        }
        if ("紫".equals(color) || "紫色".equals(color)) {
            return 10;
        }
        if ("青".equals(color) || "青色".equals(color)) {
            return 11;
        }
        if ("粉".equals(color) || "粉色".equals(color)) {
            return 12;
        }
        if ("透明".equals(color) || "透明色".equals(color)) {
            return 13;
        }
        return 99;
    }

    public String checkVillageCode(String villageCode) {
        if (StringUtils.isBlank(villageCode)) {
            return "";
        }
        villageCode = villageCode.trim();
        if (villageCode.length() != VILLAGE_CODE_LENGTH) {
            return "";
        }
        return villageCode;
    }


}
