// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

#include <bdLobby/bdTitleUtilities/bdAdUnit.h>

bdAdUnit::bdAdUnit()
:m_layout(bdTitleUtilities::BD_0x0),
 m_size(0),
 m_adUnit(BD_NULL)
{
}

bdAdUnit::~bdAdUnit()
{
	if (m_adUnit)
	{
		bdMemory::deallocate(m_adUnit);
		m_adUnit = BD_NULL;
	}
}

bdBool bdAdUnit::handleResult(const bdUInt numResults, 
                              bdBitBufferRef buffer)
{
	bdBool ok = true;
	m_numResults = 0;
	
	if (numResults == 1)
	{
		ok = ok && buffer->readUInt64(m_unitID);

		bdInt16 layout = 0;
		ok = ok && buffer->readInt16(layout);
		m_layout = static_cast<bdTitleUtilities::bdUnitLayout>(layout);

		bdInt16 screenType = 0;
		ok = ok && buffer->readInt16(screenType);
		m_screenType = static_cast<bdTitleUtilities::bdScreenType>(screenType);

		ok = ok && buffer->readString(m_mimeType,BD_MAX_MIMETYPE_STRING_LENGTH);
		
		bdUInt32 size = 0;
		ok = ok && buffer->readUInt32(size);

		if(ok)
		{
			m_size = size;
			m_adUnit = bdMemory::allocate(size);
			ok = ok && buffer->readBlob(static_cast<bdUByte8*>(m_adUnit), size);
			m_numResults = 1;
		}
	}
	return ok;
}

