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

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740283517091#/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%2Fgeneral_basic&method=post
 * <p>
 * <p>
 * 通用文字识别（标准版）
 */
@Component
@Slf4j
@CustomPath("generalBasic")
public class BGeneralBasicHandle extends BaiduBaseHandle<BGeneralBasicRequest, BGeneralBasicResp> {

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

    @Override
    public String check(BGeneralBasicRequest bGeneralBasicRequest) {
        if (!ApiConstants.suportStandLanguage.containsKey(bGeneralBasicRequest.getLanguageType())) {
            return "languageType仅能传：" + JSON.toJSONString(ApiConstants.suportStandLanguage);
        }
        if (!ApiConstants.trueOrFalse.contains(bGeneralBasicRequest.getDetectDirection())) {
            return "detectDirection 仅能传：" + ApiConstants.trueOrFalse;
        }
        if (!ApiConstants.trueOrFalse.contains(bGeneralBasicRequest.getDetectLanguage())) {
            return "detectLanguage 仅能传" + ApiConstants.trueOrFalse + ",false。当前支持中文、英语、日语、韩语";
        }
        if (!ApiConstants.trueOrFalse.contains(bGeneralBasicRequest.getParagraph())) {
            return "paragraph 仅能传" + ApiConstants.trueOrFalse + ",true/false   是否输出段落信息";
        }
        if (!ApiConstants.trueOrFalse.contains(bGeneralBasicRequest.getKeyword())) {
            return "paragraph 仅能传" + ApiConstants.trueOrFalse + ",true/false   是否返回识别结果中每一行的置信度";
        }
        return checkOfd(bGeneralBasicRequest);
    }

    @Override
    public String getContent(BGeneralBasicRequest bGeneralBasicRequest) {
        StringBuffer sb = getOfdContent(bGeneralBasicRequest);
        if (StringUtils.isNotBlank(bGeneralBasicRequest.getLanguageType())) {
            sb.append("&language_type=" + bGeneralBasicRequest.getLanguageType());
        }
        if (StringUtils.isNotBlank(bGeneralBasicRequest.getDetectDirection())) {
            sb.append("&detect_direction=" + bGeneralBasicRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(bGeneralBasicRequest.getDetectLanguage())) {
            sb.append("&detect_language=" + bGeneralBasicRequest.getDetectLanguage());
        }
        if (StringUtils.isNotBlank(bGeneralBasicRequest.getParagraph())) {
            sb.append("&paragraph=" + bGeneralBasicRequest.getParagraph());
        }
        if (StringUtils.isNotBlank(bGeneralBasicRequest.getKeyword())) {
            sb.append("&keyword=" + bGeneralBasicRequest.getKeyword());
        }
        return sb.toString();
    }
}
