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

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740288076380#/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%2Fdoc_analysis_office&method=post
 */
@Component
@Slf4j
@CustomPath("docAnalysisOffice")
public class BDocAnalysisOfficeHandle extends BaiduBaseHandle<BDocAnalysisOfficeRequest, BDocAnalysisOfficeResp> {


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

    @Override
    public String check(BDocAnalysisOfficeRequest bDocAnalysisOfficeRequest) {
        if (!ApiConstants.suportLanguage.containsKey(bDocAnalysisOfficeRequest.getLanguageType())) {
            return "languageType仅支持" + JSON.toJSONString(bDocAnalysisOfficeRequest.getLanguageType()) + "这几种类型";
        }

        if (!ApiConstants.bigOrSmall.containsKey(bDocAnalysisOfficeRequest.getResultType())) {
            return "resultType仅支持" + JSON.toJSONString(bDocAnalysisOfficeRequest.getResultType()) + ",返回识别结果是按单行结果返回，还是按单字结果返回";
        }

        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getCharProbability())) {
            return "charProbability仅支持" + ApiConstants.trueOrFalse + ",是否返回单字符置信度";
        }

        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getDetectDirection())) {
            return "detectDirection仅支持" + ApiConstants.trueOrFalse + ",是否检测图像朝向";
        }
        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getLineProbability())) {
            return "lineProbability仅支持" + ApiConstants.trueOrFalse + ",是否返回每行识别结果的置信度。默认为false";
        }
        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getDispLinePoly())) {
            return "dispLinePoly仅支持" + ApiConstants.trueOrFalse + ",是否返回每行的四角点坐标";
        }
        if (!ApiConstants.handwringOnlyOrHandprintMix.containsKey(bDocAnalysisOfficeRequest.getWordsType())) {
            return "wordsType仅支持" + JSON.toJSONString(ApiConstants.handwringOnlyOrHandprintMix);
        }

        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getLayoutAnalysis())) {
            return "layoutAnalysis仅支持" + ApiConstants.trueOrFalse + ",是否分析文档版面：包括layout（图、表、标题、段落、目录、印章）；attribute（栏、页眉、页脚、页码、脚注）的分析输出。";
        }

        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getRecgTables())) {
            return "recgTables 仅支持" + ApiConstants.trueOrFalse + ",是否识别并输出表格相关信息，包括单元格内容。";
        }

        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getRecogSeal())) {
            return "recogSeal 仅支持" + ApiConstants.trueOrFalse + ",是否识别并输出印章相关信息";
        }


        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getRecgFormula())) {
            return "recgFormula 仅支持" + ApiConstants.trueOrFalse + ",是否检测并识别公式，公式以Latex格式返回";
        }


        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getEraseSeal())) {
            return "eraseSeal 仅支持" + ApiConstants.trueOrFalse + ",是否先擦除水印、印章后再识别文档。";
        }

        if (!ApiConstants.trueOrFalse.contains(bDocAnalysisOfficeRequest.getDispUnderlineAnalysis())) {
            return "dispUnderlineAnalysis 仅支持" + ApiConstants.trueOrFalse + ",是否识别并输出下划线";
        }
        return checkOfd(bDocAnalysisOfficeRequest);

    }

    @Override
    public String getContent(BDocAnalysisOfficeRequest bDocAnalysisOfficeRequest) {
        StringBuffer sb = getOfdContent(bDocAnalysisOfficeRequest);
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getLanguageType())) {
            sb.append("&language_type=").append(bDocAnalysisOfficeRequest.getLanguageType());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getResultType())) {
            sb.append("&result_type=").append(bDocAnalysisOfficeRequest.getResultType());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getCharProbability())) {
            sb.append("&char_probability=").append(bDocAnalysisOfficeRequest.getCharProbability());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getDetectDirection())) {
            sb.append("&detect_direction=").append(bDocAnalysisOfficeRequest.getDetectDirection());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getLineProbability())) {
            sb.append("&line_probability=").append(bDocAnalysisOfficeRequest.getLineProbability());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getDispLinePoly())) {
            sb.append("&disp_line_poly=").append(bDocAnalysisOfficeRequest.getDispLinePoly());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getWordsType())) {
            sb.append("&words_type=").append(bDocAnalysisOfficeRequest.getWordsType());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getLayoutAnalysis())) {
            sb.append("&layout_analysis=").append(bDocAnalysisOfficeRequest.getLayoutAnalysis());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getRecgTables())) {
            sb.append("&recg_tables=").append(bDocAnalysisOfficeRequest.getRecgTables());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getRecogSeal())) {
            sb.append("&recg_seal=").append(bDocAnalysisOfficeRequest.getRecogSeal());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getRecgFormula())) {
            sb.append("&recg_formula=").append(bDocAnalysisOfficeRequest.getRecgFormula());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getEraseSeal())) {
            sb.append("&erase_seal=").append(bDocAnalysisOfficeRequest.getEraseSeal());
        }
        if (StringUtils.isNotBlank(bDocAnalysisOfficeRequest.getDispUnderlineAnalysis())) {
            sb.append("&disp_underline_analysis=").append(bDocAnalysisOfficeRequest.getDispUnderlineAnalysis());
        }
        return sb.toString();
    }
}
