#include "videoview2.h"
#include "api/defines.h"
#include <QtGui/qpainter.h>
#include <QtGui/qbitmap.h>
#include "nativewindow.h"

///
CVideoView2::CVideoView2(QWidget* parent,int id)
    : QWidget(parent), m_videoRendererView(0),m_videoRenderer(0),m_id(id) {
	this->setAttribute(Qt::WA_PaintOnScreen, true);
	this->setFocusPolicy(Qt::StrongFocus);
	this->show();
}
CVideoView2::~CVideoView2() {
    if (m_videoRendererView) {
        m_videoRendererView->StopRender(0);
        m_videoRendererView->DeleteVideoRender(0);
        rtc::IVideoRenderView::DestroyVideoRenderView(m_videoRendererView);
        m_videoRendererView = NULL;
    }
}

void CVideoView2::updateViewSize(int width, int height) {
    resize(width, height);
    if (0 == m_videoRenderer) {
        qDebug()<<"updateViewSize width:"<<width<<",height:"<<height<<",this:"<<this;
        Window testWindow;
        Display* display;
        TeeAvdCreateWindow(&testWindow, &display, 0, size().width(), size().height(), (Window)winId());
        m_videoRendererView = rtc::IVideoRenderView::CreateVideoRenderView(m_id+1, (void*)testWindow, false, false);
        m_videoRenderer = m_videoRendererView->CreateVideoRender(m_id, 0, rtc::IVideoRenderView::Scale_Aspect_Fit, 0.0f, 0.0f, 1.0f, 1.0f);
        m_videoRendererView->StartRender(m_id);
        m_videoRendererView->SetScalingType(m_id,rtc::IVideoRenderView::Scale_Fill);
        //avd_render_create(m_videoRenderer, m_window, false, false);
        //avd_render_setScalingType(m_videoRenderer, Scale_Fill);
    }
}
bool CVideoView2::setScalingType(rtc::IVideoRenderView::ScalingType type) {
    return m_videoRendererView->SetScalingType(m_id,type);

}

void CVideoView2::paintEvent(QPaintEvent *event) {
	QPainter dc(this);
	QFont font(dc.font());
	font.setPixelSize(15);
	font.setFamily(QString("Microsoft YaHei"));
	QPen pen(QColor(255,0,0));
	dc.setPen(pen);
	dc.setFont(font);
	dc.drawText(10, 0, rect().width()-10, 30, Qt::AlignVCenter || Qt::AlignRight, _user_name);
	QRect borderRect(0, 0, rect().width() - 1, rect().height() - 1);
}
void CVideoView2::resizeEvent(QResizeEvent* event) {
    QWidget::resizeEvent(event);
	if (m_videoRenderer) {
        m_videoRendererView->UpdateWindowSize();
	}
}
