import baiduTaskASR from "./sp/baidu/fileBaidu";
import tencentTaskASR from "./sp/tencent/fileTencent";
import aliyunTaskASR from "./sp/aliyun/fileAliyun";
import huaweiTaskASR from "./sp/huawei/fileHuawei";

class TaskASRFactory {
  constructor(sp) {
    this.policy = sp || "baidu";
  }

  static getInstance(policy, options) {
    switch (policy) {
      case "baidu":
        return new baiduTaskASR(options);
      case "tencent":
        return new tencentTaskASR(options);
      case "ali":
        return new aliyunTaskASR(options);
      case "huawei":
        return new huaweiTaskASR(options);
      default:
        return new tencentTaskASR(options);
    }
  }
  static resAnalysis(policy,res) {
    var result = {
      sp:policy,
      message: "success",
      result: "",
      statusCode:200,
      error: {}
    };
    // console.log("res:",res);
    if (res==null||res.status != 200) {
      result.message="error";
      result.error = res.statusText;
      result.statusCode=500||res.status;
      return result;
    }
    switch (policy) {
      case "baidu":
        {
          var body = res.data;
          if (body.err_no == 0) {
            result.message ="success";
            result.result = {text:body.result};
            result.statusCode=200;
          } else {
            result.message = "error";
            result.error = body.err_msg;
            result.statusCode=body.err_no;
          }
          return result;
        }
      case "tencent":
       {
        var body = res;
          result.message ="success";
          result.result ={text: body.Result};
          result.statusCode=200;
        return result;
       }
      case "ali":
        {
          var body = res.data;
          if (body.status == 20000000) {
            result.message ="success";
            result.result = {text:body.result};
            result.statusCode=200;
          } else {
            result.message = body.message;
            result.error = body.message;
            result.statusCode=501;
          }
          return result;
        }
      case "huawei":
        {
          var body = res.data;
            result.message ="success";
            result.result = {text:body.result.text};
            result.statusCode=200;
          return result;
        }
      default:
        return result;
    }

  }
}

export default TaskASRFactory;
