// ChatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ChatDlg.h"
#include "TalkDlg.h"
#include "ClientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog

CString file_lo1 ;

CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChatDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChatDlg)
	m_Send = _T("");
	//}}AFX_DATA_INIT
	
	IsWndCreat = false;
	char buffer[_MAX_PATH];
	if( getcwd( buffer, _MAX_PATH ) != NULL )
	{
		file_lo1 = buffer ;
		file_lo1 += "\\MSNpic\\";
	}

}

void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChatDlg)
	DDX_Control(pDX, IDC_REDIT_CHAT, m_ChatEdit);
	DDX_Control(pDX, IDC_LIST_USER, m_UserList);
	DDX_Text(pDX, IDC_RICHEDIT_SEND, m_Send);
	DDX_Control(pDX, IDC_RICH_SEND , m_ctlRichText);
	DDX_Control(pDX, IDC_RICH_CHAT , m_ctlRichTextView);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
	//{{AFX_MSG_MAP(CChatDlg)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_LEAVE, OnButtonLeave)
	ON_LBN_DBLCLK(IDC_LIST_USER, OnDblclkListUser)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers

int CChatDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	//AfxMessageBox("Chat");
	//this->SetWindowText(m_Param.GetTail());
	AfxInitRichEdit();

	CString str;
	//str = m_pParent->m_User + (CString)" at " + m_Roomname;
	//this->SetWindowText(str);
	this->SetWindowText(m_Roomname);
	return 0;
}

void CChatDlg::SetParent(CClientDlg *pParent)
{
	//m_pParent = new CClientDlg();
	m_pParent = pParent;
}

void CChatDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	//m_pParent->SetWindowText("Testing");
	//m_pParent->m_UserList.AddString("garlic");
	UpdateData(TRUE);
	ShowMsgTo(m_Send);
	//m_Send = "<FORWARD><CHAT>" + m_Roomname + "</CHAT><MSG>" + m_Send + "</MSG></FORWARD>";
	m_Send = "<FORWARD><CHAT>" + m_Roomname + "</CHAT><MSG>" + m_Send + "</MSG><MAN>"
		+ m_pParent->m_User + "</MAN></FORWARD>";
	m_pParent->OnSend(m_Send);
	m_Send = "";
	UpdateData(FALSE);
}

void CChatDlg::OnButtonLeave() 
{
	// TODO: Add your control notification handler code here
	CString str;
	POSITION pos;
	str = "<LEAVECHATROOM>" + m_Roomname + "</LEAVECHATROOM>";
	m_pParent->OnSend(str);
	pos = m_pParent->m_RoomList.Find(this);
	if(pos != NULL) m_pParent->m_RoomList.RemoveAt(pos);
	DestroyWindow();
}

void CChatDlg::ShowMsgFrom(CString strMan, CString strMsg)
{
	CString str;
	CHARFORMAT cf;
	//m_TalkEdit.GetDefaultCharFormat(cf);
	m_ChatEdit.GetSelectionCharFormat(cf);
	cf.dwMask = CFM_COLOR;
	cf.dwEffects = 0;
	cf.crTextColor = RGB(0, 0, 192);
	//m_TalkEdit.SetDefaultCharFormat(cf);
	m_ChatEdit.SetSelectionCharFormat(cf);
	str = strMan + (CString) " ปก:" + '\n';
	//str = (CString)"From " + m_Talkto + " :" + '\n';
	//m_TalkList.AddString(str);
	m_ChatEdit.SetSel(-1, 0);
	m_ChatEdit.ReplaceSel(str);
	m_ChatEdit.GetSelectionCharFormat(cf);
	cf.dwMask = CFM_COLOR;
	cf.dwEffects = 0;
	cf.crTextColor = RGB(0, 0, 192);
	//m_TalkEdit.SetDefaultCharFormat(cf);
	m_ChatEdit.SetSelectionCharFormat(cf);
	str =  (CString)"    " + strMsg + '\n';
	m_ChatEdit.SetSel(-1, 0);
	m_ChatEdit.ReplaceSel(str);
	str = '\n';
	m_ChatEdit.SetSel(-1, 0);
	m_ChatEdit.ReplaceSel(str);
}

void CChatDlg::ShowMsgTo(CString strMsg)
{
	CString str;
	CHARFORMAT cf;
	//m_TalkEdit.GetDefaultCharFormat(cf);
	m_ChatEdit.GetSelectionCharFormat(cf);
	cf.dwMask = CFM_COLOR;
	cf.dwEffects = 0;
	cf.crTextColor = RGB(0, 0, 0);
	//m_TalkEdit.SetDefaultCharFormat(cf);
	m_ChatEdit.SetSelectionCharFormat(cf);
	str = m_pParent->m_User + (CString)" ปก:" + '\n';
	//str = (CString)"To " + m_Talkto + " :" + '\n';
	m_ChatEdit.SetSel(-1, 0);
	m_ChatEdit.ReplaceSel(str);
	str = (CString)"    " + strMsg + '\n';
	m_ChatEdit.SetSel(-1, 0);
	m_ChatEdit.ReplaceSel(str);
	str = '\n';
	m_ChatEdit.SetSel(-1, 0);
	m_ChatEdit.ReplaceSel(str);
}

