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

import com.alibaba.fastjson.JSON;
import com.aliyun.imageaudit20191230.models.ScanTextRequest;
import com.aliyun.imageaudit20191230.models.ScanTextResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.imageaudit.AScanTextRequest;
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/ScanText
 * <p>
 * 文本内容安全
 */
@Component
@Slf4j
@CustomPath("scanImage")
public class AScanTextHandle extends AlibabaBaseHandle<AScanTextRequest, ScanTextResponse> {

    private List<String> checkScenes = Arrays.asList("spam",
            "politics",
            "abuse",
            "terrorism",
            "porn",
            "flood",
            "contraband",
            "ad");


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

    @Override
    public String check(AScanTextRequest aScanTextRequest) {
        if (CollectionUtils.isEmpty(aScanTextRequest.getScenes())) {
            return "请传scenes，内容如下 ：spam：文字垃圾内容识别" +
                    "，politics：文字敏感内容识别" +
                    "，abuse：文字辱骂内容识别" +
                    "，terrorism：文字暴恐内容识别" +
                    "，porn：文字鉴黄内容识别" +
                    "，flood：文字灌水内容识别" +
                    "，contraband：文字违禁内容识别" +
                    "，ad：文字广告内容识别 ";
        }
        Set<String> set = new HashSet<>(aScanTextRequest.getScenes());
        if (set.size() > 10) {
            return "scenes的最大长度不能超过10";
        }
        aScanTextRequest.setScenes(new ArrayList<>(set));
        for (String scene : aScanTextRequest.getScenes()) {
            if (!checkScenes.contains(scene)) {
                return scene + "并不在" + JSON.toJSONString(checkScenes) + "之内";
            }
        }
        return null;
    }


    @Override
    public ScanTextResponse run(AScanTextRequest aScanTextRequest, RuntimeOptions runtime) throws Exception {

        ScanTextRequest scanTextRequest = new ScanTextRequest();
        List<ScanTextRequest.ScanTextRequestLabels> scanTextRequestLabels = new ArrayList<>();
        for (String scene : aScanTextRequest.getScenes()) {
            ScanTextRequest.ScanTextRequestLabels scanTextRequestLabel = new ScanTextRequest.ScanTextRequestLabels();
            scanTextRequestLabel.setLabel(scene);
            scanTextRequestLabels.add(scanTextRequestLabel);
        }
        scanTextRequest.setLabels(scanTextRequestLabels);

        List<ScanTextRequest.ScanTextRequestTasks> scanTextRequestTasks = new ArrayList<>();
        for (String content : aScanTextRequest.getContents()) {
            ScanTextRequest.ScanTextRequestTasks scanTextRequestTask = new ScanTextRequest.ScanTextRequestTasks();
            scanTextRequestTask.setContent(content);
            scanTextRequestTasks.add(scanTextRequestTask);
        }
        scanTextRequest.setTasks(scanTextRequestTasks);
        // 复制代码运行请自行打印 API 的返回值
        return client.scanTextWithOptions(scanTextRequest, runtime);
    }
}
