package com.mcwl.common.utils.baidu;


import com.mcwl.common.utils.Base64Util;
import com.mcwl.common.utils.HttpUtil;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


/**百度审核接口
 * @author DaiZibo
 * @date 2025/1/16
 * @apiNote
 */


public class BaiduCensor {

    //设置APPID/AK/SK
    public static final String APP_ID = "117094023";
    public static final String API_KEY = "bAfO5WLsaMoGTgznvYTIoxwA";
    public static final String SECRET_KEY = "BU6PmWntW44IMRfotl3ySF6rTHLleVtT";

    //图像审核
    public static String ImageCnesor(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
        try {
            byte[] imgData = downloadImageFromOSS(filePath);
            // 本地文件路径
//   String filePath = "[本地文件路径]";
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            //获取accessToken
            String accessToken = AuthService.getAuth(API_KEY, SECRET_KEY);
            String result = HttpUtil.post(url, accessToken, param);
            //接口说明 https://ai.baidu.com/docs#/ImageCensoring-API/top
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String TextCensor(String content) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
        try {
            String param = "text=" + URLEncoder.encode(content, "utf-8");

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth(API_KEY, SECRET_KEY);

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 从阿里云读取文件
     *
     * @param ossUrl
     * @return
     */
    public static byte[] downloadImageFromOSS(String ossUrl) {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            //读取阿里云的图片使用次方法
            URL url = new URL(ossUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                return outputStream.toByteArray();
            } else {
                System.err.println("Failed to retrieve data from OSS: HTTP error code " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String s = ImageCnesor("https://ybl2112.oss-cn-beijing.aliyuncs.com/2025/MARCH/4/11/53/009ddedc-3102-4ec2-82d2-e69399ba51fa.jpg");
        System.out.println(s);
    }

}