void CChatDlg::OnDblclkListUser() 
{
	// TODO: Add your control notification handler code here
	//m_pParent->OnDblclkListUser();
	POSITION pos;
	BOOL TkFlag = FALSE;
	CTalkDlg* pTkDlg;
	CString str;
	int iIdx = m_UserList.GetCurSel();
	m_UserList.GetText(iIdx, str);
	for(pos = m_pParent->m_TalkList.GetHeadPosition(); pos != NULL;) {
		pTkDlg = (CTalkDlg*)m_pParent->m_TalkList.GetNext(pos);
		if(!str.Compare(pTkDlg->m_Talkto)) {
			TkFlag = TRUE;
			pTkDlg->ShowWindow(SW_SHOW);
			//pTkDlg->ShowMsg(strp0, strp1);
		}
	}
	if(!TkFlag) {
		pTkDlg = new CTalkDlg(this);
		//pTkDlg->m_Param.AddTail(str);
		pTkDlg->m_Talkto = str;
		pTkDlg->Create(IDD_TALK, this);
		pTkDlg->ShowWindow(SW_SHOW);
		pTkDlg->SetParent(m_pParent);
		//pTkDlg->ShowMsg(strp0, strp1);
		m_pParent->m_TalkList.AddTail(pTkDlg);
	}	
}

