// stdafx.cpp : source file that includes just the standard includes
//	GUI_VisualStudio7.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"
#include "uim.h"
#include "_aimclient.h"
#include "aim_event_definitions.h"


AIMClient* g_aim;


void AIMMain() {
	while (1) {
		try {
			uchar *data = new uchar[8192];
			long rec = 0;
			memset(data,0,8192);
			if (g_aim->Main(data,rec)) {
				ThreadData *d = new ThreadData(data,rec);
				delete [] data;
				CreateThread(0,0,(LPTHREAD_START_ROUTINE)AIMHandle,(LPVOID)d,0,0);

			}
			else break;

		}
		catch (...) {
			AfxMessageBox("Fatal error");
			break;
		}
	}
}

void AIMHandle(LPVOID d) {

	AIMEvent event = g_aim->Handle(((ThreadData*)d)->data,((ThreadData*)d)->recvd);
	switch (event.Type) {
	case UIM_RECV_MESSAGE_ONLINE: {
		AfxMessageBox(event.message.Message.c_str());
		break;
	}
	case UIM_RECV_MESSAGE_OFFLINE: {
		AfxMessageBox(event.message.Message.c_str());
		break;
	}
	case UIM_BUDDY_WENT_ONLINE: {
		theApp.m_pMainFrame->BuddyWentOnline(event.contact.ScreenName);
		break;
	}

	case UIM_BUDDY_WENT_OFFLINE: {
//		AfxMessageBox("OFFLINE");
//		AfxMessageBox(event.contact.ScreenName.c_str());

		break;
	}
	case UIM_LOGGED_IN: {
		theApp.m_pMainFrame->StopAnimation();
		theApp.m_pMainFrame->ResetAnimation();
		theApp.m_pMainFrame->SetWindowText("Logged in");
		AfxMessageBox("LOGGED IN");
		break;
	}
	case UIM_ERROR: 
		if (event.ErrorCode == 0xa) {
		//server ack for stored info, implies 
                            /* 0x000a - Error adding item (maybe the group ID#/buddy ID# is already taken?)
			       (maybe this screen name already exists in the give group?)(maybe the name is too long? or invalid?) 
			       I think should actually be 0x0010, but I haven't verified. */
			       
		
		
		}
		break;
	default :
		break;
	}	

	delete d;
}

VOID CALLBACK  AIMKeepAlive(
	HWND hwnd,        
	UINT uMsg,         
	unsigned int idEvent,  
	DWORD dwTime       //
	)
{

	g_aim->KeepAlive();
}


void SetText() {

	//theApp.m_pMainFrame->StartAnimation();

// 	POSITION x = theApp.m_pDocTemplate->GetFirstDocPosition();
// //	theApp.m_pDocTemplate->GetNextDoc(x);
	
// 	CDocument* doc = theApp.m_pDocTemplate->GetNextDoc(x);

// 	POSITION y = doc->GetFirstViewPosition();

// 	CEditView* my_edit = (CEditView*)doc->GetNextView(y);

// 	CEdit &edit = my_edit->GetEditCtrl();
// 	edit.ReplaceSel("NEW TEXT!");

	POSITION x= theApp.m_pHtmlTemplate->GetFirstDocPosition();

	CDocument* doc = theApp.m_pDocTemplate->GetNextDoc(x);
 
	POSITION y = doc->GetFirstViewPosition();

	CHtmlView* my_html = (CHtmlView*)doc->GetNextView(y);
	
	LPDISPATCH pDisp = my_html->GetHtmlDocument();

	ASSERT(pDisp);
	
	IWebBrowser2* pWeb;
	IHTMLDocument2* pDoc;
	IDispatch* docDisp;
	
	
	pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);

	
	VARIANT *param;
	HRESULT hr;
	CString data = "<html>This is an image <img src=\"C:\\image.gif\"></html>";
	bstr_t bsData ;
			
	SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
	bsData = (LPCTSTR)data;
	hr = SafeArrayAccessData(psa, (LPVOID*)&param);
	param->vt = VT_BSTR;
	param->bstrVal = (BSTR)bsData;
	
	hr = pDoc->write(psa);
	hr = pDoc->close();

}
	

