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

#include <bdLobby/bdProfiles/bdProfiles.h>
#include <bdLobby/bdProfiles/bdProfileInfo.h>
#include <bdLobby/bdRemoteTaskManager/bdRemoteTaskManager.h>

#define BD_LOG_LEVEL	("lobby/profiles")

bdProfiles::bdProfiles(bdRemoteTaskManager *const remoteTaskManager)
: m_errorCode(BD_NO_ERROR),
  m_remoteTaskManager(remoteTaskManager)
{
}

bdProfiles::~bdProfiles()
{
	m_remoteTaskManager = BD_NULL;
}

bdRemoteTask* bdProfiles::createProfile(bdUByte8 /*controllerIndex*/, 
            							bdProfileInfo *const profileInfo)
{
	bdRemoteTask *remoteTask = BD_NULL;

	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);
	profileInfo->serialize(buffer);

	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
												BD_PROFILE_MANAGEMENT_SERVICE,
												BD_PROFILE_CREATE,
												buffer);

	return remoteTask;
}

bdRemoteTask* bdProfiles::getPublicInfo(bdUByte8 /*controllerIndex*/,
										bdOnlineUserID userID)
{
	bdRemoteTask *remoteTask = BD_NULL;

	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);
	buffer.writeUInt64(userID);

	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
												BD_PROFILE_MANAGEMENT_SERVICE,
												BD_PROFILE_GET_PUBLIC_INFO,
												buffer);

	return remoteTask;
}

bdRemoteTask* bdProfiles::getPrivateInfo(bdUByte8 /*controllerIndex*/)
{
	bdRemoteTask *remoteTask = BD_NULL;

	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);

	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
												BD_PROFILE_MANAGEMENT_SERVICE,
												BD_PROFILE_GET_PRIVATE_INFO,
												buffer);

	return remoteTask;
}

bdRemoteTask* bdProfiles::setPublicInfo(bdUByte8 /*controllerIndex*/,
										bdProfileInfo *const profileInfo)
{
	bdRemoteTask *remoteTask = BD_NULL;

	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);
	profileInfo->serialize(buffer);
	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
												BD_PROFILE_MANAGEMENT_SERVICE,
												BD_PROFILE_SET_PUBLIC_INFO,
												buffer);
	return remoteTask;
}

bdRemoteTask* bdProfiles::setPrivateInfo(bdUByte8 /*controllerIndex*/,
                                         bdProfileInfo *const profileInfo)
{
	bdRemoteTask *remoteTask = BD_NULL;
	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);
	profileInfo->serialize(buffer);

	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
												BD_PROFILE_MANAGEMENT_SERVICE,
												BD_PROFILE_SET_PRIVATE_INFO,
												buffer);

	return remoteTask;
}

bdRemoteTask* bdProfiles::deleteProfile(bdUByte8 /*controllerIndex*/)
{
	bdRemoteTask *remoteTask = BD_NULL;
	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);
	 

	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
												BD_PROFILE_MANAGEMENT_SERVICE,
												BD_PROFILE_DELETE,
												buffer);

	return remoteTask;
}

bdRemoteTask* bdProfiles::checkProfileExists(bdUByte8 /*controllerIndex*/,
											 bdOnlineUserID userID)
{
	bdRemoteTask *remoteTask = BD_NULL;
	bdBitBuffer buffer (0,true);
	buffer.writeUByte8(0);
	buffer.writeUInt64(userID);

	m_errorCode = m_remoteTaskManager->startTask(&remoteTask,
		BD_PROFILE_MANAGEMENT_SERVICE,
		BD_PROFILE_EXISTS,
		buffer);

	return remoteTask;
}


bdBool bdProfiles::handleTask(const bdRemoteTask *remoteTask, bdQueryResult *queryResult)
{
	bdBitBufferRef buffer = remoteTask->getResults();
	bdUInt32 taskErrorCode = BD_MAX_ERROR_CODE;
	bdBool ok = buffer->readUInt32(taskErrorCode);
	if (ok)
	{
		m_errorCode = static_cast<bdLobbyErrorCode>(taskErrorCode);
		if ( m_errorCode == BD_NO_ERROR)
		{
			bdUByte8 operation = 0;
			ok = buffer->readUByte8(operation);
			if(BD_NULL != queryResult)
			{
				const bdUInt numReplies = 1;
				ok = ok && queryResult->handleResult(numReplies, buffer);
			}
		}
#if defined BD_DEBUG
		else
		{

			bdUInt64 transactionID;
			bdUInt64 connectionID;

			connectionID = m_remoteTaskManager->getConnectionID();
			transactionID = remoteTask->getTransactionID();

			// here we originally called bdRemoteTaskManager::obtainTransactionID
			BD_WARN(BD_LOG_LEVEL, "Transaction ID " BD_UINT_64_FORMAT ":" BD_UINT_64_FORMAT " failed.",
				connectionID, transactionID);
		}
#endif // BD_DEBUG	
	}

	return (m_errorCode == BD_NO_ERROR && ok);
}

bdLobbyErrorCode bdProfiles::getErrorCode() const
{
	return m_errorCode;
}
