package com.heyu.api.controller.common;


import com.heyu.api.baidu.handle.common.BWebImageLocHandle;
import com.heyu.api.baidu.request.common.BWebImageLocRequest;
import com.heyu.api.baidu.response.common.BWebImageLocResp;
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.common.WebImageLocLineResp;
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/?_=1740219852952&timestamp=1740294887952#/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%2Fwebimage_loc&method=post
 * 网络图片文字识别（含位置版）
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/image/web/line")
@NotIntercept
public class WebImageLocLineController {


    @Autowired
    private BWebImageLocHandle bWebImageLocHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BWebImageLocRequest request) {


        request.setRecognizeGranularity("big");

        List<WebImageLocLineResp> respList = new ArrayList<>();

        ApiR<BWebImageLocResp> bR = bWebImageLocHandle.handle(request);
        if(bR.isSuccess()){

            BWebImageLocResp bWebImageLocResp =  bR.getData();
            if(CollectionUtils.isNotEmpty(bWebImageLocResp.getWordsResult())){
                for (BWebImageLocResp.WordsResultDTO wordsResultDTO : bWebImageLocResp.getWordsResult()) {
                    WebImageLocLineResp resp = new WebImageLocLineResp();

                    resp.setWords(wordsResultDTO.getWords());
                    // 当 probability=true 时返回该字段。识别结果中每一行的置信度值，包含 average：行置信度平均值，variance：行置信度方差，min：行置信度最小值
                    WebImageLocLineResp.ProbabilityDTO probabilityDTO = new WebImageLocLineResp.ProbabilityDTO();

                    WebImageLocLineResp.LocationDTO locationDTO = new WebImageLocLineResp.LocationDTO();

                    if(wordsResultDTO.getProbability() !=null){
                        probabilityDTO.setMin(wordsResultDTO.getProbability().getMin());
                        probabilityDTO.setVariance(wordsResultDTO.getProbability().getVariance());
                        probabilityDTO.setAverage(wordsResultDTO.getProbability().getAverage());

                    }

                    if(wordsResultDTO.getLocation() !=null){
                        locationDTO.setTop(wordsResultDTO.getLocation().getTop());
                        locationDTO.setLeft(wordsResultDTO.getLocation().getLeft());
                        locationDTO.setWidth(wordsResultDTO.getLocation().getWidth());
                        locationDTO.setHeight(wordsResultDTO.getLocation().getHeight());
                    }

                    resp.setDirection(bWebImageLocResp.getDirection() + "");
                    resp.setProbability(probabilityDTO);
                    resp.setLocation(locationDTO);


                    List<WebImageLocLineResp.CharsDTO> chars = new ArrayList<>();
                    if(CollectionUtils.isNotEmpty(wordsResultDTO.getChars())){
                        for (BWebImageLocResp.WordsResultDTO.CharsDTO aChar : wordsResultDTO.getChars()) {
                            WebImageLocLineResp.CharsDTO c = new  WebImageLocLineResp.CharsDTO();
                            c.setCharX(aChar.getCharX());

                            if(aChar.getLocation() !=null){
                                WebImageLocLineResp.CharsDTO.LocationDTO location = new WebImageLocLineResp.CharsDTO.LocationDTO();

                                location.setTop(aChar.getLocation().getTop());
                                location.setLeft(aChar.getLocation().getLeft());
                                location.setWidth(aChar.getLocation().getWidth());
                                location.setHeight(aChar.getLocation().getHeight());
                                c.setLocation(location);
                            }
                            chars.add(c);
                        }
                    }
                    respList.add(resp);
                }
            }
        }
        return R.error();
    }

}
