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


import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.annotation.QPS;
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.BIdcardRequest;
import com.heyu.api.baidu.response.certificate.BIdcardResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740301134480#/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&method=post
 * 身份证识别
 */
@Component
@Slf4j
@CustomPath("idcard")
@QPS(2)
public class BIdcardHandle extends BaiduBaseHandle<BIdcardRequest, BIdcardResp> {


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

    @Override
    public String check(BIdcardRequest bIdcardRequest) {
        if (!ApiConstants.frontOrback.contains(bIdcardRequest.getIdCardSide())) {
            return "idCardSide 仅能传：" + ApiConstants.frontOrback + ",front：身份证含照片的一面,back：身份证带国徽的一面";
        }

        if (!ApiConstants.trueOrFalse.contains(bIdcardRequest.getDetectPs())) {
            return "detectPs 仅能传：" + ApiConstants.trueOrFalse + ",是否检测上传的身份证被PS";
        }

        if (!ApiConstants.trueOrFalse.contains(bIdcardRequest.getDetectRisk())) {
            return "detectRisk 仅能传：" + ApiConstants.trueOrFalse + ",是否开启身份证风险类型,身份证复印件、临时身份证、身份证翻拍、修改过的身份证）检测功能";
        }

        if (!ApiConstants.trueOrFalse.contains(bIdcardRequest.getDetectQuality())) {
            return "detectQuality 仅能传：" + ApiConstants.trueOrFalse + ",是否开启身份证质量类型（清晰模糊、边框/四角不完整、头像或关键字段被遮挡/马赛克）检测功能";
        }

        if (!ApiConstants.trueOrFalse.contains(bIdcardRequest.getDetectPhoto())) {
            return "detectPhoto 仅能传：" + ApiConstants.trueOrFalse + ",是否检测头像内容";
        }

        if (!ApiConstants.trueOrFalse.contains(bIdcardRequest.getDetectCard())) {
            return "detectQuality 仅能传：" + ApiConstants.trueOrFalse + ",是否检测身份证进行裁剪";
        }

        if (!ApiConstants.trueOrFalse.contains(bIdcardRequest.getDetectDirection())) {
            return "detectDirection 仅能传：" + ApiConstants.trueOrFalse + ",是否检测上传的身份证图片方向。";
        }

        return checkImageUri(bIdcardRequest);
    }

    @Override
    public String getContent(BIdcardRequest bIdcardRequest) {
        StringBuffer sb = getImageContent(bIdcardRequest);
        if (!StringUtils.isNotEmpty(bIdcardRequest.getIdCardSide())) {
            sb.append("&id_card_side=").append(bIdcardRequest.getIdCardSide());
        }
        if (!StringUtils.isNotEmpty(bIdcardRequest.getDetectPs())) {
            sb.append("&detect_ps=").append(bIdcardRequest.getDetectPs());
        }
        if (!StringUtils.isNotEmpty(bIdcardRequest.getDetectRisk())) {
            sb.append("&detect_risk=").append(bIdcardRequest.getDetectRisk());
        }
        if (!StringUtils.isNotEmpty(bIdcardRequest.getDetectQuality())) {
            sb.append("&detect_quality=").append(bIdcardRequest.getDetectQuality());
        }
        if (!StringUtils.isNotEmpty(bIdcardRequest.getDetectPhoto())) {
            sb.append("&detect_photo=").append(bIdcardRequest.getDetectPhoto());
        }
        if (!StringUtils.isNotEmpty(bIdcardRequest.getDetectCard())) {
            sb.append("&detect_card=").append(bIdcardRequest.getDetectCard());
        }
        if (!StringUtils.isNotEmpty(bIdcardRequest.getDetectDirection())) {
            sb.append("&detect_direction=").append(bIdcardRequest.getDetectDirection());
        }
        return sb.toString();
    }
}
