package com.bohuikeji.frame.module.admin.utils;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;

/**
 * 百度AI工具类
 */
@Slf4j
public class BaiDuAiUtils {

    /**
     * 获取银行卡信息
     * @param file
     * @return
     * @throws IOException
     */
    public static JSONObject bankCard(MultipartFile file) throws IOException {
        return getOrcClient().bankcard(file.getBytes(),new HashMap<String, String>());
    }

    /**
     * 获取银行卡信息
     * @param bytes
     * @return
     * @throws IOException
     */
    public static JSONObject bankCard(byte[] bytes)throws IOException{
        return getOrcClient().bankcard(bytes,new HashMap<String, String>());
    }

    /**
     *  语音合成接口
     * @return 返回音频链接
     */
    public static String tts(String text){
        // 设置可选参数
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("vol", "15");
        options.put("per", "0");

        // 调用接口
        TtsResponse res = getTtsClient().synthesis(text, "zh", 1, options);
        byte[] data = res.getData();

        //MinioUtil.uploadFiles()

        //JSONObject res1 = res.getResult();
        if (data != null) {
            return MinioUtil.uploadFile(new ByteArrayInputStream(data),".mp3");
        }

        return null;
    }


    /**
     * 获取客户端示例
     * @return
     */
    public static AipOcr getOrcClient(){
        return OrcClientHolder.orcClient;
    }
    public static AipSpeech getTtsClient(){
        return OrcClientHolder.ttsClient;
    }

    /**
     * 单例获取
     */
    private static class OrcClientHolder{
        //设置APPID/AK/SK
        private static final String APP_ID = "20193180";
        private static final String API_KEY = "kpW6ltg5YqxPWvOV2qS5ZRno";
        private static final String SECRET_KEY = "gnBqpUzCKgSiwto6eAOfzuIRBZVomxlR";
        private static AipOcr orcClient;
        private static AipSpeech ttsClient;
        static {
            orcClient = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
            ttsClient = new AipSpeech("24113404", "jWKHRnhM2kKjN05U2xEyRSze", "R70tzodySRq9p1UhjonLXwzxYH8FjVb8");
        }
    }
}
