import config from "../../config";
const tencentcloud = require("tencentcloud-sdk-nodejs");

const TtsClient = tencentcloud.tts.v20190823.Client;
function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      var r = Math.random() * 16 | 0,
          v = c == 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
  });
}


class TencentTTS {
    constructor(options) {
        //配置项
        this.config = options.config.params;
        this.token = options.token;
    }

    async textToVoice(text) {
        const clientConfig = {
            credential: {
              secretId:  this.token?.secretId,
              secretKey: this.token?.secretKey,
            },
            region: "ap-shanghai",
            profile: {
              httpProfile: {
                endpoint: "tts.tencentcloudapi.com",
              },
            },
          };
          // 实例化要请求产品的client对象,clientProfile是可选的
          const client = new TtsClient(clientConfig);
          const param = {
            "Text": text,
            "SessionId": guid(),
            "ProjectId": 0,
            "ModelType": 1,
            "PrimaryLanguage": 1,
            "EnableSubtitle": true,
            ...this.config
          };
          var result={};
          try{
            result= await client.TextToVoice(param);
            // console.log("+++++++++++=",result.Subtitles);
            if(param.EnableSubtitle){
              result.time=result.Subtitles[result.Subtitles.length-1].EndTime;
            }
            result.status=200;
          }catch(e){
            result.statusText=e;
            result.status=500;
          }
        return result;
    }

    async createTtsTask(text) {
      const clientConfig = {
          credential: {
            secretId:  this.token?.secretId,
            secretKey: this.token?.secretKey,
          },
          region: "ap-shanghai",
          profile: {
            httpProfile: {
              endpoint: "tts.tencentcloudapi.com",
            },
          },
        };
        // 实例化要请求产品的client对象,clientProfile是可选的
        const client = new TtsClient(clientConfig);
        const param = {
          "Text": text,
          "ModelType": 1,
          ...this.config
        };
        var result={};
        try{
          result= await client.CreateTtsTask(param);
        }catch(e){
          result.statusText=e;
          result.status=500;
        }
      return result;
  }

  async describeTtsTaskStatus(taskId) {
    const clientConfig = {
        credential: {
          secretId:  this.token?.secretId,
          secretKey: this.token?.secretKey,
        },
        region: "ap-shanghai",
        profile: {
          httpProfile: {
            endpoint: "tts.tencentcloudapi.com",
          },
        },
      };
      // 实例化要请求产品的client对象,clientProfile是可选的
      const client = new TtsClient(clientConfig);
      const param = {
        "TaskId": taskId
      };
      var result={};
      try{
        result= await client.DescribeTtsTaskStatus(param);
      }catch(e){
        result.statusText=e;
        result.status=500;
      }
    return result;
}

}

export default TencentTTS;