package com.heyu.api.controller.imagerecog;


import com.aliyun.imagerecog20190930.models.EvaluateCertificateQualityResponse;
import com.aliyun.imagerecog20190930.models.EvaluateCertificateQualityResponseBody;
import com.heyu.api.alibaba.handle.imagerecog.AEvaluateCertificateQualityHandle;
import com.heyu.api.alibaba.request.imagerecog.AEvaluateCertificateQualityRequest;
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.request.imagerecog.EvaluateCertificateQualityReq;
import com.heyu.api.resp.imagerecog.EvaluateCertificateQualityResp;
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://next.api.aliyun.com/api/imagerecog/2019-09-30/EvaluateCertificateQuality?tab=DOC&lang=JAVA
 *证件照质量审核
 *
 * EvaluateCertificateQuality
 *
 *功能描述
 * 证件照质量审核能力可以识别拍摄的证件照片是否存在质量问题，且有哪些质量问题。
 */
@Slf4j
@RestController
@RequestMapping("/certificate")
@NotIntercept
public class EvaluateCertificateQualityController extends BaseController {

    @Autowired
    private AEvaluateCertificateQualityHandle aEvaluateCertificateQualityHandle;


    @RequestMapping("/quality")
    @CacheResult
    public R quality(EvaluateCertificateQualityReq req) {
        List<EvaluateCertificateQualityResp> respList = new ArrayList<>();
        AEvaluateCertificateQualityRequest request = new AEvaluateCertificateQualityRequest();
        /**
         * 证照类型。
         *
         * IDCard：身份证。
         * BusinessLicense：电子执照。
         * 示例值:
         * BusinessLicense
         */
        request.setType(req.getType());
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        ApiR<EvaluateCertificateQualityResponse> aR = aEvaluateCertificateQualityHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            EvaluateCertificateQualityResponseBody.EvaluateCertificateQualityResponseBodyData responseBodyData = aR.getData().getBody().getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getElements())) {
                for (EvaluateCertificateQualityResponseBody.EvaluateCertificateQualityResponseBodyDataElements element : responseBodyData.getElements()) {
                    EvaluateCertificateQualityResp resp = new EvaluateCertificateQualityResp();
                    resp.setPass(element.getPass());
                    resp.setScore(element.getScore());
                    resp.setValue(element.getValue());
                    respList.add(resp);
                }

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


}
