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


import com.aliyun.facebody20191230.models.RecognizeActionRequest;
import com.aliyun.facebody20191230.models.RecognizeActionResponse;
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.ARecognizeActionRequest;
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/RecognizeAction
 * <p>
 * 动作行为识别 RecognizeAction
 */
@Component
@Slf4j
@CustomPath("action")
public class ARecognizeActionHandle extends AlibabaBaseHandle<ARecognizeActionRequest, RecognizeActionResponse> {


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

    @Override
    public String check(ARecognizeActionRequest aRecognizeActionRequest) {
        if (StringUtils.isBlank(aRecognizeActionRequest.getVideoUrl())
                && StringUtils.isBlank(aRecognizeActionRequest.getVideoBase64())
                && CollectionUtils.isEmpty(aRecognizeActionRequest.getImageUrlList())) {
            return "videoUrl:视频url或videoBase64视频ba64编码或图片列表，你总要输入一个";
        }

        return null;
    }

    @Override
    public RecognizeActionResponse run(ARecognizeActionRequest aRecognizeActionRequest, RuntimeOptions runtime) throws Exception {

        RecognizeActionRequest request = new RecognizeActionRequest();
        if (StringUtils.isNotBlank(aRecognizeActionRequest.getVideoUrl())
                || StringUtils.isNotBlank(aRecognizeActionRequest.getVideoBase64())) {
            /**
             * 输入内容类型，包括视频和图像两种类型。
             * 0：表示输入视频 URL 或视频 Base64 编码字符串。
             * 1：表示输入图像 URL 或图像 Base64 编码字符串。
             * 其他：非法输入。
             * 示例值:
             * 0
             */
            request.setType(0);
            request.setVideoUrl(aRecognizeActionRequest.getVideoUrl());
            request.setVideoData(aRecognizeActionRequest.getVideoBase64());

        } else {
            request.setType(1);
            List<RecognizeActionRequest.RecognizeActionRequestURLList> URLList = new ArrayList<>();
            for (ACommonTextRequest aCommonTextRequest : aRecognizeActionRequest.getImageUrlList()) {
                RecognizeActionRequest.RecognizeActionRequestURLList recognizeActionRequestURL = new RecognizeActionRequest.RecognizeActionRequestURLList();
                recognizeActionRequestURL.setURL(aCommonTextRequest.getImageUrl());
                recognizeActionRequestURL.setImageData(aCommonTextRequest.getImageBase64());
                URLList.add(recognizeActionRequestURL);
            }
            request.setURLList(URLList);
        }

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