package com.heyu.api.controller.ocr;


import com.heyu.api.alibaba.request.common.text.GeneralBasicOcrRequest;
import com.heyu.api.baidu.handle.common.BGeneralBasicHandle;
import com.heyu.api.baidu.request.common.BGeneralBasicRequest;
import com.heyu.api.baidu.response.common.BGeneralBasicResp;
import com.heyu.api.controller.BaseController;
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("/general/text/orc/")
public class GeneralBasicOcrController extends BaseController {


    @Autowired
    private BGeneralBasicHandle bGeneralBasicHandle;


    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/recognize1")
    public R recognize1(@RequestBody GeneralBasicOcrRequest generalBasicOcrRequest) throws Exception {
        return recognize(generalBasicOcrRequest);
    }
    // http://localhost:8888/general/text/orc/recognize?imageBase64=3232
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/recognize")
    public R recognize(@RequestBody GeneralBasicOcrRequest generalBasicOcrRequest) throws Exception {


        //String base64 = Base64Utils.encodeImageToBase64("/Users/quyixiao/Desktop/ocr/testxxx.jpg");

        //log.info("base64 :{}",base64);

        //String xx = StringUtils.encodeURIComponent(base64);

        //log.info("xx :{}",xx);




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


        //String a = lines.collect(Collectors.joining());


        //log.info("imagebase64:{}", a);
        //generalBasicOcrRequest.setImageBase64(a);


        //generalBasicOcrRequest.setImageBase64(null);

        //generalBasicOcrRequest.setImageUrl("https://heyuoss.oss-cn-shanghai.aliyuncs.com/prd/testxxx.jpg");




        // 读取文件内容到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(Collectors.joining());


        //log.info("imagebase64:{}", a);
        //generalBasicOcrRequest.setImageBase64(null);
        //generalBasicOcrRequest.setPdfFile("xxxxxx");


        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);
        }


        BGeneralBasicRequest bGeneralBasicRequest = new BGeneralBasicRequest();
        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());

        ApiR<BGeneralBasicResp> aR = bGeneralBasicHandle.handle(bGeneralBasicRequest);
        List<String> list = new ArrayList<>();

        if (aR.isSuccess()) {
            BGeneralBasicResp bGeneralBasicResp = aR.getData();
            if (CollectionUtils.isNotEmpty(bGeneralBasicResp.getWordsResult())) {
                for (BGeneralBasicResp.WordsResultDTO wordsResultDTO : bGeneralBasicResp.getWordsResult()) {
                    list.add(wordsResultDTO.getWords());
                }
                return R.ok().setData(list);
            }
            return R.ok().setMsg(bGeneralBasicResp.getErrorMsg());
        }
        return R.error(aR.getErrorMsg());


    }


}
