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

#include "tmg_audio_effect_ctrl_bindings.h"
#include "../include/av_audio_effect_ctrl.h"
#include "../include/av_error.h"

#define CHECK_AUDIO_EFFECT_CTRL_IS_NULL(ret_val) \
  if (!audio_effect_ctrl_) {                     \
    return ret_val;                              \
  }

TMGAudioEffectCtrlBindings::TMGAudioEffectCtrlBindings(
    std::shared_ptr<TMGContextBindings> context_bindings) {
  if (!context_bindings) {
    return;
  }
  ITMGContext* current_context = context_bindings->getTmgContext();
  if (current_context) {
    audio_effect_ctrl_ = current_context->GetAudioEffectCtrl();
  }
}

int TMGAudioEffectCtrlBindings::startPlayMusic(int sound_id,
                                               const std::string& file_path,
                                               int loop_count) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->StartPlayMusic(sound_id, file_path.c_str(), loop_count);
}

int TMGAudioEffectCtrlBindings::stopPlayMusic(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->StopPlayMusic(sound_id);
}

int TMGAudioEffectCtrlBindings::pausePlayMusic(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->PausePlayMusic(sound_id);
}

int TMGAudioEffectCtrlBindings::resumePlayMusic(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->ResumePlayMusic(sound_id);
}

bool TMGAudioEffectCtrlBindings::isMusicPlayEnd(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(false);
  return audio_effect_ctrl_->IsMusicPlayEnd(sound_id);
}

int TMGAudioEffectCtrlBindings::setAllMusicVolume(int volume) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetAllMusicVolume(volume);
}

int TMGAudioEffectCtrlBindings::setMusicPublishVolume(int sound_id, int volume) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetMusicPublishVolume(sound_id, volume);
}

int TMGAudioEffectCtrlBindings::getMusicPublishVolume(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->GetMusicPublishVolume(sound_id);
}

int TMGAudioEffectCtrlBindings::setMusicPlayoutVolume(int sound_id, int volume) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetMusicPlayoutVolume(sound_id, volume);
}

int TMGAudioEffectCtrlBindings::getMusicPlayoutVolume(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->GetMusicPlayoutVolume(sound_id);
}

int TMGAudioEffectCtrlBindings::setMusicPitch(int sound_id, float pitch) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetMusicPitch(sound_id, pitch);
}

int TMGAudioEffectCtrlBindings::getMusicDurationInMS(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->GetMusicDurationInMS(sound_id);
}

int TMGAudioEffectCtrlBindings::getMusicCurrentPosInMS(int sound_id) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->GetMusicCurrentPosInMS(sound_id);
}

int TMGAudioEffectCtrlBindings::seekMusicToPosInTime(int sound_id, int time) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SeekMusicToPosInTime(sound_id, time);
}

int TMGAudioEffectCtrlBindings::enableMusicPublish(int sound_id, bool enable) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->EnableMusicPublish(sound_id, enable);
}

int TMGAudioEffectCtrlBindings::enableMusicPlayout(int sound_id, bool enable) {
  return audio_effect_ctrl_->EnableMusicPlayout(sound_id, enable);
}

int TMGAudioEffectCtrlBindings::setVoiceType(ITMG_VOICE_TYPE type) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetVoiceType(type);
}

int TMGAudioEffectCtrlBindings::setKaraokeType(ITMG_KARAOKE_TYPE type) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetKaraokeType(type);
}

int TMGAudioEffectCtrlBindings::startRecord(const std::string& file_path,
                                            ITMG_AUDIO_RECORDING_CONTENT content) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->StartRecord(file_path.c_str(), content);
}

int TMGAudioEffectCtrlBindings::stopRecord() {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->StopRecord();
}

int TMGAudioEffectCtrlBindings::startSystemAudioLoopback(const std::string& player_path) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->StartSystemAudioLoopback(player_path.c_str());
}

int TMGAudioEffectCtrlBindings::stopSystemAudioLoopback() {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->StopSystemAudioLoopback();
}

int TMGAudioEffectCtrlBindings::setSystemAudioLoopbackVolume(int volume) {
  CHECK_AUDIO_EFFECT_CTRL_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return audio_effect_ctrl_->SetSystemAudioLoopbackVolume(volume);
}
