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


import com.aliyun.facebody20191230.models.CompareFaceWithMaskRequest;
import com.aliyun.facebody20191230.models.CompareFaceWithMaskResponse;
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.ACompareFaceWithMaskRequest;
import com.heyu.api.oss.OssFileUploadService;
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/DetectLivingFace
 *
 */
@Component
@Slf4j
@CustomPath("compareFaceWithMask")
public class ACompareFaceWithMaskHandle extends AlibabaBaseHandle<ACompareFaceWithMaskRequest, CompareFaceWithMaskResponse> {


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

    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Override
    public String check(ACompareFaceWithMaskRequest aCompareFaceWithMaskRequest) {
        if (StringUtils.isBlank(aCompareFaceWithMaskRequest.getUrlA())
                && StringUtils.isBlank(aCompareFaceWithMaskRequest.getBase64A())) {
            return "urlA 和 base64A 必须有一个不为空";
        }

        if (StringUtils.isBlank(aCompareFaceWithMaskRequest.getUrlB())
                && StringUtils.isBlank(aCompareFaceWithMaskRequest.getBase64B())) {
            return "urlB 和 base64B 必须有一个不为空";
        }


        if (StringUtils.isBlank(aCompareFaceWithMaskRequest.getUrlA())
                && StringUtils.isNotBlank(aCompareFaceWithMaskRequest.getBase64A())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aCompareFaceWithMaskRequest.getBase64A(), "jpg");
            if (StringUtils.isBlank(url)) {
                log.error("base64A oss file upload is null");
                return "你的base64A内容有误";
            }
            aCompareFaceWithMaskRequest.setUrlA(url);
        }


        if (StringUtils.isBlank(aCompareFaceWithMaskRequest.getUrlB())
                && StringUtils.isNotBlank(aCompareFaceWithMaskRequest.getBase64B())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aCompareFaceWithMaskRequest.getBase64B(), "jpg");
            if (StringUtils.isBlank(url)) {
                log.error("base64B oss file upload is null");
                return "你的base64B内容有误";
            }
        }
        return null;
    }

    @Override
    public CompareFaceWithMaskResponse run(ACompareFaceWithMaskRequest aCompareFaceWithMaskRequest, RuntimeOptions runtime) throws Exception {


        CompareFaceWithMaskRequest compareFaceWithMaskRequest = new CompareFaceWithMaskRequest();
        compareFaceWithMaskRequest.setImageURLA(aCompareFaceWithMaskRequest.getUrlA());
        compareFaceWithMaskRequest.setImageURLB(aCompareFaceWithMaskRequest.getUrlB());
        compareFaceWithMaskRequest.setQualityScoreThreshold(aCompareFaceWithMaskRequest.getQualityScoreThreshold());
        // 复制代码运行请自行打印 API 的返回值
        return client.compareFaceWithMaskWithOptions(compareFaceWithMaskRequest, runtime);
    }
}
