const { FileOperationMachine } = require("../../connect/file");
const EventEmitter = require("events");
const { doDmLogic } = require("../../service/DM/DMUtils");
const { adapterTranslate } = require("../../service/Translate");
const { adapterTTS } = require("../../service/TTS");
const { startAdapterDH } = require("../../service/DH");
const { getNowTime } = require("../../../utils/timeUtil");

class OSource {
  constructor(oSource, context) {
    Object.assign(this, oSource, context);
    this.connectStateMachine;
    this.eventEmitter = new EventEmitter();
    this.oSink = {};
    this.state = "init";
    this.taskList = [];
  }

  update(oSource) {
    Object.assign(this, oSource);
  }

  mutationUpdate(oSource, oSink = {}) {
    this.close();
    Object.assign(this, oSource);
    Object.assign(this.oSink, oSink);
    this.open();
  }
  open() {
    if (this.oSink?.config.sinkType == "mutation") {
      if (this.oSink.serviceType === "ASR") {
        if (this.oSink.config.integrateType === "sdk") {
          if (this.oSink.config.endpoint.mode === "task") {
            //启动定时任务
            this.interval = setInterval(this.getAsrTaskState.bind(this), 10000);
          }
        }
      } else if (this.oSink.serviceType === "TTS") {
        if (this.oSink.config.integrateType === "sdk") {
          if (this.oSink.config.endpoint.mode === "task") {
            //启动定时任务
            this.interval = setInterval(this.getTTSTaskState.bind(this), 10000);
          }
        }
      }
    }
  }
  close() {
    clearInterval(this.interval);
    this.state = "cancelled";
  }

  async start(param, data) {
    if (data === "success") {
      this.eventEmitter.emit("start", data);
      return;
    }
    if (this.oSink.config.sinkType == "query") {
      if (this.oSink.serviceType === "DM") {
        data = await doDmLogic(data, this.instanceId);
      } else if (this.oSink.serviceType === "DH") {
        data = await startAdapterDH(data, this.oSink);
      }
      this.eventEmitter.emit("start", data);
    } else if (this.oSink.config.sinkType == "job") {
      //todo处理job
      this.eventEmitter.emit("start", "success");
    } else {
      this.eventEmitter.emit("start", "success");
    }
    this.state = "running";
  }

  stop() { }

  pause() { }

  resume() { }

  async query(param, data) {
    if (this.oSink.config.sinkType == "query") {
      if (this.oSink.serviceType === "Translate") {
        data = await adapterTranslate(data, this.oSink);
        console.log("【Translate】接收结果时间：%s ",getNowTime());
      } else if (this.oSink.serviceType === "TTS") {
        data = await adapterTTS(data, this.oSink, this);
        console.log("【TTS】接收结果时间：%s ",getNowTime());

      }
      this.eventEmitter.emit("query", data);
    } else if (this.oSink.config.sinkType == "mutation") {
      if (this.oSink.serviceType === "ASR") {
        if (this.oSink.config.integrateType === "sdk") {
          if (this.oSink.config.endpoint.mode === "task") {
            this.eventEmitter.emit("query", "success");
            //todo 多任务管理
            this.taskList.push({ taskId: data.Data.TaskId, target: data.target, });
          }
        }
      } else if (this.oSink.serviceType === "TTS") {
        if (this.oSink.config.integrateType === "sdk") {
          if (this.oSink.config.endpoint.mode === "task") {
            this.eventEmitter.emit("query", "success");
            //todo 多任务管理
            this.taskList.push({
              taskId: data.Data.TaskId,
              target: data.target,
            });
          }
        }
      }
    } else if (this.oSink.config.sinkType == "job") {
      if (this.oSink.serviceType === "ASR") {
        if (this.oSink.config.integrateType === "sdk") {
          if (this.oSink.config.endpoint.mode === "task") {
            this.eventEmitter.emit("query", data);
          }
        }
      } else if (this.oSink.serviceType === "TTS") {
        if (this.oSink.config.integrateType === "sdk") {
          if (this.oSink.config.endpoint.mode === "task") {
            this.eventEmitter.emit("query", data);
          }
        }
      }
    }
    this.state = "running";
  }

  async getTaskStatus(param, data) {
    this.eventEmitter.emit("getTaskStatus", data);
  }

  async getAsrTaskState() {
    console.log("getAsrTaskState", this.taskList);
    for (let i = 0; i < this.taskList.length; i++) {
      let result = await this.oSink.serviceInstance.describeTaskStatus(this.taskList[i].taskId);
      result.target = this.taskList[i].target;
      this.eventEmitter.emit("query", result);
      if (
        result.Data?.StatusStr == "success" ||
        result.Data?.StatusStr == "failed"
      ) {
        this.taskList.splice(i, 1);
      }
    }
  }

  async getTTSTaskState() {
    console.log("getAsrTaskState", this.taskList);
    for (let i = 0; i < this.taskList.length; i++) {
      let result = await this.oSink.serviceInstance.describeTtsTaskStatus(this.taskList[i].taskId);
      result.target = this.taskList[i].target;
      this.eventEmitter.emit("query", result);
      if (
        result.Data?.StatusStr == "success" ||
        result.Data?.StatusStr == "failed"
      ) {
        this.taskList.splice(i, 1);
      }
    }
  }
}

module.exports.OSource = OSource;
