package com.heyu.api.baidu.handle.educational;


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.educational.BDocAnalysisRequest;
import com.heyu.api.baidu.response.educational.BDocAnalysisResp;
import org.springframework.stereotype.Component;

/***
 *
 * https://console.bce.baidu.com/support/?_=1740575657628&timestamp=1740583045120#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E6%95%99%E8%82%B2%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Fdoc_analysis&method=post
 *
 *
 * 试卷分析与识别
 *
 */
@Component
public class BDocAnalysisHandle extends BaiduBaseHandle<BDocAnalysisRequest, BDocAnalysisResp> {

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

    @Override
    public String check(BDocAnalysisRequest bDocAnalysisRequest) {
        if (checkNotChnEngOrEng(bDocAnalysisRequest.getLanguageType())) {
            return "languageType必须为" + ApiConstants.chnEngOREng + ", 识别语言类型，默认为CHN_ENG";
        }
        if (!ApiConstants.bigOrSmall.equals(bDocAnalysisRequest.getResultType())) {
            return "resultType必须是" + ApiConstants.bigOrSmall + ", 返回识别结果是按单行结果返回，还是按单字结果返回";
        }
        if(checkNotTrueFalse(bDocAnalysisRequest.getDetectDirection())){
            return "detectDirection必须是" + trueOrFalse + ",  是否检测图像朝向";
        }

        if(checkNotTrueFalse(bDocAnalysisRequest.getLineProbability())){
            return "lineProbability必须是" + trueOrFalse + ",   是否返回每行识别结果的置信度";
        }


        if(checkNotTrueFalse(bDocAnalysisRequest.getDispLinePoly())){
            return "dispLinePoly 必须是" + trueOrFalse + ",  是否返回每行的四角点坐标";
        }



        if(!ApiConstants.handwringOnlyOrHandprintMix.containsKey(bDocAnalysisRequest.getWordsType())){
            return "wordsType 必须是" + StringUtils.printMap(ApiConstants.handwringOnlyOrHandprintMix) + ",  handwring_only：手写文字识别, handprint_mix： 手写印刷混排识别";
        }



        if(checkNotTrueFalse(bDocAnalysisRequest.getLayoutAnalysis())){
            return "layoutAnalysis 必须是" + trueOrFalse + ",  是否分析文档版面：包括layout（图、表、标题、段落、目录）；attribute（栏、页眉、页脚、页码、脚注）的分析输出";
        }


        if(checkNotTrueFalse(bDocAnalysisRequest.getRecgFormula())){
            return "recgFormula 必须是" + trueOrFalse + ",  是否检测并识别公式";
        }


        if(checkNotTrueFalse(bDocAnalysisRequest.getRecgLongDivision())){
            return "recgLongDivision 必须为" + trueOrFalse + ", 是否检测并识别手写竖式";
        }
        return checkPdf(bDocAnalysisRequest);
    }

    @Override
    public String getContent(BDocAnalysisRequest bDocAnalysisRequest) {
        StringBuffer sb = getPdfContent(bDocAnalysisRequest);
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getLanguageType())){
            sb.append("&language_type=" + bDocAnalysisRequest.getLanguageType());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getResultType())){
            sb.append("&result_type=" + bDocAnalysisRequest.getResultType());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getDetectDirection())){
            sb.append("&detect_direction=" + bDocAnalysisRequest.getDetectDirection());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getLineProbability())){
            sb.append("&line_probability=" + bDocAnalysisRequest.getLineProbability());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getDispLinePoly())){
            sb.append("&disp_line_poly=" + bDocAnalysisRequest.getDispLinePoly());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getWordsType())){
            sb.append("&words_type=" + bDocAnalysisRequest.getWordsType());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getLayoutAnalysis())){
            sb.append("&layout_analysis=" + bDocAnalysisRequest.getLayoutAnalysis());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getRecgFormula())){
            sb.append("&recg_formula=" + bDocAnalysisRequest.getRecgFormula());
        }
        if(StringUtils.isNotBlank(bDocAnalysisRequest.getRecgLongDivision())){
            sb.append("&recg_long_division=" + bDocAnalysisRequest.getRecgLongDivision());
        }
        return sb.toString();
    }
}
