import { initDhDriver } from "./dhDriver";
import ControLogic from "./ControlEngine";
import renderLogic from "./RenderEngine";

export default class dmDriver {
  constructor(sessionId, config = {}) {
    this.sessionId = sessionId;
    this.dhDriver = initDhDriver({ sessionId: sessionId });
    this.dhDriver.subscribe(this.dhEvent.bind(this));    

    this.controlLogic=new ControLogic({
      config:config
    });
    this.controlLogic.createrLogic(sessionId);
    this.controlLogic.onOutput=(event)=>{
      //传入表达引擎
      renderLogic(event,this.eventEmitter.bind(this),this.dhDriver);
    }

  }

  async setTask(speechs, config) {
    const queueData = await this.controlLogic.setTask(this.sessionId,speechs,config);
    this.eventEmitter({
      name: "task",
      data: queueData,
    });
  }

  async drive(speechs, config) {
    const queueData = await this.controlLogic.getAnswer(this.sessionId,speechs,config);
    // console.log("dmDriver-------drive>>>>>>>>>>>>>>>>>>>>>>>>>>:",queueData);
    this.eventEmitter({
      name: "task",
      data: queueData,
    });
  }

  stop() {
    this.controlLogic.clear(this.sessionId);
  }

  start() {
    this.controlLogic.start(this.sessionId);
  }

  delay(timeStamp) {
    this.controlLogic.delay(this.sessionId,timeStamp);
  }

  setData(speechs) {
    this.controlLogic.addData(this.sessionId,speechs);
  }


  dhEvent(event) {
    // event.sessionId = this.sessionId;
    // this.onOutput(event);
  }


  // contentsEvent()

  eventEmitter(event, channel) {
    this.onOutput({
      to: channel || "client",
      event: event,
      sessionId: this.sessionId,
    })

  }

  eventHandler(event) {
    switch (event.name) {
      case "join":
        this.dhDriver.eventHandler(event);
        break;
      case "init":
        this.dhDriver.eventHandler(event);
        break;
      case "drive":
        this.drive(event.data);
        break;
      case "task":
        this.setTask(event.data);
        break;
      case "error":
        this.handlerError(event.data);
        break;
    }
  }
  //
  onOutput(){};
}
