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


import com.alibaba.fastjson.JSON;
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.common.BWebImageLocRequest;
import com.heyu.api.baidu.response.common.BWebImageLocResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740294887952#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E9%80%9A%E7%94%A8%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Fwebimage_loc&method=post
 * <p>
 * <p>
 * 网络图片文字识别（含位置版）
 */
@Component
@Slf4j
@CustomPath("webImageLoc")
public class BWebImageLocHandle extends BaiduBaseHandle<BWebImageLocRequest, BWebImageLocResp> {

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

    @Override
    public String check(BWebImageLocRequest bWebImageLocRequest) {
        if (!ApiConstants.trueOrFalse.contains(bWebImageLocRequest.getDetectDirection())) {
            return "detectDirection 仅能传 " + ApiConstants.trueOrFalse + ",是否检测图像朝向。";
        }

        if (!ApiConstants.trueOrFalse.contains(bWebImageLocRequest.getProbability())) {
            return "probability 仅能传 " + ApiConstants.trueOrFalse + ",是否返回每行识别结果的置信度。";
        }


        if (!ApiConstants.trueOrFalse.contains(bWebImageLocRequest.getPolyLocation())) {
            return "polyLocation 仅能传 " + ApiConstants.trueOrFalse + ",是否返回文字所在区域的外接四边形的4个点坐标信息。";
        }


        if (!ApiConstants.bigOrSmall.containsKey(bWebImageLocRequest.getRecognizeGranularity())) {
            return "recognizeGranularity 仅能传 " + JSON.toJSONString(ApiConstants.bigOrSmall) + ",是否返回每行识别结果的置信度。";
        }
        return checkOfd(bWebImageLocRequest);
    }

    @Override
    public String getContent(BWebImageLocRequest bWebImageLocRequest) {
        StringBuffer sb = getOfdContent(bWebImageLocRequest);
        if (StringUtils.isNotBlank(bWebImageLocRequest.getDetectDirection())) {
            sb.append("&detect_direction=").append(bWebImageLocRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(bWebImageLocRequest.getProbability())) {
            sb.append("&probability=").append(bWebImageLocRequest.getProbability());
        }
        if (StringUtils.isNotBlank(bWebImageLocRequest.getPolyLocation())) {
            sb.append("&poly_location=").append(bWebImageLocRequest.getPolyLocation());
        }
        if (StringUtils.isNotBlank(bWebImageLocRequest.getRecognizeGranularity())) {
            sb.append("&recognize_granularity=").append(bWebImageLocRequest.getRecognizeGranularity());
        }
        return sb.toString();
    }
}

