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

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740284590514#/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&method=post
 * <p>
 * <p>
 * 通用文字识别（标准含。
 */
@Component
@Slf4j
@CustomPath("general")
public class BGeneralHandle extends BaiduBaseHandle<BGeneralRequest, BGeneralResp> {


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

    @Override
    public String check(BGeneralRequest bGeneralRequest) {

        if (!ApiConstants.bigOrSmall.containsKey(bGeneralRequest.getRecognizeGranularity())) {
            return "recognizeGranularity仅支持" + JSON.toJSONString(ApiConstants.bigOrSmall);
        }

        if (!ApiConstants.suportStandLanguage.containsKey(bGeneralRequest.getLanguageType())) {
            return "languageType 仅支持" + JSON.toJSONString(ApiConstants.suportStandLanguage);
        }


        if (!ApiConstants.trueOrFalse.contains(bGeneralRequest.getDetectDirection())) {
            return "detectDirection 仅支持" + ApiConstants.trueOrFalse + ", true：检测朝向 false：不检测朝向";
        }


        if (!ApiConstants.trueOrFalse.contains(bGeneralRequest.getDetectLanguage())) {
            return "detectLanguage 仅支持" + ApiConstants.trueOrFalse + ",是否检测语言，默认不检测，即：false。当前支持中文、英语、日语、韩语";
        }


        if (!ApiConstants.trueOrFalse.contains(bGeneralRequest.getParagraph())) {
            return "paragraph 仅支持" + ApiConstants.trueOrFalse + ",是否输出段落信息";
        }

        if (!ApiConstants.trueOrFalse.contains(bGeneralRequest.getVertexesLocation())) {
            return "vertexesLocation 仅支持" + ApiConstants.trueOrFalse + ",是否返回文字外接多边形顶点位置，不支持单字位置。";
        }


        if (!ApiConstants.trueOrFalse.contains(bGeneralRequest.getProbability())) {
            return "probability 仅支持" + ApiConstants.trueOrFalse + ",是否返回识别结果中每一行的置信度";
        }

        return checkOfd(bGeneralRequest);
    }


    @Override
    public String getContent(BGeneralRequest bGeneralRequest) {
        StringBuffer sb = getOfdContent(bGeneralRequest);
        if (StringUtils.isNotBlank(bGeneralRequest.getRecognizeGranularity())) {
            sb.append("&recognize_granularity=" + bGeneralRequest.getRecognizeGranularity());
        }

        if (StringUtils.isNotBlank(bGeneralRequest.getLanguageType())) {
            sb.append("&language_type=" + bGeneralRequest.getLanguageType());
        }
        if (StringUtils.isNotBlank(bGeneralRequest.getDetectDirection())) {
            sb.append("&detect_direction=" + bGeneralRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(bGeneralRequest.getDetectLanguage())) {
            sb.append("&detect_language=" + bGeneralRequest.getDetectLanguage());
        }
        if (StringUtils.isNotBlank(bGeneralRequest.getParagraph())) {
            sb.append("&paragraph=" + bGeneralRequest.getParagraph());
        }
        if (StringUtils.isNotBlank(bGeneralRequest.getVertexesLocation())) {
            sb.append("&vertexes_location=" + bGeneralRequest.getVertexesLocation());
        }
        if (StringUtils.isNotBlank(bGeneralRequest.getProbability())) {
            sb.append("&probability=" + bGeneralRequest.getProbability());
        }
        return sb.toString();
    }
}
