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.AHandwritingRequest;
import com.heyu.api.baidu.response.common.AHandwritingResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740295688238#/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%2Fhandwriting&method=post
 * <p>
 * 手写文字识别
 */
@Component
@Slf4j
@CustomPath("accurateBasic")
public class BHandwritingHandle extends BaiduBaseHandle<AHandwritingRequest, AHandwritingResp> {
    @Override
    public String getUri() {
        return "/rest/2.0/ocr/v1/handwriting";
    }

    @Override
    public String check(AHandwritingRequest aHandwritingRequest) {
        if (!ApiConstants.bigOrSmall.containsKey(aHandwritingRequest.getRecognizeGranularity())) {
            return "recognizeGranularity 仅支持：" + JSON.toJSONString(aHandwritingRequest.getRecognizeGranularity()) + "，是否定位单字符位置。";
        }
        if (!ApiConstants.trueOrFalse.contains(aHandwritingRequest.getProbability())) {
            return "probability 仅能传：" + ApiConstants.trueOrFalse + ",是否返回识别结果中每一行的置信度";
        }
        if (!ApiConstants.trueOrFalse.contains(aHandwritingRequest.getDetectDirection())) {
            return "detectDirection 仅能传：" + ApiConstants.trueOrFalse + ", 是否检测图像朝向.";
        }

        if (!ApiConstants.trueOrFalse.contains(aHandwritingRequest.getDetectAlteration())) {
            return "detectAlteration 仅能传：" + ApiConstants.trueOrFalse + ",是否检测涂改痕迹";
        }

        if (!ApiConstants.trueOrFalse.contains(aHandwritingRequest.getLanguageType())) {
            return "languageType 仅能传：" + ApiConstants.trueOrFalse + ",识别语言类型.";
        }
        return checkOfd(aHandwritingRequest);
    }

    @Override
    public String getContent(AHandwritingRequest aHandwritingRequest) {
        StringBuffer sb = getOfdContent(aHandwritingRequest);
        if (StringUtils.isNotBlank(aHandwritingRequest.getRecognizeGranularity())) {
            sb.append("&recognize_granularity=").append(aHandwritingRequest.getRecognizeGranularity());
        }
        if (StringUtils.isNotBlank(aHandwritingRequest.getProbability())) {
            sb.append("&probability=").append(aHandwritingRequest.getProbability());
        }
        if (StringUtils.isNotBlank(aHandwritingRequest.getDetectDirection())) {
            sb.append("&detect_direction=").append(aHandwritingRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(aHandwritingRequest.getDetectAlteration())) {
            sb.append("&detect_alteration=").append(aHandwritingRequest.getDetectAlteration());
        }
        if (StringUtils.isNotBlank(aHandwritingRequest.getLanguageType())) {
            sb.append("&language_type=").append(aHandwritingRequest.getLanguageType());
        }
        return sb.toString();
    }
}
