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


import com.aliyun.facebody20191230.models.DeepfakeFaceRequest;
import com.aliyun.facebody20191230.models.DeepfakeFaceResponse;
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.ADeepfakeFaceRequest;
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/DeepfakeFace
 * <p>
 * 换脸鉴别
 * <p>
 * DeepfakeFace
 */
@Component
@Slf4j
@CustomPath("detectPedestrian")
public class ADeepfakeFaceHandle extends AlibabaBaseHandle<ADeepfakeFaceRequest, DeepfakeFaceResponse> {

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

    @Override
    public String check(ADeepfakeFaceRequest aDeepfakeFaceRequest) {
        for (ACommonTextRequest aCommonTextRequest : aDeepfakeFaceRequest.getUrls()) {
            if (StringUtils.isBlank(aCommonTextRequest.getImageUrl())
                    && StringUtils.isBlank(aCommonTextRequest.getImageBase64())) {
                return "url和base64不能同时为空";
            }
        }
        return null;
    }

    @Override
    public DeepfakeFaceResponse run(ADeepfakeFaceRequest aDeepfakeFaceRequest, RuntimeOptions runtime) throws Exception {


        DeepfakeFaceRequest deepfakeFaceRequest = new DeepfakeFaceRequest();
        List<DeepfakeFaceRequest.DeepfakeFaceRequestTasks> tasks = new ArrayList<>();
        for (ACommonTextRequest aCommonTextRequest : aDeepfakeFaceRequest.getUrls()) {
            DeepfakeFaceRequest.DeepfakeFaceRequestTasks task = new DeepfakeFaceRequest.DeepfakeFaceRequestTasks();
            task.setImageURL(aCommonTextRequest.getImageUrl());
            task.setImageData(aCommonTextRequest.getImageBase64());
            tasks.add(task);
        }
        deepfakeFaceRequest.setTasks(tasks);
        // 复制代码运行请自行打印 API 的返回值
        return client.deepfakeFaceWithOptions(deepfakeFaceRequest, runtime);
    }
}
