#include "videoviewhost.h"
#include <QtGui/qpainter.h>
#include <QtGui/qbitmap.h>

#ifdef WIN32
#include "tchar.h"

static bool g_isRegistRenderWndClasss = false;
const LPCTSTR VideoRenderWndClass = TEXT("VideoRenderClass");

#else
#include <QtGui/QPaintEvent>
#include <QtGui/QResizeEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QFocusEvent>
#include <QtGui/QPaintEvent>
#endif

///
CVideoViewHost::CVideoViewHost(QWidget* parent)
    : QWinHost(parent), m_hwnd(NULL), _parent(parent) {
	this->setAttribute(Qt::WA_PaintOnScreen, true);
	this->setFocusPolicy(Qt::StrongFocus);
	this->show();
}
CVideoViewHost::~CVideoViewHost() {
}
void CVideoViewHost::updateViewSize(int width, int height) {
	resize(width, height);
	onCreateWnd(m_hwnd);
}
void CVideoViewHost::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);
}

HWND CVideoViewHost::createWindow(HWND parent, HINSTANCE instance) {
	RegistRenderWndClass();
	// Create the main window.
	HWND hwnd = CreateWindowEx(
		0, // no extended styles
		VideoRenderWndClass, // class name
		TEXT("VideoRenderTest Window"), // window name
		WS_CHILD | WS_CLIPSIBLINGS | WS_TABSTOP, // overlapped window
		CW_USEDEFAULT, // horizontal position
		0, // vertical position
		400, // width
		300, // height
		(HWND)parent, // no parent or owner window
		(HMENU)NULL, // class menu used
		instance, // instance handle
		NULL); // no window creation data
	//onCreateWnd(hwnd);
	m_hwnd = hwnd;
	return m_hwnd;
}
#if QT_VERSION >= 0x050000
#define QT_WA(unicode, ansi) unicode
#endif
LRESULT CALLBACK CVideoViewHost::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	QWidget *widget = QWidget::find((WId)GetParent(hWnd));
	CVideoViewHost *window = qobject_cast<CVideoViewHost*>(widget);

	if (window) switch (message) {
	case WM_SETFOCUS:
		//window->message("SetFocus for Win32 window!", 1000);
		window->focusInEvent(NULL);
		break;
	case WM_KILLFOCUS:
		window->focusOutEvent(NULL);
		//window->message("KillFocus for Win32 window!", 1000);
		break;
	case WM_MOUSEMOVE:
		//window->message("Moving the mouse, aren't we?", 200);
		break;
	case WM_LBUTTONDBLCLK:
		window->mouseDoubleClickEvent(NULL);
		break;
	case WM_LBUTTONDOWN:
		window->mousePressEvent(NULL);
		break;
	case WM_LBUTTONUP:
		window->mouseReleaseEvent(NULL);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
void CVideoViewHost::RegistRenderWndClass() {
	if (!g_isRegistRenderWndClasss) {
		HINSTANCE hinst = GetModuleHandle(0);
		WNDCLASSEX wcx;
		wcx.hInstance = hinst;
		wcx.lpszClassName = VideoRenderWndClass;
		wcx.lpfnWndProc = (WNDPROC)WndProc;
		wcx.style = CS_DBLCLKS;
		wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
		wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
		wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
		wcx.lpszMenuName = NULL;
		wcx.cbSize = sizeof(WNDCLASSEX);
		wcx.cbClsExtra = 0;
		wcx.cbWndExtra = 0;
		wcx.hbrBackground = GetSysColorBrush(COLOR_3DFACE);

		// Register our window class with the operating system.
		// If there is an error, exit program.
		if (!RegisterClassEx(&wcx)) {
			MessageBox(0, TEXT("Failed to register window class!"), TEXT("Error!"), MB_OK | MB_ICONERROR);
			return;
		}
		g_isRegistRenderWndClasss = true;
	}
}
void CVideoViewHost::mouseDoubleClickEvent(QMouseEvent* event){
	emit doubleClick();
}
void CVideoViewHost::mousePressEvent(QMouseEvent *event){
}
void CVideoViewHost::mouseReleaseEvent(QMouseEvent *event){
	emit click();
}
void CVideoViewHost::resizeEvent(QResizeEvent* event) {
	QWinHost::resizeEvent(event);
}
