package com.heyu.api.controller.ocr;


import com.heyu.api.alibaba.request.common.text.HighPrecisionOcrRequest;
import com.heyu.api.baidu.handle.common.BDocAnalysisOfficeHandle;
import com.heyu.api.baidu.ocr.HighPrecisionOcrResp;
import com.heyu.api.baidu.ocr.LineProbabilityResp;
import com.heyu.api.baidu.ocr.WordsLocationResp;
import com.heyu.api.baidu.request.common.BDocAnalysisOfficeRequest;
import com.heyu.api.baidu.response.common.BDocAnalysisOfficeResp;
import com.heyu.api.data.annotation.EbAuthentication;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/high/precision/orc/")
public class HighPrecisionOcrController {


    @Autowired
    private BDocAnalysisOfficeHandle bDocAnalysisOfficeHandle;


    //  http://localhost:8888/high/precision/orc/recognize?imageBase64=3232
    // https://heyuoss.oss-cn-shanghai.aliyuncs.com/prd/testxxx.jpg
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/recognize")
    public R recognize(@RequestBody HighPrecisionOcrRequest generalBasicOcrRequest) throws Exception {

        // 读取文件内容到Stream流中，按行读取
        //Stream<String> lines = Files.lines(Paths.get("/Users/quyixiao/gitb/eb-service-api/api-web/api-interface/src/main/resources/pdfbase64.txt"));

        //String a = lines.collect(Colle
        // ctors.joining());
        //log.info("imagebase64:{}", a);
        //generalBasicOcrRequest.setImageBase64(null);
        //generalBasicOcrRequest.setPdfFile("xxxxxxxxx");
        //generalBasicOcrRequest.setImageUrl("https://heyuoss.oss-cn-shanghai.aliyuncs.com/prd/testxxx.jpg");


        if(StringUtils.isNotBlank(generalBasicOcrRequest.getImageBase64())){
            String imageBase64 = StringUtils.encodeURIComponent(generalBasicOcrRequest.getImageBase64());
            generalBasicOcrRequest.setImageBase64(imageBase64);
        }


        if(StringUtils.isNotBlank(generalBasicOcrRequest.getPdfFile())){
            String pdfFile = StringUtils.encodeURIComponent(generalBasicOcrRequest.getPdfFile());
            generalBasicOcrRequest.setPdfFile(pdfFile);
        }

        if(StringUtils.isNotBlank(generalBasicOcrRequest.getOfdFile())){
            String ofdFile = StringUtils.encodeURIComponent(generalBasicOcrRequest.getOfdFile());
            generalBasicOcrRequest.setOfdFile(ofdFile);
        }





        BDocAnalysisOfficeRequest bGeneralBasicRequest = new BDocAnalysisOfficeRequest();
        bGeneralBasicRequest.setImageBase64(generalBasicOcrRequest.getImageBase64());
        bGeneralBasicRequest.setImageUrl(generalBasicOcrRequest.getImageUrl());
        bGeneralBasicRequest.setPdfFile(generalBasicOcrRequest.getPdfFile());
        bGeneralBasicRequest.setPdfFileNum(generalBasicOcrRequest.getPdfFileNum());
        bGeneralBasicRequest.setOfd_file(generalBasicOcrRequest.getOfdFile());
        bGeneralBasicRequest.setOfd_file_num(generalBasicOcrRequest.getOfdFileNum());
        bGeneralBasicRequest.setLineProbability("true");

        ApiR<BDocAnalysisOfficeResp> aR = bDocAnalysisOfficeHandle.handle(bGeneralBasicRequest);
        List<HighPrecisionOcrResp> list = new ArrayList<>();
        if(aR.isSuccess()){
            BDocAnalysisOfficeResp bGeneralBasicResp  = aR.getData();
            if(CollectionUtils.isNotEmpty(bGeneralBasicResp.getResults())){
                for (BDocAnalysisOfficeResp.ResultsDTO result : bGeneralBasicResp.getResults()) {
                    BDocAnalysisOfficeResp.ResultsDTO.WordsDTO wordsDTO =  result.getWords();
                    if(wordsDTO !=null){
                        HighPrecisionOcrResp highPrecisionOcrResp = new HighPrecisionOcrResp();
                        highPrecisionOcrResp.setText(wordsDTO.getWord());

                        BDocAnalysisOfficeResp.ResultsDTO.WordsDTO.LineProbabilityDTO lineProbabilityDTO = wordsDTO.getLineProbability();


                        LineProbabilityResp lineProbabilityResp = new LineProbabilityResp();
                        lineProbabilityResp.setMin(lineProbabilityDTO.getMin());
                        lineProbabilityResp.setAverage(lineProbabilityDTO.getAverage());


                        highPrecisionOcrResp.setLineProbabilityResp(lineProbabilityResp);

                        BDocAnalysisOfficeResp.ResultsDTO.WordsDTO.WordsLocationDTO wordsLocationDTO = wordsDTO.getWordsLocation();


                        WordsLocationResp wordsLocationResp = new WordsLocationResp();
                        wordsLocationResp.setHeight(wordsLocationDTO.getHeight());
                        wordsLocationResp.setWidth(wordsLocationDTO.getWidth());
                        wordsLocationResp.setTop(wordsLocationDTO.getTop());
                        wordsLocationResp.setLeft(wordsLocationDTO.getLeft());

                        highPrecisionOcrResp.setWordsLocationResp(wordsLocationResp);

                        list.add(highPrecisionOcrResp);
                    }
                }
                return R.ok().setData(list);
            }

            return R.ok().setMsg(bGeneralBasicResp.getErrorMsg());

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




}
