#include "use_video.h"
#include "api/avdengine.h"
#include "api/musermanager.h"
#include "videoview.h"
#include "log2ui.h"
#include <QtGui/QVector3D>

using namespace rtc;

MediaStats g_emptyMediaStats;
///
Use_Video::Use_Video(QWidget *parent) : Com_Video(parent), module_(NULL) {
}
Use_Video::~Use_Video() {
}
void Use_Video::onAttach(rtc::IRoom* room) {
	if (nullptr == room) {
		module_ = nullptr;
	}
	else {		module_ = IMVideo::getVideo(room);
		module_->setListener(this);

	}
	Com_Video::onAttach();
}
void Use_Video::onDetach() {
	if (module_) {
		module_->setListener(NULL);
		module_ = NULL;
	}

	Com_Video::onDetach();
}
void Use_Video::updateLocalCameras() {
	if (!isAttach()) {
		return;
	}
	Com_Video::updateLocalCameras();
	ICameraDevice* devices = module_->getCameraDevice();
	CamerasType items;
	devices->getLocalCameras(items);
	for (Camera var : items) {
		QListWidgetAddItem(ui.lst_video_localCameras, var.name.c_str(), var.id.c_str());
	}
}
void  Use_Video::api_switchIndexvideo(int index){
	if (!isAttach()) {
		return;
	}
	ICameraDevice* devices = module_->getCameraDevice();
	CamerasType items;
	devices->getLocalCameras(items);
	if ( index <= items.size())
		devices->setDefaultCamera(items[index].id);
}
void Use_Video::updateRemotePublishedCameras() {
	if (!isAttach()) {
		return;
	}
	Com_Video::updateRemotePublishedCameras();
	ICameraDevice* devices = module_->getCameraDevice();
	CamerasType items;
	module_->getPublishedCameras(items);
	for (Camera var : items) {
		if (api_isSelfDevice(var.id)) {
			continue;
		}
		QListWidgetAddItem(ui.lst_video_remotePublishedCameras, var.name.c_str(), var.id.c_str());
	}
}
void Use_Video::fillResolutions() {
	if (!isAttach()) {
		return;
	}
	Com_Video::fillResolutions();
    rtc::DeviceId selId = getSelectedDeviceId(ui.lst_video_localCameras);
	if (selId.empty()) {
		return;
	}
	CapabilitiesType caps = module_->getCameraDevice()->getCameraCapability(selId);
	for (CameraCapability item : caps) {
		QString sitem;
		sitem.sprintf("%dx%d %d", item.width, item.height, item.maxFPS);
		QVector3D qtdata(item.width, item.height, item.maxFPS);
		ui.cbo_video_resolutions->addItem(sitem, qtdata);
	}
}
///
bool Use_Video::api_engineSetOption(rtc::EngineOption option, void* value) {
	IAVDEngine::Instance()->setOption(option, value);
	return true;
}
bool Use_Video::api_isSelfDevice(const rtc::DeviceId& dId) const {
	return isAttach() ? module_->isSelfDevice(dId) : false;
}
Result Use_Video::api_attachRender(const rtc::DeviceId& dId, IVideoView* view) {

    return module_->attachRender(dId, view->getRender());
}
Result Use_Video::api_detachRender(const rtc::DeviceId& dId) {
	return module_->detachRender(dId);
}
Result Use_Video::api_detachRender_1(IVideoView* render){

    Result ret = module_->detachRender(render->getRender());
    //render->onDetach();
    return ret;
}
Result Use_Video::api_subscribe(const rtc::DeviceId& dId) {
	return module_->subscribe(dId);
}
Result Use_Video::api_unsubscribe(const rtc::DeviceId& dId) {
	return module_->unsubscribe(dId);
}
Result Use_Video::api_muteLocalCamera(const rtc::DeviceId& dId) {
	return module_->muteLocalCamera(dId);
}
Result Use_Video::api_unmuteLocalCamera(const rtc::DeviceId& dId) {
	return module_->unmuteLocalCamera(dId);
}

Result Use_Video::api_publishLocalCamera( rtc::Camera& cam) {
    StreamType type = stream_main;
    int w = ui.lineEdit_2->text().toInt();
    int h = ui.lineEdit_3->text().toInt();
    int fps = ui.lineEdit->text().toInt();
    if(w != 0 && h != 0 && fps != 0 ){
        CameraCapability acap(w,h,fps);
        VideoCodec codec = codec_h264;
        cam.publishedQualities.setStreamOptions(type, acap, codec);
    }
	return module_->publishLocalCamera(cam);
}
Result Use_Video::api_unpublishLocalCamera() {
	return module_->unpublishLocalCamera();
}

Result Use_Video::api_previewLocalCamera(const rtc::DeviceId& dId) {
	return module_->previewLocalCamera(dId, 
#ifdef __USE_VIDEOVIEW_
		localview_->getRender());
#else
		NULL);
#endif
}
Result Use_Video::api_unpreviewLocalCamera() {
	return module_->unpreviewLocalCamera();
}
Result Use_Video::api_setVideoBitrate(const rtc::DeviceId& dId, int min, int max) {
	return module_->setVideoBitrate(dId, min, max);
}
const MediaStats& Use_Video::getMediaStats() {
	if (!isAttach() || curLocalId_.empty()) {
        return g_emptyMediaStats;
	}
	return module_->getRoom()->getMediaStats(curLocalId_);
}
rtc::Camera Use_Video::api_getCamera(const rtc::DeviceId& dId) {
	return module_->getCameraDevice()->getCamera(dId);
}
Result Use_Video::api_changeCapability(const rtc::DeviceId& dId, const rtc::CameraCapability& cap){
	return AVD_OK;
}
