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

/** 获取签名 start */

function toUint8Array(wordArray) {
  // Shortcuts
  const words = wordArray.words;
  const sigBytes = wordArray.sigBytes;

  // Convert
  const u8 = new Uint8Array(sigBytes);
  for (let i = 0; i < sigBytes; i++) {
    u8[i] = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  }
  return u8;
}

function Uint8ArrayToString(fileData) {
  let dataString = "";
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData[i]);
  }
  return dataString;
}
// 签名函数示例
function signCallback(signStr) {
  const secretKey = config.tencent.secretKey;
  const hash = global.CryptoJSTest.HmacSHA1(signStr, secretKey);
  const bytes = Uint8ArrayToString(toUint8Array(hash));
  const result = Buffer.from(bytes, "ascii").toString("base64");
  return result;
}

/** 获取签名 end */

class tencentASR {
  constructor(options) {
    this.speechRecognizer = null;
    this.isCanSendData = false;
    this.isCanStop = false;
    this.config = options.config.params;
    this.token = options.token;
  }
  //初始化
  async init(params) {
    params = {
      signCallback: signCallback, // 鉴权函数 用户提供鉴权函数，不传则为null
      // 用户参数
      secretid: this.token?.secretId || config.tencent.secretId,
      appid: this.token?.appId || config.tencent.appId,
      // 实时识别接口参数
      engine_model_type: this.config?.engine_model_type || "16k_zh", // 引擎
      voice_format: this.config?.voice_format || 1,
      needvad: this.config?.needvad || 1,
      vad_silence_time: 300,
    };
    delete this.config?.engine_model_type;
    delete this.config?.voice_format;
    delete this.config?.needvad;
    // 以下为非必填参数，可跟据业务自行修改
    Object.assign(params, this.config);
    console.log("【Tencent ASR】params>>>>>", params);
    if (!this.speechRecognizer) {
      this.speechRecognizer = new SpeechRecognizer(params);
    }
    // 开始识别
    this.speechRecognizer.OnRecognitionStart = (res) => {
      console.log("开始识别", res);
      this.isCanSendData = true;
      this.isCanStop = true;
    };
    // 一句话开始
    this.speechRecognizer.OnSentenceBegin = (res) => {
      // console.log('一句话开始', res);
    };
    // 识别变化时
    this.speechRecognizer.OnRecognitionResultChange = (res) => {
      // console.log('识别变化时', res);
      // const currentText = `${resultText}${res.result.voice_text_str}`;
    };
    // 一句话结束
    this.speechRecognizer.OnSentenceEnd = (res) => {
      // console.log('一句话结束', res);
      // resultText += res.result.voice_text_str;
      this.OnSentenceEnd(res);
    };
    // 识别结束
    this.speechRecognizer.OnRecognitionComplete = (res) => {
      console.log("识别结束", res);
      this.isCanSendData = false;
      this.onRecognitionComplete(res);
    };
    // 识别错误
    this.speechRecognizer.OnError = (res) => {
      console.log("识别失败", res);
      this.isCanSendData = false;
    };
  }
  //获取url
  async getUrl() {    
    return await this.speechRecognizer.getUrl();
  }
  //连接
  async start() {
    await this.speechRecognizer.start();
  }
  write(data) {
    if (this.isCanSendData) {
      this.speechRecognizer.write(data);
    }
  }
  // 暂停识别，关闭连接
  stop() {
    if (this.isCanStop) {
      this.speechRecognizer.stop();
    }
  }
  //识别结束
  onRecognitionComplete() {}
  //一句话结束
  OnSentenceEnd() {}
}

export default tencentASR;
