#include "IEInstance.h"
#include <tchar.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_SIZE:
		{
			// Resize the browser object to fit the window
			//ResizeBrowser(hwnd, LOWORD(lParam), HIWORD(lParam));
			CIEInstance *pIEInst = (CIEInstance *)GetWindowLong(hwnd, GWL_USERDATA);
			pIEInst->ResizeBrowser(LOWORD(lParam), HIWORD(lParam));
			return(0);
		}

	case WM_CREATE:
		{
			// Embed the browser object into our host window. We need do this only
			// once. Note that the browser object will start calling some of our
			// IOleInPlaceFrame and IOleClientSite functions as soon as we start
			// calling browser object functions in EmbedBrowserObject().
			//if (EmbedBrowserObject(hwnd)) return(-1);

			// Another window created with an embedded browser object
			//++WindowCount;

			// Success
			CIEInstance *pIEInst = new CIEInstance(hwnd);
			SetWindowLong(hwnd, GWL_USERDATA, (LONG)pIEInst);
			return(0);
		}

	case WM_DESTROY:
		{
			// Detach the browser object from this window, and free resources.
			//UnEmbedBrowserObject(hwnd);

			// One less window
			//--WindowCount;

			// If all the windows are now closed, quit this app
			//if (!WindowCount) PostQuitMessage(0);

			CIEInstance *pIEInst = (CIEInstance *)GetWindowLong(hwnd, GWL_USERDATA);
			delete pIEInst;
			return(TRUE);
		}
	}

	return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
	MSG			msg;

	// Initialize the OLE interface. We do this once-only.
	if (OleInitialize(NULL) == S_OK)
	{
		WNDCLASSEX		wc;

		// Register the class of our window to host the browser. 'WindowProc' is our message handler
		// and 'ClassName' is the class name. You can choose any class name you want.
		ZeroMemory(&wc, sizeof(WNDCLASSEX));
		wc.cbSize = sizeof(WNDCLASSEX);
		wc.hInstance = hInstance;
		wc.lpfnWndProc = WindowProc;
		wc.lpszClassName = _T("MyIE");
		RegisterClassEx(&wc);

		// Create a window. NOTE: We embed the browser object duing our WM_CREATE handling for
		// this window.
		//if ((msg.hwnd = CreateWindowEx(0, &ClassName[0], "An HTML string", WS_OVERLAPPEDWINDOW,
		//	CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
		//	HWND_DESKTOP, NULL, hInstance, 0)))
		//{
		//	// For this window, display a string in the BODY of a web page.
		//	DisplayHTMLStr(msg.hwnd, "<H2><CENTER>HTML string test</CENTER></H2><P><FONT COLOR=RED>This is a <U>HTML string</U> in memory.</FONT>");

		//	// Show the window.
		//	ShowWindow(msg.hwnd, nCmdShow);
		//	UpdateWindow(msg.hwnd);
		//}

		// Create another window with another browser object embedded in it.
		if ((msg.hwnd = CreateWindowEx(0, _T("MyIE"), _T("MyIE"), WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
			HWND_DESKTOP, NULL, hInstance, 0)))
		{
			// For this window, display a URL. This could also be a HTML file on disk such as "c:\\myfile.htm".
			//DisplayHTMLPage(msg.hwnd, "http://www.microsoft.com");
			CIEInstance *pIEInst = (CIEInstance *)GetWindowLong(msg.hwnd, GWL_USERDATA);
			pIEInst->DisplayHTMLPage(L"http://www.baidu.com");

			// Show the window.
			ShowWindow(msg.hwnd, nShowCmd);
			UpdateWindow(msg.hwnd);
		}

		// Do a message loop until WM_QUIT.
		while (GetMessage(&msg, 0, 0, 0) == 1)
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		// Free the OLE library.
		OleUninitialize();

		return(0);
	}

	MessageBox(0, _T("Can't open OLE!"), _T("ERROR"), MB_OK);
	return(-1);
}