const EventEmitter = require("events");

const { RtcSDK } = require("../service/RTC/rtcSDK");



export class RtcWorker extends EventEmitter{
  constructor(params) {
    super();
    this.isClose = false;
  }

  async pullAudiio(params) {
    var roomId =params.roomId;
    var peerId =params.peerId;
    this.rtc = new RtcSDK(params);
    const that = this;
    this.rtc.socketConnect().then(async () => {
      this.rtc.pullAudio(roomId, peerId);
      console.log("【RTC】Start PULL",roomId,peerId,await this.rtc.roomStats());
      this.rtc.on('newAudioData', (data) => {
        if (this.isClose) {
          console.log("【RTC】Stop");
         return;
        }
        that.emit("data", data);
      });
    });
  }

  async subMsg(params) {
    var roomId =params.roomId;
    this.rtc = new RtcSDK(params);
    const that = this;
    this.rtc.socketConnect().then(async () => {
      this.rtc.roomStatus(roomId);
      console.log("【RTC】Start PULL",roomId,await this.rtc.roomStats());
      this.rtc.on('roomStatus', (data) => {
        console.log("【RTC】roomStatus",data);
        that.emit("roomStatus", data);
      });
    });
  }

  close(){
    this.isClose=true;
    this.rtc.socketClose();
  }

  start(){
    this.isClose=true;
  }

  stop() {
    this.isClose=false;
  }

}