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


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.traffic.BUnifiedValidPeriodRequest;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

/**
 * <p>
 * <p>
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740325062124#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E4%BA%A4%E9%80%9A%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Fdriving_license&method=post
 * 驾驶证识别
 */

@Component
@Slf4j
@CustomPath("unifiedValidPeriod")
public class BUnifiedValidPeriodHandle extends BaiduBaseHandle<BUnifiedValidPeriodRequest, JSONObject> {


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

    @Override
    public String check(BUnifiedValidPeriodRequest bVehicleLicenseRequest) {

        if (checkNotTrueFalse(bVehicleLicenseRequest.getDetectDirection())) {
            return "detectDirection 必须传 " + ApiConstants.trueOrFalse + ",false：不进行图像方向自动矫正, true: 开启图像方向自动矫正功能，可对旋转 90/180/270 度的图片进行自动矫正并识别";
        }

        if (checkNotFrontBack(bVehicleLicenseRequest.getDrivingLicenseSide())) {
            return "drivingLicenseSide 必须传 " + ApiConstants.frontOrback + ",front：识别驾驶证正页、电子驾驶证正页,back：识别驾驶证副页";
        }

        if (checkNotTrueFalse(bVehicleLicenseRequest.getUnifiedValidPeriod())) {
            return "unifiedValidPeriod 必须为 " + ApiConstants.trueOrFalse + ", false 不进行归一化处理, true：归一化格式输出，将驾驶证正页的「有效起始日期」+「有效期限」及「有效期限」+「至」，归一化为「有效起始日期」+「失效日期」格式输出";
        }

        if (checkNotTrueFalse(bVehicleLicenseRequest.getQualityWarn())) {
            return "qualityWarn 必须为" + ApiConstants.trueOrFalse + ",false：不输出质量告警信息, true : 输出行驶证遮挡、不完整质量告警信息";
        }
        if (checkNotFrontBack(bVehicleLicenseRequest.getRiskWarn())) {
            return "riskWarn 必须为" + ApiConstants.trueOrFalse + ",false：不输出风险告警信息 , true：开启，输出行驶证复印、翻拍、PS等告警信息";
        }
        return checkImageUri(bVehicleLicenseRequest);
    }

    @Override
    public String getContent(BUnifiedValidPeriodRequest bUnifiedValidPeriodRequest) {
        StringBuffer sb = getImageContent(bUnifiedValidPeriodRequest);
        if (StringUtils.isNotBlank(bUnifiedValidPeriodRequest.getDetectDirection())) {
            sb.append("&detect_direction=").append(bUnifiedValidPeriodRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(bUnifiedValidPeriodRequest.getDrivingLicenseSide())) {
            sb.append("&driving_license_side=").append(bUnifiedValidPeriodRequest.getDrivingLicenseSide());
        }
        if (StringUtils.isNotBlank(bUnifiedValidPeriodRequest.getUnifiedValidPeriod())) {
            sb.append("&unified_valid_period=").append(bUnifiedValidPeriodRequest.getUnifiedValidPeriod());
        }
        if (StringUtils.isNotBlank(bUnifiedValidPeriodRequest.getQualityWarn())) {
            sb.append("&quality_warn=").append(bUnifiedValidPeriodRequest.getQualityWarn());
        }
        if (StringUtils.isNotBlank(bUnifiedValidPeriodRequest.getRiskWarn())) {
            sb.append("&risk_warn=").append(bUnifiedValidPeriodRequest.getRiskWarn());
        }

        return sb.toString();
    }
}
