///////////////////////////////////////////////////////////////////////////////
/// @file dePacketList.cpp
///
/// @brief 
///
/// @author trajar
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Jun 2002
/// @author trajar
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////

#include "dePacketList.hpp"

#ifdef	_DEBUG
 #include	<crtdbg.h> 
 #define new new(_NORMAL_BLOCK,__FILE__, __LINE__)
#endif

_inline dePacketList::dePacketList(void)
{
	m_ID = 0;
}

_inline dePacketList::~dePacketList(void)
{
	m_aPackets.clear();
}

_inline void dePacketList::AddPacket( dePacket * pPacket )
{
	deASSERT( pPacket );
	
	// make sure the IDs are the same
	if( GetNumPakets() > 0 )
	{
		deASSERT( pPacket->GetHeader().ID == m_ID );
	}
	else
	{
		// get ID
		m_ID = pPacket->GetHeader().ID;
	}

	m_aPackets.insert( *pPacket );
}

dePacket * dePacketList::ConsolidatePackets( void )
{
	// total packets
	int iNum	= this->GetNumPakets();
	int i		= 0;

	// allocate memory
	dePacket * packets = new dePacket[ iNum ];

	// copy data
	aPackets::iterator itr = m_aPackets.begin();
	while( itr != NULL && itr != m_aPackets.end() )
	{
		packets[i] = *itr;
		++itr;
		i++;
	}

	// deallocate mem
	SAFE_DELETE_ARRAY( packets );

	return dePacket::ConsolidatePackets( packets, this->GetNumPakets() );
}

_inline int dePacketList::GetNumPakets( void )
{
	return m_aPackets.size();
}

bool dePacketList::IsCompleteList( void )
{
	bool	bComplete	= false;
	int		iSize		= 0;
	int		iLastSize	= -1;

	aPackets::iterator itr = m_aPackets.begin();
	
	while( itr != NULL && itr != m_aPackets.end() )
	{
		// ensure everything has the same total size
		if( iLastSize != -1 && itr->GetHeader().TotalSize != iLastSize )
		{
			bComplete = false;
			break;
		}

		// increment the packet size
		iSize += itr->GetHeader().Size;

		iLastSize = itr->GetHeader().TotalSize;
	}

	bComplete = iLastSize == iSize;
	
	return bComplete;
}
