package com.heyu.api.alibaba.handle.facebody;


import com.aliyun.facebody20191230.models.CompareFaceRequest;
import com.aliyun.facebody20191230.models.CompareFaceResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.facebody.ACompareFaceRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * https://next.api.aliyun.com/api/facebody/2019-12-30/CompareFace
 * <p>
 * 人脸比对
 */
@Component
@Slf4j
@CustomPath("compareFace")
public class ACompareFaceHandle extends AlibabaBaseHandle<ACompareFaceRequest, CompareFaceResponse> {


    @Autowired
    private com.aliyun.facebody20191230.Client client;

    @Override
    public String check(ACompareFaceRequest aCompareFaceRequest) {
        if (StringUtils.isBlank(aCompareFaceRequest.getUrlA()) && StringUtils.isBlank(aCompareFaceRequest.getBase64A())) {
            return "urlA和base64A两个参数不能同时为空";
        }
        if (StringUtils.isBlank(aCompareFaceRequest.getUrlB()) && StringUtils.isBlank(aCompareFaceRequest.getBase64B())) {
            return "urlB和base64B两个参数不能同时为空";
        }

        if (StringUtils.isNotBlank(aCompareFaceRequest.getUrlA()) && StringUtils.isNotBlank(aCompareFaceRequest.getBase64A())) {
            return "urlA和base64A两个参数只能传一个参数";
        }
        if (StringUtils.isNotBlank(aCompareFaceRequest.getUrlB()) && StringUtils.isNotBlank(aCompareFaceRequest.getBase64B())) {
            return "urlB和base64B两个参数只能传一个";
        }
        if (aCompareFaceRequest.getQualityScoreThreshold() == null) {
            return "qualityScoreThreshold不能为空";
        }

        return null;
    }

    @Override
    public CompareFaceResponse run(ACompareFaceRequest aCompareFaceRequest, RuntimeOptions runtime) throws Exception {
        CompareFaceRequest compareFaceRequest = new CompareFaceRequest();
        compareFaceRequest.setImageURLA(aCompareFaceRequest.getUrlA());
        compareFaceRequest.setImageURLB(aCompareFaceRequest.getUrlB());
        compareFaceRequest.setImageDataA(aCompareFaceRequest.getBase64A());
        compareFaceRequest.setImageDataB(aCompareFaceRequest.getBase64B());
        compareFaceRequest.setQualityScoreThreshold(aCompareFaceRequest.getQualityScoreThreshold());


        // 复制代码运行请自行打印 API 的返回值
        return client.compareFaceWithOptions(compareFaceRequest, runtime);
    }
}
