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


import com.aliyun.facebody20191230.models.RecognizePublicFaceRequest;
import com.aliyun.facebody20191230.models.RecognizePublicFaceResponse;
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.ARecognizePublicFaceRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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/FaceBeauty?RegionId=cn-shanghai
 *
 *
 * 人脸美颜
 */
@Component
@Slf4j
@CustomPath("publicFace")
public class ARecognizePublicFaceHandle extends AlibabaBaseHandle<ARecognizePublicFaceRequest, RecognizePublicFaceResponse> {


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


    @Override
    public String check(ARecognizePublicFaceRequest aRecognizePublicFaceRequest) {
        if (CollectionUtils.isEmpty(aRecognizePublicFaceRequest.getFaces())) {
            return "请传入faces";
        }

        for (ACommonTextRequest face : aRecognizePublicFaceRequest.getFaces()) {
            if (StringUtils.isBlank(face.getImageUrl())
                    && StringUtils.isBlank(face.getImageBase64())) {
                return "url和base64不能同时为空";
            }
        }
        return null;
    }

    @Override
    public RecognizePublicFaceResponse run(ARecognizePublicFaceRequest aRecognizePublicFaceRequest, RuntimeOptions runtime) throws Exception {

        RecognizePublicFaceRequest request = new RecognizePublicFaceRequest();
        List<RecognizePublicFaceRequest.RecognizePublicFaceRequestTask> tasks = new ArrayList<>();

        for (ACommonTextRequest face : aRecognizePublicFaceRequest.getFaces()) {
            RecognizePublicFaceRequest.RecognizePublicFaceRequestTask faceTask = new RecognizePublicFaceRequest.RecognizePublicFaceRequestTask();
            faceTask.setImageURL(face.getImageUrl());
            faceTask.setImageData(face.getImageBase64());
            tasks.add(faceTask);
        }
        request.setTask(tasks);


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