// MessageDiag.cpp : implementation file
//

#include "stdafx.h"
#ifdef DENETWINSERVER
 #include "..\deNetWinServer\deNetWinServer.h"
#else
 #include "..\deNetWinClient\deNetWinClient.h"
#endif
#include "MessageDiag.h"

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

DWORD OnNetLog( const char * pStr )
{
	if( CMessageDiag::GetWin() )
		CMessageDiag::GetWin()->AddMessage( pStr );
	return 0;
};

/////////////////////////////////////////////////////////////////////////////
// CMessageDiag

IMPLEMENT_DYNCREATE(CMessageDiag, CFormView)

// static decl
//--------------------------------------------
CMessageDiag * CMessageDiag::m_Singleton = NULL;

CMessageDiag::CMessageDiag()
	: CFormView(CMessageDiag::IDD)
{
	m_Singleton = this;
	
	//{{AFX_DATA_INIT(CMessageDiag)
	//}}AFX_DATA_INIT
}

CMessageDiag::~CMessageDiag()
{
	m_Singleton = NULL;
}

void CMessageDiag::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMessageDiag)
	DDX_Control(pDX, IDC_LSTMESSAGES, m_lstMessages);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMessageDiag, CFormView)
	//{{AFX_MSG_MAP(CMessageDiag)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMessageDiag diagnostics

#ifdef _DEBUG
void CMessageDiag::AssertValid() const
{
	CFormView::AssertValid();
}

void CMessageDiag::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMessageDiag message handlers

void CMessageDiag::OnSize(UINT nType, int cx, int cy) 
{
	// resize form
	CFormView::OnSize(nType, cx, cy);

	if( m_lstMessages.m_hWnd )
	{
		// resize controls
		m_lstMessages.SetColumnWidth( 0, cx );
		m_lstMessages.MoveWindow( 0, 0, cx, cy, TRUE );
		m_lstMessages.UpdateWindow();
	}
}

void CMessageDiag::OnInitialUpdate() 
{
	// form update
	CFormView::OnInitialUpdate();

	int			iRet = 0;
	LVCOLUMN	col;

	if( m_imgMessages.m_hImageList == NULL )
	{
		// load imglist
		if( m_imgMessages.Create( IDB_IMGLST_MESSAGE, 16, 0, RGB( 255,0,255 ) ) == FALSE )
			return;
	}

	// assign to control
	if( m_imgMessages.m_hImageList != NULL )
	{
		m_lstMessages.SetImageList( &m_imgMessages, LVSIL_SMALL );
	}

	// setup control
	m_lstMessages.SetExtendedStyle(LVS_EX_FULLROWSELECT /*| LVS_EX_TRACKSELECT */ );
	m_lstMessages.EnableToolTips( FALSE );

	// clear control
	m_lstMessages.DeleteColumn( 0 );

	// clear it
	memset( &col, 0, sizeof(LVCOLUMN) );
	
	// setup column
	col.pszText		= "Message Log";
	col.mask		= LVCF_TEXT;
	col.iSubItem	= 0;

	// add column
	iRet = m_lstMessages.InsertColumn( 0, &col );
}

void CMessageDiag::ClearMessages( void )
{
	// clear control
	m_lstMessages.DeleteAllItems();
	AddMessage( ">> Message Log Started <<" );
}

bool CMessageDiag::AddMessage( const char * pText )
{
	// setup color

	// setup item struct
	int			iImg = -1;
	int			iPos = -1;
	LVITEM		item;
	COLORREF	color;
	COLORREF	bgColor;
	memset( &item, 0,sizeof(LVITEM) );

	CString strText = pText;

	// clean up string
	strText.Replace( "\n", "" );

	color	= RGB( 0, 0, 0 );
	bgColor	= RGB( 255, 255, 255 );
	
	// get string type
	iPos = strText.Find( "**", 0 );
	if( iPos >= 0 )
	{
		// app created error
		color = RGB( 255, 200, 40 );
		iImg = 1;
	}

	iPos = strText.Find( ">>", 0 );
	if( iPos >= 0 )
	{
		// general log/update
		color = RGB( 0, 0, 0 );
		iImg = 0;
	}

	iPos = strText.Find( "!!", 0 );
	if( iPos >= 0 )
	{
		// eNetError thrown error
		color = RGB( 255, 0, 0 );
		iImg = 2;
	}

	iPos = strText.Find( "--", 0 );
	if( iPos >= 0 )
	{
		// low-level update
		color = RGB( 100, 100, 100 );
		iImg = -1;

#ifndef DN_SHOW_ALL_MESSAGES
		// skip low-level
		return true;
#endif
	}

	if( iImg >= 0 && m_imgMessages.m_hImageList != NULL )
		item.mask	= LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
	else
		item.mask	= LVIF_TEXT | LVIF_STATE | LVIF_INDENT;

	item.iIndent	= 1;
	item.iImage		= iImg;
	item.iItem		= m_lstMessages.GetItemCount();
	item.iSubItem	= 0;
	item.state		= LVIS_SELECTED | LVIS_FOCUSED;
	item.stateMask	= (UINT)-1;
	item.pszText	= strText.GetBuffer( strText.GetLength() );

	m_lstMessages.SetRedraw( FALSE );

	bool	bOK  = false;
	int		iRet = m_lstMessages.InsertItem( &item );
	
	if( iRet >= 0 )
	{
		// set color
		m_lstMessages.SetItemText( iRet, 0, item.pszText, color, bgColor );

		// scroll down
		m_lstMessages.SetCurSel( iRet );
		m_lstMessages.EnsureVisible( iRet, FALSE );
		
		bOK = true;
	}
	else
		bOK = false;

	m_lstMessages.SetRedraw( TRUE );
	m_lstMessages.UpdateWindow();

	return bOK;
}
