#include "use_room.h"
#include "log2ui.h"
#include "api_c/musermanager_c.h"
#include "com/transfer_c_cpp.h"
#include "listenerset.h"

static ListenerSet<Use_Room> sets;
///
Use_Room::Use_Room(QWidget *parent) : Com_Room(parent), room_(0) {
}
Use_Room::~Use_Room() {
}
///
void Use_Room::modulesAttach() {
    if (ui.audio) {
        ui.audio->onAttach(room_);
	}
    if (ui.video) {
        ui.video->onAttach(room_);
	}
}
Result Use_Room::api_joinRoom(const char* roomId, const char* userId, const char* userName, const char* userData) {
	cb_room_.room_onJoinResult = Use_Room::s_onJoinResult;
	cb_room_.room_onLeaveIndication = Use_Room::s_onLeaveIndication;
	cb_room_.room_onPublicData = Use_Room::s_onPublicData;
	cb_room_.room_onPrivateData = Use_Room::s_onPrivateData;
	cb_room_.room_onAppDataNotify = Use_Room::s_onAppDataNotify;
	cb_room_.room_onRoomStatusNotify = Use_Room::s_onRoomStatusNotify;
	cb_room_.room_onConnectionStatus = Use_Room::s_onConnectionStatus;
	Result ret = avd_room_init(roomId, &cb_room_, room_);
	if (RT_FAILED(ret)) {
		return ret;
	}
	AVDUser user;
	strcpy(user.userId, userId);
	strcpy(user.userName, userName);
	strcpy(user.userData, userData);
			ret = avd_room_join(room_, user, "", NULL);
	sets.add(this);
	return ret;
}
void Use_Room::api_leaveRoom() {
	sets.remove(room_);
	avd_room_leave(room_, AVD_OK);
	avd_room_uninit(room_);
	room_ = 0;
}
bool  Use_Room::api_isWorking() const {
	return avd_room_isWorking(room_);
}
Result Use_Room::api_enableStats(bool enable) {
	return avd_room_enableStats(room_, enable);
}
rtc::NetworkStats Use_Room::getRoomNetStats() const {
	AVDRoomStats roomstats;
    rtc::NetworkStats netstat;
	avd_room_getRoomStats(room_, roomstats);
    utility::NetStatsC2Cpp(roomstats.netstats,netstat);
    return netstat;
}
///
void Use_Room::s_onJoinResult(AVDHandle room, Result result) {
	Use_Room* item = sets.getItem(room);
	if (item) {	item->onJoinResult(result);	}
}
void Use_Room::s_onLeaveIndication(AVDHandle room, Result reason, const char* fromId) {
	Use_Room* item = sets.getItem(room);
	if (item) { item->onLeaveIndication(reason, fromId); }
}
void Use_Room::s_onPublicData(AVDHandle room, const char* data, uint32 len, const char* fromId) {
	Use_Room* item = sets.getItem(room);
	if (item) { item->onPublicData(data, len, fromId); }
}
void Use_Room::s_onPrivateData(AVDHandle room, const char* data, uint32 len, const char* fromId) {
	Use_Room* item = sets.getItem(room);
	if (item) { item->onPrivateData(data, len, fromId); }
}
void Use_Room::s_onAppDataNotify(AVDHandle room, const char* key, const char* value) {
	Use_Room* item = sets.getItem(room);
	if (item) { item->onAppDataNotify(key, value); }
}
void Use_Room::s_onRoomStatusNotify(AVDHandle room, AVDRoomStatus status){
	Use_Room* item = sets.getItem(room);
	if (item) { item->onRoomStatusNotify(status); }
}
void Use_Room::s_onConnectionStatus(AVDHandle room, AVDConnectionStatus status) {
	Use_Room* item = sets.getItem(room);
	if (item) { item->onConnectionStatus(status); }
}
