package com.heyu.api.controller.educational;


import com.heyu.api.baidu.handle.educational.BDocAnalysisHandle;
import com.heyu.api.baidu.request.educational.BDocAnalysisRequest;
import com.heyu.api.baidu.response.educational.BDocAnalysisResp;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.doc.DocAnalysisResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/***
 * https://console.bce.baidu.com/support/?_=1740575657628&timestamp=1742719522919#/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
 *
 * 试卷分析与识别
 *
 *
 * 接口描述
 * 可对文档版面进行分析，输出图、表、标题、文本的位置，并输出分版块内容的OCR识别结果，支持中、英两种语言，手写、印刷体混排多种场景，支持公式识别、手写竖式识别。
 *
 */
@Slf4j
@RestController
@RequestMapping("/doc")
@NotIntercept
public class DocAnalysisController {

    @Autowired
    private BDocAnalysisHandle bDocAnalysisHandle;


    @RequestMapping("/analysis")
    @CacheResult
    public R analysis(BDocAnalysisRequest request) {

        DocAnalysisResp resp = new DocAnalysisResp();

        ApiR<BDocAnalysisResp> bR = bDocAnalysisHandle.handle(request);
        if (bR.isSuccess()) {
            BDocAnalysisResp bDocAnalysisResp = bR.getData();

            resp.setResultsNum(bDocAnalysisResp.getResultsNum());
            resp.setLayoutsNum(bDocAnalysisResp.getLayoutsNum());
            resp.setImgDirection(bDocAnalysisResp.getImgDirection());

            List<DocAnalysisResp.ResultsDTO> resultsDTOList = new ArrayList<>();

            if (CollectionUtils.isNotEmpty(bDocAnalysisResp.getResults())) {

                for (BDocAnalysisResp.ResultsDTO result : bDocAnalysisResp.getResults()) {
                    DocAnalysisResp.ResultsDTO resultsDTO = new DocAnalysisResp.ResultsDTO();
                    resultsDTO.setWordsType(result.getWordsType());
                    resultsDTO.setWords(result.getWordsType());
                    resultsDTOList.add(resultsDTO);
                }
            }

            resp.setResults(resultsDTOList);


            if (CollectionUtils.isNotEmpty(resp.getFormWords())) {
                List<String> formWords = new ArrayList<>();
                for (String formWord : resp.getFormWords()) {
                    formWords.add(formWord);
                }
                resp.setFormWords(formWords);
            }

            if (CollectionUtils.isNotEmpty(resp.getWords())) {
                List<String> words = new ArrayList<>();
                for (String word : resp.getWords()) {
                    words.add(word);
                }
                resp.setWords(words);
            }
        }
        return R.error();
    }


}
