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


import com.aliyun.imageaudit20191230.models.ScanImageRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
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.imageaudit.AScanImageRequest;
import com.heyu.api.alibaba.request.imageaudit.AScanImageRequestTask;
import com.heyu.api.oss.OssFileUploadService;
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.*;


/**
 * https://next.api.aliyun.com/api/imageaudit/2019-12-30/ScanImage
 * <p>
 * 图片内容安全
 */
@Component
@Slf4j
@CustomPath("scanImage")
public class AScanImageHandle extends AlibabaBaseHandle<AScanImageRequest, ScanImageResponse> {

    private static final List<String> checkScene = Arrays.asList("porn", "terrorism", "ad", "live", "logo");


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

    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Override
    public String check(AScanImageRequest aScanImageRequest) {
        if (CollectionUtils.isEmpty(aScanImageRequest.getScenes())) {
            return "scenes不能为空,请传 porn：图片智能鉴黄" +
                    "，terrorism：图片敏感内容识别、图片风险人物识别" +
                    "，ad：图片垃圾广告识别" +
                    "，live：图片不良场景识别" +
                    "，logo：图片 Logo 识别，支持同时传多个场景，但scenes总场景数不能超过6";

        }
        Set<String> set = new HashSet<>(aScanImageRequest.getScenes());
        if (set.size() > 6) {
            return "scenes的总长度不能超过6";
        }
        aScanImageRequest.setScenes(Arrays.asList(set.toArray(new String[0])));
        for (String scene : aScanImageRequest.getScenes()) {
            if (!checkScene.contains(scene)) {
                return "scenes的scene不在场景之中";
            }
        }

        if (CollectionUtils.isEmpty(aScanImageRequest.getTasks())) {
            return "tasks不能为空";
        }

        for (AScanImageRequestTask task : aScanImageRequest.getTasks()) {
            if (StringUtils.isNotBlank(task.getImageBase64())) {
                String url = ossFileUploadService.uploadFileContentByBase64(task.getImageBase64(),"jpg");
                if (StringUtils.isBlank(url)) {
                    log.error("AScanImageHandle oss file upload is null");
                    return "你的base64内容有误";
                }
                task.setImageUrl(url);
            }
        }
        return null;
    }



    @Override
    public ScanImageResponse run(AScanImageRequest aScanImageRequest, RuntimeOptions runtime) throws Exception {
        ScanImageRequest scanImageRequest = new ScanImageRequest();
        scanImageRequest.setScene(aScanImageRequest.getScenes());
        List<ScanImageRequest.ScanImageRequestTask> scanImageRequests = new ArrayList<>();

        for (AScanImageRequestTask task : aScanImageRequest.getTasks()) {
            ScanImageRequest.ScanImageRequestTask task1 = new ScanImageRequest.ScanImageRequestTask();

            task1.setDataId(UUID.randomUUID().toString());
            task1.setImageURL(task.getImageUrl());
            task1.setImageTimeMillisecond(task.getImageTimeMillisecond());
            task1.setInterval(task.getInterval());
            task1.setMaxFrames(task.getMaxFrames());

            scanImageRequests.add(task1);
        }

        scanImageRequest.setTask(scanImageRequests);

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