
#include "nativewindow.h"
#if defined(RT_WIN32)
#include <tchar.h>
#include <windows.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>
#include <ddraw.h>

#elif defined(RT_LINUX) && !defined(RT_ANDROID)

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <iostream>
#include <sys/time.h>

#endif

using namespace std;
#if defined(RT_WIN32)
static bool g_isRegistRenderWndClasss = false;
const LPCTSTR avd_videoRenderClass = TEXT("AVD_VideoRenderClass");

LRESULT CALLBACK TeeAvdWinProc( HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_DESTROY:
        break;
        case WM_COMMAND:
        break;
    }
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
void RegistRenderWndClass() {
	if (!g_isRegistRenderWndClasss) {
		HINSTANCE hinst = GetModuleHandle(0);
		WNDCLASSEX wcx;
		wcx.hInstance = hinst;
		wcx.lpszClassName = avd_videoRenderClass;
		wcx.lpfnWndProc = (WNDPROC)TeeAvdWinProc;
		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;
	}
}

int TeeAvdCreateWindow(HWND &hwndMain, int winNum, int width, int height, HWND parent)
{
	RegistRenderWndClass();

	// Create the main window.
	HWND hwnd = CreateWindowEx(
		0, // no extended styles
		avd_videoRenderClass, // class name
		TEXT("AVD_VideoRenderClass 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
		GetModuleHandle(0), // instance handle
		NULL); // no window creation data

	if (!hwnd)
        return -1;

    // Show the window using the flag specified by the program
    // that started the application, and send the application
    // a WM_PAINT message.
	ShowWindow(hwnd, SW_SHOWDEFAULT);
	UpdateWindow(hwnd);
	hwndMain = hwnd;
    return 0;
}

#elif defined(RT_LINUX) && !defined(RT_ANDROID)

int TeeAvdCreateWindow(Window *outWindow, Display **outDisplay, int winNum, int width, int height, Window parent)
{
    int screen, xpos = 10, ypos = 10;
    XEvent evnt;
    XSetWindowAttributes xswa; // window attribute struct
    XVisualInfo vinfo; // screen visual info struct
    unsigned long mask; // attribute mask

    // get connection handle to xserver
    Display* _display = XOpenDisplay( NULL );

    // get screen number
    screen = DefaultScreen(_display);

    // put desired visual info for the screen in vinfo
    if( XMatchVisualInfo(_display, screen, 24, TrueColor, &vinfo) != 0 )
    {
        qDebug()<<"Screen visual info match!";
        //printf( "Screen visual info match!\n" );
    }

    // set window attributes
    xswa.colormap = XCreateColormap(_display, (0 == parent ? DefaultRootWindow(_display) : parent), vinfo.visual, AllocNone);
    xswa.event_mask = StructureNotifyMask | ExposureMask;
    xswa.background_pixel = 0;
    xswa.border_pixel = 0;

    // value mask for attributes
    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;

    switch( winNum )
    {
        case 0:
        xpos = 0;
        ypos = 0;
        break;
        case 1:
        xpos = 300;
        ypos = 200;
        break;
        default:
        break;
    }

    // create a subwindow for parent (defroot)
    Window _window = XCreateWindow(_display, (0 == parent ? DefaultRootWindow(_display) : parent),
            xpos, ypos,
            width,
            height,
            0, vinfo.depth,
            InputOutput,
            vinfo.visual,
            mask, &xswa);
    qDebug()<<"_display:"<<_display<<"\n _window:"<<_window<<"\n";
    // Set window name
    if( winNum == 0 )
    {
        XStoreName(_display, _window, "VE MM Local Window");
        XSetIconName(_display, _window, "VE MM Local Window");
    }
    else if( winNum == 1 )
    {
        XStoreName(_display, _window, "VE MM Remote Window");
        XSetIconName(_display, _window, "VE MM Remote Window");
    }

    // make x report events for mask
    XSelectInput(_display, _window, StructureNotifyMask);

    // map the window to the display
    XMapWindow(_display, _window);

    // wait for map event
    do
    {
        XNextEvent(_display, &evnt);
    }
    while (evnt.type != MapNotify || evnt.xmap.event != _window);

    *outWindow = _window;
    *outDisplay = _display;

    return 0;
}
#endif  // LINUX

//void RunVideoRenderTests(void* window, VideoRenderType windowType) {
//	int myId = 12345;
//
//	// Create the render module
//	printf("Create render module\n");
//	VideoRender* renderModule = NULL;
//	renderModule = VideoRender::CreateVideoRender(myId,
//		window,
//		false,
//		windowType);
//	assert(renderModule != NULL);
//
//
//	delete renderModule;
//	renderModule = NULL;
//
//	printf("VideoRender unit tests passed.\n");
//}
//
//// Note: The Mac main is implemented in testApi_mac.mm.
//#if defined(RT_WIN32)
//int _tmain(int argc, _TCHAR* argv[])
//#elif defined(RT_LINUX) && !defined(RT_ANDROID)
//int main(int argc, char* argv[])
//#endif
//#if !defined(RT_MACOS) && !defined(RT_ANDROID)
//{
//    // Create a window for testing.
//    void* window = NULL;
//#if defined (RT_WIN32)
//    HWND testHwnd;
//    TeeAvdCreateWindow(testHwnd, 0, 352, 288);
//    window = (void*)testHwnd;
//    VideoRenderType windowType = kRenderWindows;
//#elif defined(RT_LINUX)
//    Window testWindow;
//    Display* display;
//    TeeAvdCreateWindow(&testWindow, &display, 0, 352, 288);
//    VideoRenderType windowType = kRenderX11;
//    window = (void*)testWindow;
//#endif // RT_LINUX
//
//    RunVideoRenderTests(window, windowType);
//    return 0;
//}
//#endif  // !RT_MACOS
