package com.heyu.api.baidu.handle.certificate;

import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.baidu.BaiduBaseHandle;
import com.heyu.api.baidu.request.certificate.BIdcardEncRequest;
import com.heyu.api.baidu.response.certificate.BIdcardEncResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740303251854#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E5%8D%A1%E8%AF%81OCR&api=rest%2F2.0%2Focr%2Fv1%2Fidcard_enc&method=post
 * <p>
 * <p>
 * 身份证识别（金融加密版）
 */
@Component
@Slf4j
@CustomPath("idcardEnc")
public class BIdcardEncHandle extends BaiduBaseHandle<BIdcardEncRequest, BIdcardEncResp> {


    @Override
    public String getUri() {
        return "/rest/2.0/ocr/v1/idcard_enc";
    }

    @Override
    public String check(BIdcardEncRequest idcardEncRequest) {
        if (StringUtils.isBlank(idcardEncRequest.getImageAesBase64())) {
            return "imageAesBase64 不能为空";
        }

        if (StringUtils.isBlank(idcardEncRequest.getEncAesKey())) {
            return "encAesKey 白盒加密后的AES key不能为空";
        }

        if (!ApiConstants.frontOrback.contains(idcardEncRequest.getIdCardSide())) {
            return "idCardSide必须为" + ApiConstants.frontOrback + "，front：身份证含照片的一面，back：身份证带国徽的一面";
        }

        if (checkNotTrueFalse(idcardEncRequest.getDetectRisk())) {
            return "detectRisk必须为" + ApiConstants.trueOrFalse + ",是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能";
        }
        if (checkNotTrueFalse(idcardEncRequest.getDetectQuality())) {
            return "detectQuality 必须为" + ApiConstants.trueOrFalse + ",是否开启身份证质量类型(边框/四角不完整、头像或关键字段被遮挡/马赛克)检测功能";
        }

        if (checkNotTrueFalse(idcardEncRequest.getDetectPhoto())) {
            return "detectPhoto 必须为" + ApiConstants.trueOrFalse + ",是否检测头像内容";
        }

        if (checkNotTrueFalse(idcardEncRequest.getDetectCard())) {
            return "detectCard 必须为" + ApiConstants.trueOrFalse + ",是否检测身份证进行裁剪";
        }

        return null;
    }

    @Override
    public String getContent(BIdcardEncRequest idcardEncRequest) {
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotBlank(idcardEncRequest.getImageAesBase64())) {
            sb.append("&image=").append(idcardEncRequest.getImageAesBase64());
        }
        if (StringUtils.isNotBlank(idcardEncRequest.getEncAesKey())) {
            sb.append("&enc_aes_key=").append(idcardEncRequest.getEncAesKey());
        }
        if (StringUtils.isNotBlank(idcardEncRequest.getIdCardSide())) {
            sb.append("&id_card_side=").append(idcardEncRequest.getIdCardSide());
        }
        if (StringUtils.isNotBlank(idcardEncRequest.getDetectRisk())) {
            sb.append("&detect_risk=").append(idcardEncRequest.getDetectRisk());
        }
        if (StringUtils.isNotBlank(idcardEncRequest.getDetectQuality())) {
            sb.append("&detect_quality=").append(idcardEncRequest.getDetectQuality());
        }
        if (StringUtils.isNotBlank(idcardEncRequest.getDetectPhoto())) {
            sb.append("&detect_photo=").append(idcardEncRequest.getDetectPhoto());
        }
        if (StringUtils.isNotBlank(idcardEncRequest.getDetectCard())) {
            sb.append("&detect_card=").append(idcardEncRequest.getDetectCard());
        }
        return sb.toString();


    }

}