const fs = require("fs");
const Url = require("url");
const redisService = require("../SDK/redisService").RedisService;
const { ResourceService } = require("./resourceService");
const resourceService = new ResourceService();
const Transform = require("../lib/transform/transform").Transform;
/** 保存transformInstance <instanceId,transformInstance>  */
const transformMap = new Map();
const TRANSFORM_INSTANCE = "TRANSFORM_INSTANCE_";
const axios = require("axios");
const asrMap = new Map();
const sixHoursSeconds = 6 * 60 * 60;

class TransformService {
  constructor() {}

  /**  获取state */
  async getState(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.getState(params);
    return result;
  }
  /**  分配 实例 */
  async create(params, type) {
    const transformInstance = new Transform({ serviceName: type });
    const instanceId = await transformInstance.create(params);
    // redisService.setObject(TRANSFORM_INSTANCE + instanceId, transformInstance);
    transformMap.set(instanceId, transformInstance);
    return { instanceId: instanceId };
  }
  /**释放  */
  async release(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.release(params);
    transformInstance = null;
    transformMap.delete(params.instanceId);
    return result;
  }

  /**更新  */
  async update(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.update(params);
    return result;
  }

  /**mutation更新  */
  async mutationUpdate(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.mutationUpdate(params);
    return result;
  }

  /**命令：open */
  async cmdOpen(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdOpen(params);
    return result;
  }

  /**命令：开始识别 */
  async cmdStart(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdStart(params);
    return result;
  }

  /**命令：close */
  async cmdClose(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdClose(params);
    return result;
  }

  /**命令：停止识别 */
  async cmdStop(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdStop(params);
    return result;
  }

  /**命令：暂停识别 */
  async cmdPause(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdPause(params);
    return result;
  }

  /**命令：开始 */
  async cmdResume(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdResume(params);
    return result;
  }
  /**命令：查询 */
  async cmdQuery(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdQuery(params);
    return result;
  }
  /**命令：查询 */
  async cmdGetTaskStatus(params, type) {
    var transformInstance = await this.getTransformInMap(params.instanceId);
    var result = await transformInstance.cmdGetTaskStatus(params);
    return result;
  }

  async getTransformInMap(instanceId) {
    var transformInstance = transformMap.get(instanceId);
    if (!transformInstance) {
      throw new Error("【transformInstance】 is close");
    }
    return transformInstance;
  }
}

exports.TransformService = TransformService;
