import config from "../../config";
import SpeechRecognizer from "./speechrecognizer";




class baiduASR {
    constructor(options) {
        //配置项
        this.appId = config.baidu.appId;
        this.appKey = config.baidu.appKey;
        this.devPID = config.baidu.devPID;
        this.URI = config.baidu.URI;

        this.speechRecognizer = null;
        this.isCanSendData = false;
        this.isCanStop = false;
    }

    //初始化
    async init(params) {
        params = {
            //配置项
            appId: config.baidu.appId,
            appKey: config.baidu.appKey,
            devPID: config.baidu.devPID,
            URI: config.baidu.URI
        }
        if (!this.speechRecognizer) {
            this.speechRecognizer = new SpeechRecognizer(params);
        }

        // 识别变化时
        this.speechRecognizer.OnRecognitionResultChange = (res) => {
            console.log('【百度ASR】识别变化时', res);
            // const currentText = `${resultText}${res.result.voice_text_str}`;

        };
        // 一句话结束
        this.speechRecognizer.OnSentenceEnd = (res) => {
            console.log('【百度ASR】一句话结束', res);
            // resultText += res.result.voice_text_str;
            this.OnSentenceEnd(res);

        };
        // 识别结束
        this.speechRecognizer.OnRecognitionComplete = (res) => {
            console.log('【百度ASR】识别结束', res);
            this.isCanSendData = false;
            this.onRecognitionComplete(res);
        };
        // 识别错误
        this.speechRecognizer.OnError = (res) => {
            console.log('【百度ASR】识别失败', res);
            this.isCanSendData = false;
        };
    }
    async getUrl() {

    }
    //连接
    async start() {
        this.speechRecognizer.start();
        this.isCanStop = true;

    }
    write(data) {
        this.speechRecognizer.write(data);
    }
    // 暂停识别，关闭连接
    stop() {
        if (this.isCanStop) {
            this.speechRecognizer.stop();
        }
    }
    //识别结束
    onRecognitionComplete() {

    }
    //一句话结束
    OnSentenceEnd() {

    }
}

export default baiduASR;