package com.heyu.api.controller.qrcode;


import com.aliyun.ocr20191230.models.RecognizeQrCodeResponse;
import com.aliyun.ocr20191230.models.RecognizeQrCodeResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeQrCodeHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeQrCodeRequest;
import com.heyu.api.baidu.handle.common.BQrCodeHandle;
import com.heyu.api.baidu.request.common.BQrcodeReconizeRequest;
import com.heyu.api.baidu.response.common.BQrcodeReconizeResp;
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.qrcode.RecognizeQrCodeResp;
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.List;

/***
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeQrCode?tab=DEMO&lang=JAVA
 *
 *
 * 二维码识别
 *
 * RecognizeQrCode
 *
 *
 *
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1742644047533#/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%2Fqrcode&method=post
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/qrcode")

@NotIntercept
public class RecognizeQrCodeController extends BaseController {
    @Autowired
    private ARecognizeQrCodeHandle arecognizeQrCodeHandle;


    @Autowired
    private BQrCodeHandle bqrCodeHandle;

    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BQrcodeReconizeRequest request) {
        RecognizeQrCodeResp resp = new RecognizeQrCodeResp();

        ApiR<BQrcodeReconizeResp> bR = bqrCodeHandle.handle(request);
        if (bR.isSuccess()) {
            BQrcodeReconizeResp bQrcodeReconizeResp = bR.getData();
            List<BQrcodeReconizeResp.CodesResultDTO> codesResultDTOS = bQrcodeReconizeResp.getCodesResult();
            for (BQrcodeReconizeResp.CodesResultDTO codesResultDTO : codesResultDTOS) {
                resp.setQrCodesData(codesResultDTO.getText());
                resp.setType(resp.getType());
                return R.ok().setData(resp);
            }
        }

        ARecognizeQrCodeRequest arecognizeQrCodeRequest = new ARecognizeQrCodeRequest();
        arecognizeQrCodeRequest.setImageBase64(request.getImageBase64());
        arecognizeQrCodeRequest.setImageUrl(request.getImageUrl());

        ApiR<RecognizeQrCodeResponse> aR = arecognizeQrCodeHandle.handle(arecognizeQrCodeRequest);

        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeQrCodeResponse response = aR.getData();
            RecognizeQrCodeResponseBody responseBody = response.getBody();
            RecognizeQrCodeResponseBody.RecognizeQrCodeResponseBodyData responseBodyData = responseBody.getData();
            List<RecognizeQrCodeResponseBody.RecognizeQrCodeResponseBodyDataElements> responseBodyDataElements = responseBodyData.getElements();
            if (CollectionUtils.isNotEmpty(responseBodyDataElements)) {
                RecognizeQrCodeResponseBody.RecognizeQrCodeResponseBodyDataElements responseBodyDataElements1 = responseBodyDataElements.get(0);

                List<RecognizeQrCodeResponseBody.RecognizeQrCodeResponseBodyDataElementsResults> results = responseBodyDataElements1.getResults();
                RecognizeQrCodeResponseBody.RecognizeQrCodeResponseBodyDataElementsResults responseBodyDataElementsResults = results.get(0);
                resp.setQrCodesData(responseBodyDataElementsResults.getQrCodesData());
                resp.setRate(responseBodyDataElementsResults.getRate());
                resp.setSuggestion(responseBodyDataElementsResults.getSuggestion());
                resp.setLabel(responseBodyDataElementsResults.getLabel());
                return R.ok().setData(resp);
            }
        }


        return R.error();
    }
}
