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

#include "tmg_context_bindings.h"
#include "tmg_ptt_bindings.h"
#include "tmg_room_bindings.h"

namespace {
#define CHECK_TMG_CONTEXT_IS_NULL(ret_val) \
  if (!tmg_context_) {                     \
    return ret_val;                        \
  }
const int kAuthBufferLen = 1024;
}  // namespace

TMGContextBindings::TMGContextBindings() {
  tmg_context_ = ITMGContextGetInstance();
  tmg_delegate_ = std::make_shared<TMGDelegateBindings>();
}

void TMGContextBindings::setTMGDelegate(const se::Value& callback) {
  if (callback.isObject() && tmg_context_) {
    tmg_delegate_->setJsTMGDelegate(callback);
    tmg_context_->SetTMGDelegate(tmg_delegate_.get());
  }
}

void TMGContextBindings::setRegion(const std::string& region) {
  if (tmg_context_) {
    tmg_context_->SetRegion(region.c_str());
  }
}

std::string TMGContextBindings::getAuthBuffer(int app_id,
                                              const std::string& room_id,
                                              const std::string& user_id,
                                              const std::string& key) {
  unsigned char auth_buffer[kAuthBufferLen] = {0};
  unsigned int auth_buffer_len = kAuthBufferLen;
  QAVSDK_AuthBuffer_GenAuthBuffer(app_id, room_id.c_str(), user_id.c_str(), key.c_str(),
                                  auth_buffer, auth_buffer_len);

  return reinterpret_cast<char*>(auth_buffer);
}

int TMGContextBindings::init(const std::string& app_id, const std::string& user_id) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->Init(app_id.c_str(), user_id.c_str());
}

void TMGContextBindings::poll() {
  if (tmg_context_) {
    tmg_context_->Poll();
  }
}

void TMGContextBindings::pause() {
  if (tmg_context_) {
    tmg_context_->Pause();
  }
}

void TMGContextBindings::resume() {
  if (tmg_context_) {
    tmg_context_->Resume();
  }
}

int TMGContextBindings::uninit() {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->Uninit();
}

int TMGContextBindings::setScene(int scene) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetScene(static_cast<ITMG_APP_SCENE>(scene));
}

int TMGContextBindings::setAudioRole(int role) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetAudioRole(static_cast<ITMG_AUDIO_MEMBER_ROLE>(role));
}

int TMGContextBindings::setRangeAudioMode(int mode) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetRangeAudioMode(static_cast<ITMG_RANGE_AUDIO_MODE>(mode));
}

int TMGContextBindings::setRangeAudioTeamID(int team_id) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetRangeAudioTeamID(team_id);
}

int TMGContextBindings::enterRoom(const std::string& room_id,
                                  int room_type,
                                  const std::string& user_sig) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->EnterRoom(room_id.c_str(), static_cast<ITMG_ROOM_TYPE>(room_type),
                                 user_sig.c_str(), user_sig.length());
}

int TMGContextBindings::exitRoom() {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->ExitRoom();
}

bool TMGContextBindings::isRoomEntered() {
  CHECK_TMG_CONTEXT_IS_NULL(false);
  return tmg_context_->IsRoomEntered();
}

int TMGContextBindings::setLogLevel(int level_write, int level_print) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetLogLevel(static_cast<ITMG_LOG_LEVEL>(level_write),
                                   static_cast<ITMG_LOG_LEVEL>(level_print));
}

int TMGContextBindings::setLogPath(const std::string& log_dir) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetLogPath(log_dir.c_str());
}

std::string TMGContextBindings::getLogPath() {
  CHECK_TMG_CONTEXT_IS_NULL("");
  return tmg_context_->GetLogPath();
}

std::string TMGContextBindings::getSDKVersion() {
  CHECK_TMG_CONTEXT_IS_NULL("");
  return tmg_context_->GetSDKVersion();
}

void TMGContextBindings::setAppVersion(const std::string& app_version) {
  if (tmg_context_) {
    return tmg_context_->SetAppVersion(app_version.c_str());
  }
}

int TMGContextBindings::showDebugView(bool show) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->ShowDebugView(show);
}

int TMGContextBindings::setRecvMixStreamCount(int count) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetRecvMixStreamCount(count);
}

int TMGContextBindings::setAdvanceParams(const std::string& key, const std::string& value) {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->SetAdvanceParams(key.c_str(), value.c_str());
}

std::string TMGContextBindings::getAdvanceParams(const std::string& key) {
  CHECK_TMG_CONTEXT_IS_NULL("");
  return tmg_context_->GetAdvanceParams(key.c_str());
}

int TMGContextBindings::checkMicPermission() {
  CHECK_TMG_CONTEXT_IS_NULL(nsgme::av::AV_ERR_UNKNOWN);
  return tmg_context_->CheckMicPermission();
}

ITMGContext* TMGContextBindings::getTmgContext() {
  return tmg_context_;
}