BOOL CChatDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message == WM_KEYDOWN)
	{
		switch(pMsg->wParam)
		{
		case VK_RETURN:
			OnButtonSend();
			break;
		//case VK_ESCAPE:
		//	this->ShowWindow(SW_HIDE);
		//	break;
		default:
			break;
		}
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CChatDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//AfxInitRichEdit();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//======================================================================
BOOL CTalkDlg::InsertPic(LPCTSTR lpszFileName)
{
//***************************************************************

	CreateFromFile( file_lo + lpszFileName );

	REOBJECT reobject;
	ZeroMemory(&reobject, sizeof(REOBJECT));
	reobject.cbStruct = sizeof(REOBJECT);

	CLSID clsid;
	SCODE sc = m_lpObject->GetUserClassID(&clsid);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	reobject.clsid = clsid;
	reobject.cp = REO_CP_SELECTION;
	reobject.dvaspect = DVASPECT_CONTENT;
	reobject.dwFlags = REO_SELECTED | REO_BELOWBASELINE;
	reobject.dwUser = 0;
	reobject.poleobj = m_lpObject;

	ASSERT(m_lpClientSite != NULL);
	reobject.polesite = m_lpClientSite;

	ASSERT(m_lpStorage != NULL);
	reobject.pstg = m_lpStorage;

	SIZEL sizel;
	sizel.cx = sizel.cy = 0;
	reobject.sizel = sizel;


	HWND hWndRT = m_ctlRichText.m_hWnd;
	m_ctlRichText.SetSel( -1 , 0 );

	m_pRichEditOle->InsertObject(&reobject);

	::SendMessage(hWndRT, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
	m_ctlRichText.SetFocus( );

	if (m_lpObject)
	{
		m_lpObject->Release();
		m_lpObject = NULL;
	}
	if (m_lpStorage)
	{
		m_lpStorage->Release();
		m_lpStorage = NULL;
	}
	if (m_lpClientSite)
	{
		m_lpClientSite->Release();
		m_lpClientSite = NULL;
	}
	
	m_ctlRichText.Pic_list.AddTail( CString (lpszFileName) );
//***************************************************************	
	return true;
}

BOOL CTalkDlg::CreateFromFile(LPCTSTR lpszFileName)
{
	USES_CONVERSION;

	ASSERT_VALID(this);
	ASSERT(m_lpObject == NULL);     // one time only

	ASSERT(m_lpStorage == NULL);
	ASSERT(m_lpClientSite == NULL);
	LPLOCKBYTES lpLockBytes = NULL;
	CLSID clsid = CLSID_NULL;
	OLERENDER render = OLERENDER_DRAW;
	CLIPFORMAT cfFormat = 0;
	LPFORMATETC lpFormatEtc = NULL;

	SCODE sc;

	sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
	if (sc != S_OK)
		AfxThrowOleException(sc);
	ASSERT(lpLockBytes != NULL);

	sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
		STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
	if (sc != S_OK)
	{
		VERIFY(lpLockBytes->Release() == 0);
		lpLockBytes = NULL;
		AfxThrowOleException(sc);
	}
	ASSERT(m_lpStorage != NULL);

	// fill in FORMATETC struct
	FORMATETC formatEtc;
	lpFormatEtc = &formatEtc;
	lpFormatEtc->cfFormat = cfFormat;
	lpFormatEtc->ptd = NULL;
	lpFormatEtc->dwAspect = DVASPECT_CONTENT;
	lpFormatEtc->lindex = -1;
	lpFormatEtc->tymed = TYMED_NULL;

	// attempt to create the object
	m_pRichEditOle->GetClientSite(&m_lpClientSite);

	sc = ::OleCreateFromFile(clsid, T2COLE(lpszFileName),
		IID_IUnknown, OLERENDER_DRAW, lpFormatEtc, m_lpClientSite, m_lpStorage,
		(void**)&m_lpObject);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	// m_lpObject is currently an IUnknown, convert to IOleObject
	if (m_lpObject != NULL)
	{
		LPUNKNOWN lpUnk = m_lpObject;
		lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject);
		lpUnk->Release();
		if (m_lpObject == NULL)
			AfxThrowOleException(E_OUTOFMEMORY);
	}

	// all items are "contained" -- this makes our reference to this object
	//  weak -- which is needed for links to embedding silent update.
	OleSetContainedObject(m_lpObject, TRUE);

	ASSERT_VALID(this);
	return TRUE;
}


BOOL CTalkDlg::InsertPic_msg(LPCTSTR lpszFileName)
{
//***************************************************************
	CreateFromFile_msg( file_lo + lpszFileName );

	REOBJECT reobject;
	ZeroMemory(&reobject, sizeof(REOBJECT));
	reobject.cbStruct = sizeof(REOBJECT);

	CLSID clsid;
	SCODE sc = m_lpObject_msg->GetUserClassID(&clsid);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	reobject.clsid = clsid;
	reobject.cp = REO_CP_SELECTION;
	reobject.dvaspect = DVASPECT_CONTENT;
	reobject.dwFlags = REO_SELECTED | REO_BELOWBASELINE;
	reobject.dwUser = 0;
	reobject.poleobj = m_lpObject_msg;

	ASSERT(m_lpClientSite_msg != NULL);
	reobject.polesite = m_lpClientSite_msg;

	ASSERT(m_lpStorage_msg != NULL);
	reobject.pstg = m_lpStorage_msg;

	SIZEL sizel;
	sizel.cx = sizel.cy = 0;
	reobject.sizel = sizel;


	HWND hWndRT = m_ctlRichTextView.m_hWnd;

	m_pRichEditOle_msg->InsertObject(&reobject);

	::SendMessage(hWndRT, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);

	if (m_lpObject_msg)
	{
		m_lpObject_msg->Release();
		m_lpObject_msg = NULL;
	}
	if (m_lpStorage_msg)
	{
		m_lpStorage_msg->Release();
		m_lpStorage_msg = NULL;
	}
	if (m_lpClientSite_msg)
	{
		m_lpClientSite_msg->Release();
		m_lpClientSite_msg = NULL;
	}
	
//***************************************************************	
	return true;
}

BOOL CTalkDlg::CreateFromFile_msg(LPCTSTR lpszFileName)
{
	USES_CONVERSION;

	ASSERT_VALID(this);
	ASSERT(m_lpObject_msg == NULL);     // one time only

	ASSERT(m_lpStorage_msg == NULL);
	ASSERT(m_lpClientSite_msg == NULL);
	LPLOCKBYTES lpLockBytes = NULL;
	CLSID clsid = CLSID_NULL;
	OLERENDER render = OLERENDER_DRAW;
	CLIPFORMAT cfFormat = 0;
	LPFORMATETC lpFormatEtc = NULL;

	SCODE sc;

	sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
	if (sc != S_OK)
		AfxThrowOleException(sc);
	ASSERT(lpLockBytes != NULL);

	sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
		STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage_msg);
	if (sc != S_OK)
	{
		VERIFY(lpLockBytes->Release() == 0);
		lpLockBytes = NULL;
		AfxThrowOleException(sc);
	}
	ASSERT(m_lpStorage_msg != NULL);

	// fill in FORMATETC struct
	FORMATETC formatEtc;
	lpFormatEtc = &formatEtc;
	lpFormatEtc->cfFormat = cfFormat;
	lpFormatEtc->ptd = NULL;
	lpFormatEtc->dwAspect = DVASPECT_CONTENT;
	lpFormatEtc->lindex = -1;
	lpFormatEtc->tymed = TYMED_NULL;

	// attempt to create the object
	m_pRichEditOle_msg->GetClientSite(&m_lpClientSite_msg);

	sc = ::OleCreateFromFile(clsid, T2COLE(lpszFileName),
		IID_IUnknown, OLERENDER_DRAW, lpFormatEtc, m_lpClientSite_msg, m_lpStorage_msg,
		(void**)&m_lpObject_msg);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	// m_lpObject is currently an IUnknown, convert to IOleObject
	if (m_lpObject_msg != NULL)
	{
		LPUNKNOWN lpUnk = m_lpObject_msg;
		lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject_msg);
		lpUnk->Release();
		if (m_lpObject_msg == NULL)
			AfxThrowOleException(E_OUTOFMEMORY);
	}

	// all items are "contained" -- this makes our reference to this object
	//  weak -- which is needed for links to embedding silent update.
	OleSetContainedObject(m_lpObject_msg, TRUE);

	ASSERT_VALID(this);
	return TRUE;
}

