package com.heyu.api.controller.educational;


import com.heyu.api.baidu.handle.educational.BPenHandle;
import com.heyu.api.baidu.request.educational.BPenRequest;
import com.heyu.api.baidu.response.educational.BPenResp;
import com.heyu.api.controller.BaseController;
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.PenResp;
import lombok.extern.slf4j.Slf4j;
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=1742720986912#/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%2Fpen&method=post
 *
 *
 * 词典笔文字识别
 *
 *
 *
 接口描述
 面向词典笔应用场景，是词典笔的基本功能之一，主要用于扫描文字并识别，为字词查询和翻译提供文本信息支撑。支持中英文识别、印刷手写混排识别，满足多种复杂背景下的文字扫描识别。



 */
@Slf4j
@RestController
@RequestMapping("/doc")
@NotIntercept
public class PenController extends BaseController {


    @Autowired
    private BPenHandle bPenHandle;


    @RequestMapping("/pen")
    @CacheResult
    public R pen(BPenRequest request) {
        List<PenResp> respList = new ArrayList<>();

        ApiR<BPenResp> bR = bPenHandle.handle(request);
        if (bR.isSuccess()) {
            BPenResp bPenResp = bR.getData();
            for (BPenResp.WordsResultDTO wordsResultDTO : bPenResp.getWordsResult()) {
                PenResp penResp = new PenResp();
                penResp.setWords(wordsResultDTO.getWords());

                PenResp.ProbabilityDTO probabilityDTO = new PenResp.ProbabilityDTO();
                BPenResp.WordsResultDTO.ProbabilityDTO probability = wordsResultDTO.getProbability();


                probabilityDTO.setAverage(probability.getAverage());
                probabilityDTO.setVariance(probability.getVariance());
                probabilityDTO.setMin(probability.getMin());

                penResp.setProbability(probabilityDTO);

            }

            return R.ok().setData(respList);
        }

        return R.error(bR.getErrorMsg());
    }
}
