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


import com.aliyun.facebody20191230.models.DetectInfraredLivingFaceRequest;
import com.aliyun.facebody20191230.models.DetectInfraredLivingFaceResponse;
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.ACommonTextRequest;
import com.heyu.api.alibaba.request.facebody.ADetectInfraredLivingFaceRequest;
import com.heyu.api.oss.OssFileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * https://next.api.aliyun.com/api/facebody/2019-12-30/DetectInfraredLivingFace?tab=DEMO&lang=JAVA
 * <p>
 * 红外人脸活体检测
 * <p>
 * DetectInfraredLivingFace
 */
@Component
@Slf4j
@CustomPath("detectInfraredLiving")
public class ADetectInfraredLivingFaceHandle extends AlibabaBaseHandle<ADetectInfraredLivingFaceRequest, DetectInfraredLivingFaceResponse> {


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


    @Autowired
    private OssFileUploadService ossFileUploadService;


    @Override
    public String check(ADetectInfraredLivingFaceRequest aDetectInfraredLivingFaceRequest) {
        for (ACommonTextRequest aCommonTextRequest : aDetectInfraredLivingFaceRequest.getUrls()) {
            if (StringUtils.isBlank(aCommonTextRequest.getImageUrl())
                    && StringUtils.isNotBlank(aCommonTextRequest.getImageBase64())) {
                String url = ossFileUploadService.uploadFileContentByBase64(aCommonTextRequest.getImageBase64(), "jpg");
                if (StringUtils.isNotBlank(url)) {
                    aCommonTextRequest.setImageUrl(url);
                }
            }
        }
        return null;
    }


    @Override
    public DetectInfraredLivingFaceResponse run(ADetectInfraredLivingFaceRequest aDetectInfraredLivingFaceRequest, RuntimeOptions runtime) throws Exception {
        DetectInfraredLivingFaceRequest request = new DetectInfraredLivingFaceRequest();
        List<DetectInfraredLivingFaceRequest.DetectInfraredLivingFaceRequestTasks> tasks = new ArrayList<>();
        for (ACommonTextRequest url : aDetectInfraredLivingFaceRequest.getUrls()) {
            DetectInfraredLivingFaceRequest.DetectInfraredLivingFaceRequestTasks detectInfraredLivingFaceRequestTasks = new DetectInfraredLivingFaceRequest.DetectInfraredLivingFaceRequestTasks();
            detectInfraredLivingFaceRequestTasks.setImageURL(url.getImageUrl());
            tasks.add(detectInfraredLivingFaceRequestTasks);
        }
        request.setTasks(tasks);
        // 复制代码运行请自行打印 API 的返回值
        return client.detectInfraredLivingFaceWithOptions(request, runtime);
    }
}
