package com.tourism.common.huaweicloud.moderation.service;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.moderation.v3.ModerationClient;
import com.huaweicloud.sdk.moderation.v3.model.*;
import com.huaweicloud.sdk.moderation.v3.region.ModerationRegion;
import com.tourism.common.huaweicloud.moderation.config.HuaweiCloudConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class HuaweiCloudModerationService {

    @Autowired
    private HuaweiCloudConfig huaweiCloudConfig;

    /**
     * 文案审核，只要有一个不通过，都认为不通过
     * @param texts
     * @return
     */
    public Boolean runTextModeration(List<String> texts) {
        Boolean result = true;
        ICredential credential = new BasicCredentials()
                .withProjectId(huaweiCloudConfig.getProjectId())
                .withAk(huaweiCloudConfig.getAccessKey())
                .withSk(huaweiCloudConfig.getSecretKey());

        ModerationClient client = ModerationClient.newBuilder()
                .withCredential(credential)
                .withRegion(ModerationRegion.valueOf(huaweiCloudConfig.getRegion()))
                .build();

        RunTextModerationRequest request = new RunTextModerationRequest();
        TextDetectionReq body = new TextDetectionReq();
        TextDetectionDataReq databody = new TextDetectionDataReq();
        body.withCategories(Arrays.asList("terrorism", "porn", "abuse", "ban"));
        for (String text : texts) {
            databody.withText(text);
            body.withData(databody);
            body.withEventType("comment");
            request.withBody(body);
            try {
                RunTextModerationResponse response = client.runTextModeration(request);
                if("pass".equals(response.getResult().getSuggestion())){
                }else {
                    result = false;
                    break;
                }
            } catch (ServiceResponseException e) {
                // 处理异常
                e.printStackTrace();
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * 只要有一张图片不通过，就认为不通过
     * @param urls
     * @return
     */
    public Boolean runImageModeration(List<String> urls) {
        Boolean result = true;
        ICredential credential = new BasicCredentials()
                .withProjectId(huaweiCloudConfig.getProjectId())
                .withAk(huaweiCloudConfig.getAccessKey())
                .withSk(huaweiCloudConfig.getSecretKey());

        ModerationClient client = ModerationClient.newBuilder()
                .withCredential(credential)
                .withRegion(ModerationRegion.valueOf(huaweiCloudConfig.getRegion()))
                .build();

        CheckImageModerationRequest request = new CheckImageModerationRequest();
        ImageDetectionReq body = new ImageDetectionReq();
        body.withCategories(Arrays.asList("terrorism", "porn", "abuse", "ban"));
        body.withEventType("head_image");
        for (String url : urls) {
            body.withUrl(url);
            request.withBody(body);
            try {
                CheckImageModerationResponse response = client.checkImageModeration(request);
                if("pass".equals(response.getResult().getSuggestion())){
                }else {
                    result = false;
                    break;
                }
            } catch (ServiceResponseException e) {
                // 处理异常
                e.printStackTrace();
                result = false;
                break;
            }
        }
        return result;
    }
}
