// Copyright (c) 2025 Tencent. All rights reserved.
// Author: felixyyan

#include "tmg_ptt_bindings.h"
#include "../include/av_error.h"

#define CHECK_PTT_IS_NULL(ret_val) \
  if (!ptt_) {                     \
    return ret_val;                \
  }

TMGPTTBindings::TMGPTTBindings(std::shared_ptr<TMGContextBindings> context_bindings) {
  if (!context_bindings) {
    return;
  }
  ITMGContext* current_context = context_bindings->getTmgContext();
  if (current_context) {
    ptt_ = current_context->GetPTT();
  }
}

int TMGPTTBindings::applyPTTAuthbuffer(const std::string& user_sig) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->ApplyPTTAuthbuffer(user_sig.c_str(), user_sig.length());
}

int TMGPTTBindings::setMaxMessageLength(int ms_time) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->SetMaxMessageLength(ms_time);
}

int TMGPTTBindings::startRecording(const std::string& file_path) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->StartRecording(file_path.c_str());
}

int TMGPTTBindings::stopRecording() {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->StopRecording();
}

int TMGPTTBindings::cancelRecording() {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->CancelRecording();
}

int TMGPTTBindings::uploadRecordedFile(const std::string& file_path) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->UploadRecordedFile(file_path.c_str());
}

int TMGPTTBindings::downloadRecordedFile(const std::string& file_id, const std::string& file_path) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->DownloadRecordedFile(file_id.c_str(), file_path.c_str());
}

int TMGPTTBindings::playRecordedFile(const std::string& file_path, ITMG_VOICE_TYPE voice_type) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->PlayRecordedFile(file_path.c_str(), voice_type);
}

int TMGPTTBindings::stopPlayFile() {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->StopPlayFile();
}

int TMGPTTBindings::speechToText(const std::string& file_id,
                                 const std::string& speech_language,
                                 const std::string& translate_language) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->SpeechToText(file_id.c_str(), speech_language.c_str(), translate_language.c_str());
}

int TMGPTTBindings::translateText(const std::string& text,
                                  const std::string& source_language,
                                  const std::string& translate_language) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->TranslateText(text.c_str(), source_language.c_str(), translate_language.c_str());
}

int TMGPTTBindings::getFileSize(const std::string& file_path) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->GetFileSize(file_path.c_str());
}

int TMGPTTBindings::getVoiceFileDuration(const std::string& file_path) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->GetVoiceFileDuration(file_path.c_str());
}

int TMGPTTBindings::startRecordingWithStreamingRecognition(const std::string& file_path,
                                                           const std::string& speech_language,
                                                           const std::string& translate_language) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->StartRecordingWithStreamingRecognition(file_path.c_str(), speech_language.c_str(),
                                                      translate_language.c_str());
}

int TMGPTTBindings::setPTTSourceLanguage(const std::string& source_language) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->SetPTTSourceLanguage(source_language.c_str());
}

int TMGPTTBindings::textToSpeech(int serial_number,
                                 const std::string& text,
                                 const std::string& voice_name,
                                 const std::string& language_code,
                                 float speaking_rate) {
  CHECK_PTT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return ptt_->TextToSpeech(serial_number, text.c_str(), voice_name.c_str(), language_code.c_str(),
                            speaking_rate);
}
