// import baiduTTS from "./sp/baidu/baidu";
import TencentTTS from "./sp/tencent/tencent";
import aliyunTTS from "./sp/aliyun/aliyun";
import huaweiTTS from "./sp/huawei/huawei";

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

  static getInstance(policy, options) {
    switch (policy) {
      // case "baidu":
      //   return new baiduTTS(options);
      case "tencent":
        return new TencentTTS(options);
      case "ali":
        return new aliyunTTS(options);
      case "huawei":
        return new huaweiTTS(options);
      default:
        return new TencentTTS(options);
    }
  }
  static resAnalysis(policy, res) {
    var result = {
      sp: policy,
      message: "success",
      result: "",
      time:"",
      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 = body.err_msg;
            result.result = body.result;
            result.statusCode = 200;
          } else {
            result.message = body.err_msg;
            result.error = body.err_msg;
            result.statusCode = body.err_no;
          }
          return result;
        }
      case "tencent":
        {
          var body = res;
          result.message = "success";
          result.result = body.Audio;
          result.time=body.time;
          result.statusCode = 200;
          return result;
        }
      case "ali":
        {
          var body = res;
          result.message = "success";
          result.result = res.data;
          result.statusCode = 200;
          return result;
        }
      case "huawei":
        {
          var body = res;
          if (body.httpStatusCode == 200) {
            result.message = "success";
            result.result = body.result.data;
            result.statusCode = 200;
          } else {
            result.message = "error";
            delete res.status;
            result.error = res;
            result.statusCode = body.httpStatusCode || 501;
          }

          return result;
        }
      default:
        return result;
    }
  }
}
export default TTSFactory;