void CTalkDlg::ShowRichMSG()
{

	CString temp , temp_pic , temp_new , temp_command;
	CString replsym  = "OBJET200410OBJECT";
	CString startcommand = "<OBJECT>";
	CString endcommand = "</OBJECT>";
	long position = 0 , end_posi = 0 ;
	int i = 0 , number = 0;

	temp = m_ctlRichText.GetMRTF();
	
	m_ctlRichTextView.SetMRTF( temp );

	do
	{
		number = m_ctlRichTextView.InMSGPiclist.GetCount();
		if( m_ctlRichTextView.InMSGPiclist.IsEmpty( ) )	
			break;
		temp_pic = m_ctlRichTextView.InMSGPiclist.GetHead();
		m_ctlRichTextView.InMSGPiclist.RemoveHead( );
		temp_command = replsym + startcommand + temp_pic + endcommand ;

		temp_new = m_ctlRichTextView.GetTEXT();
		position = temp_new.Find( temp_command );//, end_posi);
		end_posi = position + temp_command.GetLength() - 1 ;

		m_ctlRichTextView.SetSel( position , temp_command.GetLength() );
		i = 0;
		do{
			if( m_ctlRichTextView.GetSelText().GetLength() >  temp_command.GetLength() )
				m_ctlRichTextView.SetSel( position , temp_command.GetLength() - i );
			else if(m_ctlRichTextView.GetSelText().GetLength() <  temp_command.GetLength())
				m_ctlRichTextView.SetSel( position , temp_command.GetLength() + i );
			else 
				break;
			i++;
		}while(1);

		InsertPic_msg( temp_pic );
	}while(1);

}

void CTalkDlg::AddRichMSG( CString pre_temp , CString temp )
{
	CString  temp_pic , temp_new , temp_command;
	CString replsym  = "OBJET200410OBJECT";
	CString startcommand = "<OBJECT>";
	CString endcommand = "</OBJECT>";
	long position = 0 , end_posi = 0 ;
	int i = 0 , number = 0;

	m_ctlRichTextView.HideSelection( true , false );
	m_ctlRichTextView.AddMRTF(  pre_temp , temp );

	do
	{
		number = m_ctlRichTextView.InMSGPiclist.GetCount();
		if( m_ctlRichTextView.InMSGPiclist.IsEmpty( ) )	
			break;
		temp_pic = m_ctlRichTextView.InMSGPiclist.GetHead();
		m_ctlRichTextView.InMSGPiclist.RemoveHead( );
		temp_command = replsym + startcommand + temp_pic + endcommand ;

		temp_new = m_ctlRichTextView.GetTEXT();
		position = temp_new.Find( temp_command );//, end_posi);
		end_posi = position + temp_command.GetLength() - 1 ;

		m_ctlRichTextView.SetSel( position , temp_command.GetLength() );
		i = 0;
		do{
			if( m_ctlRichTextView.GetSelText().GetLength() >  temp_command.GetLength() )
				m_ctlRichTextView.SetSel( position , temp_command.GetLength() - i );
			else if(m_ctlRichTextView.GetSelText().GetLength() <  temp_command.GetLength())
				m_ctlRichTextView.SetSel( position , temp_command.GetLength() + i );
			else 
				break;
			i++;
		}while(1);

		InsertPic_msg( temp_pic );
	}while(1);
	m_ctlRichTextView.HideSelection( false , false );

}

void CTalkDlg::FacePicSelect(int nFace)
{

	switch( (nFace + 1) )
	{
		case 1:	
			InsertPic( "49_49.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 2:
			InsertPic( "48_48.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 3:
			InsertPic( "what_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 4:
			InsertPic( "tongue_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 5:
			InsertPic( "teeth_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 6:
			InsertPic( "shades_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 7:
			InsertPic( "sad_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 8:
			InsertPic( "regular_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 9:
			InsertPic( "omg_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 10:
			InsertPic( "50_50.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 11:
			InsertPic( "rose.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 12:
			InsertPic( "star.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 13:
			InsertPic( "heart.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 14:
			InsertPic( "thumbs_up.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 15:
			InsertPic( "kiss.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
	}

}

