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

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740281560085#/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%2Faccurate&method=post
 *
 *
 * 通用文字识别（高精度。。
 *
 *
 */
@Component
@Slf4j
@CustomPath("accurate")
public class BAccurateHandle extends BaiduBaseHandle<BAccurateRequest, BAccurateResp> {


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

    @Override
    public String check(BAccurateRequest bAccurateRequest) {
        if (!ApiConstants.suportLanguage.containsKey(bAccurateRequest.getLanguageType())) {
            return "language_type支持类型是：" + JSON.toJSONString(ApiConstants.suportLanguage);
        }

        if (!ApiConstants.wordAndLetter.contains(bAccurateRequest.getEngGranularity())) {
            return "recognize_granularity支持的类型是：" + JSON.toJSONString(ApiConstants.wordAndLetter);
        }

        if (!ApiConstants.trueOrFalse.contains(bAccurateRequest.getDetectDirection())) {
            return "detect_direction支持的值是：" + ApiConstants.trueOrFalse;
        }

        if (!ApiConstants.trueOrFalse.contains(bAccurateRequest.getVertexesLocation())) {
            return "vertexes_location支持的值是：" + ApiConstants.trueOrFalse;
        }

        if (!ApiConstants.trueOrFalse.contains(bAccurateRequest.getParagraph())) {
            return "paragraph 支持的值是：" + ApiConstants.trueOrFalse;
        }


        if (!ApiConstants.trueOrFalse.contains(bAccurateRequest.getProbability())) {
            return "probability 支持的值是：" + ApiConstants.trueOrFalse;
        }

        if (!ApiConstants.trueOrFalse.contains(bAccurateRequest.getCharProbability())) {
            return "char_probability 支持的值是：" + ApiConstants.trueOrFalse;
        }
        return checkOfd(bAccurateRequest);
    }


    @Override
    public String getContent(BAccurateRequest bAccurateRequest) {
        StringBuffer sb = getOfdContent(bAccurateRequest);
        if (StringUtils.isNotBlank(bAccurateRequest.getLanguageType())) {
            sb.append("&language_type=" + bAccurateRequest.getLanguageType());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getEngGranularity())) {
            sb.append("&eng_granularity=" + bAccurateRequest.getEngGranularity());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getRecognizeGranularity())) {
            sb.append("&recognize_granularity=" + bAccurateRequest.getRecognizeGranularity());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getDetectDirection())) {
            sb.append("&detect_direction=" + bAccurateRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getVertexesLocation())) {
            sb.append("&vertexes_location=" + bAccurateRequest.getVertexesLocation());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getParagraph())) {
            sb.append("&paragraph=" + bAccurateRequest.getParagraph());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getProbability())) {
            sb.append("&probability=" + bAccurateRequest.getProbability());
        }
        if (StringUtils.isNotBlank(bAccurateRequest.getCharProbability())) {
            sb.append("&char_probability=" + bAccurateRequest.getCharProbability());
        }
        return sb.toString();
    }
}
