///////////////////////////////////////////////////////////////////////////////
//
// Implementation for List of Data
//
///////////////////////////////////////////////////////////////////////////////

#include <StdAfx.h>

#include "WAW_User.hpp"
#include "WAW_Main.hpp"
#include "WAW_List.hpp"
#include "WAW_Strg.hpp"
#include "ErO.h"

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

extern WAW_MainDialog * WAW_g_pclWatchWindow;
extern CList<WAW_hUserInfo,WAW_hUserInfo> WAW_g_clListOfUser;

#define WAW_M_bCheckInfo(_hInfo) (WAW_g_clListOfUser.Find((_hInfo)) != NULL)


WAW_DataList::WAW_DataList( CString _csUserModuleName ) : CList<WAW_Data*,WAW_Data*>()
{
	WAW_hUserInfo hInfo = WAW_fn_hFindInfo( _csUserModuleName );
	if( hInfo == NULL ) return;
	
	m_pri_hUserInfo = hInfo;
	//m_pub_fn_vGetAll();
}


WAW_DataList::WAW_DataList( WAW_hUserInfo _hInfo ) : CList<WAW_Data*,WAW_Data*>()
{
	if( !WAW_M_bCheckInfo( _hInfo ) ) return;
	
	m_pri_hUserInfo = _hInfo;
	m_pub_fn_vGetAll();
}


void WAW_DataList::m_pub_fn_vGetAll()
{
	RemoveAll();
	ERROR_PREPARE_M( WAW_g_C_csModuleNameForError,
									 "Construct a list of Data",
									 "WAW_DataList::WAW_DataList",
									 E_ERROR_GRAVITY_INSTABLE,
									 "The pointer to CTreeCtrl is NULL" );
	CTreeCtrl * pclTreeCtrl = WAW_g_pclWatchWindow->m_pub_fn_pclGetTreeCtrl();
	ERROR_ASSERT( pclTreeCtrl != NULL );
	if( pclTreeCtrl == NULL ) return;

	HTREEITEM hItem = pclTreeCtrl->GetNextItem( NULL, TVGN_ROOT );
	m_pri_fn_vFillList( pclTreeCtrl, hItem );
}


void WAW_DataList::m_pri_fn_vFillList( CTreeCtrl * _pclTree, HTREEITEM _hItem )
{
	while( _hItem != NULL )
		{
		WAW_Data * pclData = (WAW_Data*)_pclTree->GetItemData( _hItem );
		if( pclData->m_pri_pstUserInfo == m_pri_hUserInfo ) AddTail( pclData );
		m_pri_fn_vFillList( _pclTree, _pclTree->GetChildItem( _hItem ) );
		_hItem = _pclTree->GetNextItem( _hItem, TVGN_NEXT );
		}
}


void WAW_DataList::m_pub_fn_vGetData()
{
	m_pub_fn_vGetAll();
	m_pub_fn_vSelectData();
}


void WAW_DataList::m_pub_fn_vSelectData()
{
	POSITION pos = GetHeadPosition();
	POSITION posOld = NULL;
	while( pos != NULL )
		{
		posOld = pos;
		WAW_Data * pclData = GetNext( pos );
		if( !pclData->m_pri_bIsData ) RemoveAt( posOld );
		}
}


void WAW_DataList::m_pub_fn_vGetNode()
{
	m_pub_fn_vGetAll();
	m_pub_fn_vSelectNode();
}


void WAW_DataList::m_pub_fn_vSelectNode()
{
	POSITION pos = GetHeadPosition();
	POSITION posOld = NULL;
	while( pos != NULL )
		{
		posOld = pos;
		WAW_Data * pclData = GetNext( pos );
		if( pclData->m_pri_bIsData ) RemoveAt( posOld );
		}
}


void WAW_DataList::m_pub_fn_vGetWithName( CString _csName )
{
	m_pub_fn_vGetAll();
	m_pub_fn_vSelectWithName( _csName );
}


void WAW_DataList::m_pub_fn_vSelectWithName( CString _csName )
{
	POSITION pos = GetHeadPosition();
	POSITION posOld = NULL;
	while( pos != NULL )
		{
		posOld = pos;
		WAW_Data * pclData = GetNext( pos );
		if( pclData->m_pri_csDataName != _csName ) RemoveAt( posOld );
		}
}


void WAW_DataList::m_pub_fn_vGetChildren( CString _csName )
{
	m_pub_fn_vGetAll();
	m_pub_fn_vSelectChildren( _csName );
}


void WAW_DataList::m_pub_fn_vSelectChildren( CString _csName )
{
	ERROR_PREPARE_M( WAW_g_C_csModuleNameForError,
									 "Select the children of a Data",
									 "WAW_DataList::m_pub_fn_vSelectChildren",
									 E_ERROR_GRAVITY_INSTABLE,
									 "The pointer to CTreeCtrl is NULL" );
	CTreeCtrl * pclTreeCtrl = WAW_g_pclWatchWindow->m_pub_fn_pclGetTreeCtrl();
	ERROR_ASSERT( pclTreeCtrl != NULL );
	if( pclTreeCtrl == NULL ) return;

	POSITION pos = GetHeadPosition();
	POSITION posOld = NULL;
	while( pos != NULL )
		{
		posOld = pos;
		WAW_Data * pclData = GetNext( pos );
		HTREEITEM hParentItem = pclTreeCtrl->GetParentItem( pclData->m_pri_hTreeItem );
		if( hParentItem != NULL )
		{
			WAW_Data * pclParentData = (WAW_Data*)pclTreeCtrl->GetItemData( hParentItem );
			if( pclParentData->m_pri_csDataName != _csName ) RemoveAt( posOld );
		}
		else RemoveAt( posOld );
		}
}


void WAW_DataList::m_pub_fn_vGetVisible()
{
	m_pub_fn_vGetAll();
	m_pub_fn_vSelectVisible();
}


void WAW_DataList::m_pub_fn_vSelectVisible()
{
	m_pri_fn_vUpdateVisible();

	POSITION pos = GetHeadPosition();
	POSITION posOld = NULL;
	while( pos != NULL )
	{
		posOld = pos;
		WAW_Data * pclData = GetNext( pos );
		if( pclData->m_pri_bVisible == FALSE ) RemoveAt( posOld );
	}
}


void WAW_DataList::m_pri_fn_vUpdateVisible()
{
	POSITION pos = GetHeadPosition();
	while( pos != NULL )
	{
		WAW_Data * pclData = GetNext( pos );
		pclData->m_pri_bVisible = FALSE;
	}

	ERROR_PREPARE_M( WAW_g_C_csModuleNameForError,
									 "Select the visible Data",
									 "WAW_DataList::m_pub_fn_vSelectVisible",
									 E_ERROR_GRAVITY_INSTABLE,
									 "The pointer to CTreeCtrl is NULL" );
	CTreeCtrl * pclTreeCtrl = WAW_g_pclWatchWindow->m_pub_fn_pclGetTreeCtrl();
	ERROR_ASSERT( pclTreeCtrl != NULL );
	if( pclTreeCtrl == NULL ) return;

	HTREEITEM hItem = pclTreeCtrl->GetFirstVisibleItem();
	while( hItem != NULL )
	{
		WAW_Data * pclData = (WAW_Data*)pclTreeCtrl->GetItemData( hItem );
		pclData->m_pri_bVisible = TRUE;
		hItem = pclTreeCtrl->GetNextVisibleItem( hItem );
	}
}