package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.CompareFaceResponse;
import com.aliyun.facebody20191230.models.CompareFaceResponseBody;
import com.heyu.api.alibaba.handle.facebody.ACompareFaceHandle;
import com.heyu.api.alibaba.request.facebody.ACompareFaceRequest;
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.face.CompareFaceReq;
import com.heyu.api.resp.face.CompareFaceResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/****
 * https://next.api.aliyun.com/api/facebody/2019-12-30/CompareFace?tab=DEMO&lang=JAVA
 *
 * 人脸比对
 *
 * CompareFace
 *
 * 功能描述
 * 人脸比对 1:1 能力基于您输入的两张图片，分别挑选两张图片中的最大人脸进行比较，判断是否为同一人。
 *
 * 两张人脸图片对比：比对两张图片中人脸的相似度，返回这两个人脸的矩形框坐标、人脸五点关键点坐标、比对的置信度，以及不同误识率的置信度阈值。
 * 支持多种图片类型：支持生活照、证件照、身份证芯片照、带网纹照等类型的人脸对比。
 */
@Slf4j
@RestController
@RequestMapping("/face")
@NotIntercept
public class CompareFaceController extends BaseController {


    @Autowired
    private ACompareFaceHandle aCompareFaceHandle;

    @RequestMapping("/compare")
    @CacheResult
    public R recognize(CompareFaceReq req) {

        CompareFaceResp resp = new CompareFaceResp();

        ACompareFaceRequest aCompareFaceRequest = new ACompareFaceRequest();
        aCompareFaceRequest.setUrlA(req.getImageUrlA());
        aCompareFaceRequest.setUrlB(req.getImageUrlB());
        aCompareFaceRequest.setBase64A(req.getImageBase64A());
        aCompareFaceRequest.setBase64B(req.getImageBase64B());

        ApiR<CompareFaceResponse> aR = aCompareFaceHandle.handle(aCompareFaceRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            CompareFaceResponseBody responseBody = aR.getData().getBody();
            CompareFaceResponseBody.CompareFaceResponseBodyData responseBodyData = responseBody.getData();

            resp.setConfidence(responseBodyData.getConfidence());
            resp.setQualityScoreA(responseBodyData.getQualityScoreA());
            resp.setQualityScoreA(responseBodyData.getQualityScoreB());
            resp.setMessageTips(responseBodyData.getMessageTips());
            resp.setIsMaskA(responseBodyData.getIsMaskA());
            resp.setIsMaskB(responseBodyData.getIsMaskB());
            return R.ok().setData(resp);
        }
        return R.error(aR.getErrorMsg());
    }

}
