import config from "./config";
import ASRFactory from "./ASRFactory";
import ShortVoiceASRFactory from "./ShortVoiceASRFactory";
import TaskASRFactory from "./TaskASRFactory";
import FlashTaskASRFactory from "./FlashTaskASRFactory";

var events = require("events");

export class ASR {
  constructor(params) {
    this.params = params;
    this.source = {};
    this.sp = params.config.endpoint.sp||"tencent";
    this.isRunning = false;
    this.data = {};
    this.isDriver = false;
    this.eventEmitter = new events.EventEmitter();
  }

  async init() {
    this.ASRObject = ASRFactory.getInstance(
      this.sp,
      Object.assign({}, config[this.sp], this.params)
    );

    this.ASRObject.init(this.params);

    this.ASRObject.onRecognitionComplete = (res) => {
      this.onRecognitionComplete(res);
    };

    this.ASRObject.OnSentenceEnd = (res) => {
      //res解析
      res = ASRFactory.resAnalysis(res, this.sp);
      this.OnSentenceEnd(res);
      this.eventEmitter.emit("OnSentenceEnd", res);
    };
  }

  async getUrl() {
    return await this.ASRObject.getUrl();
  }
  async start() {
    await this.ASRObject.start();
  }
  //发送数据
  write(data) {
    this.ASRObject.write(data);
  }
  // 暂停识别，关闭连接
  stop() {
    this.ASRObject.stop();
  }
  //识别结束
  onRecognitionComplete() {}

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

export class ShortVoiceASR {
  constructor(params) {
    this.params = params;
    this.sp = params.config.endpoint.sp||"tencent";
  }

  async speechRecognition(audioDatas) {
    this.ASRObject = ShortVoiceASRFactory.getInstance(this.sp, this.params);
    //去除base64头
    audioDatas = audioDatas.split(",", 2);
    var base64 = audioDatas.length == 2 ? audioDatas[1] : audioDatas[0];
    var result;
    if (this.sp == "huawei" || this.sp == "tencent") {
      result = await this.ASRObject.speechRecognition(base64);
    } else {
      var dataBuffer = Buffer.from(base64, "base64");
      result = await this.ASRObject.speechRecognition(dataBuffer);
    }
    //统一解析处理
    if (result != undefined) {
      result = ShortVoiceASRFactory.resAnalysis(this.sp, result);
    }
    console.log("【Async ASR】REST result:", result);
    return result;
  }
}

export class TaskASR {
  constructor(params) {
    this.params = params;
    this.sp = params.config.endpoint.sp||"tencent";
  }

  async createRecTask(url) {
    this.ASRObject = TaskASRFactory.getInstance(
      this.sp,
      Object.assign({}, config[this.sp], this.params)
    );
    const result = await this.ASRObject.createRecTask(url);
    return result;
  }

  async describeTaskStatus(taskId) {
    this.ASRObject = TaskASRFactory.getInstance(
      this.sp,
      Object.assign({}, config[this.sp], this.params)
    );
    const result = await this.ASRObject.describeTaskStatus(taskId);
    return result;
  }
}

export class FlashTaskASR {
  constructor(params) {
    this.params = params;
    this.sp = params.config.endpoint.sp||"tencent";
  }

  async speechRecognition(audioDatas) {
    this.ASRObject = FlashTaskASRFactory.getInstance(
      this.sp,
      Object.assign({}, config[this.sp], this.params)
    );
  }
}
