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

#include <bdLobby/bdKeyArchive/bdArchiveKey.h>
#include <bdCore/bdUtilities/bdBytePacker.h>

#define BD_LOG_LEVEL ("lobby/bdKeyArchive/bdArchiveKey")

bdArchiveKey::bdArchiveKey()
: m_entityID(0)
{
}

bdArchiveKey::bdArchiveKey(const bdEntityID entityID,
						   const bdBool isTeam,
						   const bdString keyName)
: m_entityID(entityID),
  m_isTeam(isTeam),
  m_keyName(keyName)
{
}

bdBool bdArchiveKey::operator==(const bdArchiveKey &other) const
{
	return (m_entityID == other.m_entityID) &&
		   (m_isTeam == other.m_isTeam) &&
		   (m_keyName == other.m_keyName);
}

void bdArchiveKey::set(const bdEntityID entityID,
					   const bdBool isTeam,
					   const bdString keyName)
{
	m_entityID = entityID;
	m_isTeam = isTeam;
	m_keyName = keyName;
}

bdEntityID bdArchiveKey::getEntityID() const
{
	return m_entityID;
}

bdBool bdArchiveKey::getIsTeam() const
{
	return m_isTeam;
}

bdString bdArchiveKey::getKeyName() const
{
	return m_keyName;
}

void bdArchiveKey::serialize(bdBitBufferRef buffer) const
{
	if(m_entityID == 0)
	{
		BD_WARN(BD_LOG_LEVEL, "Serializing uninitialized data.");
	}

	buffer->writeUInt64(m_entityID);
	buffer->writeBool(m_isTeam);
	buffer->writeString(m_keyName);
}

bdBool bdArchiveKey::deserialize(bdBitBufferRef buffer)
{
	bdBool ok = true;

	ok = ok && buffer->readUInt64(m_entityID);
	ok = ok && buffer->readBool(m_isTeam);
	ok = ok && buffer->readString(m_keyName);

	return ok;
}

bdUInt bdArchiveKeyHash::getHash(const bdArchiveKey &key) const
{
	bdUInt h;
	bdHashingClass hashingClass;
	h = hashingClass.getHash(key.getEntityID());
	h += hashingClass.getHash(key.getIsTeam());
	h += hashingClass.getHash(key.m_keyName);
	return h;
}
