/*
 * Result of query for loginState entry with given username and serviceName when removing a buddy
 *
 * Remove buddy from buddyStatus and contact DB
 */
MojErr SendOneMessageHandler::findAccountIdResult(MojObject& result, MojErr findErr)
{

	if (findErr) {

		MojString error;
		MojErrToString(findErr, error);
		MojLogError(IMServiceApp::s_log, _T("findAccountIdResult failed: error %d - %s"), findErr, error.data());
		// not much we can do here...
		failMessage(ERROR_SEND_GENERIC_ERROR);

	} else {

		// parse out the accountId
		readAccountIdFromResults(result);

		if (!m_accountId.empty()){

			// construct our where clause - find by username, accountId and servicename
			MojDbQuery query;
			query.where("username", MojDbQuery::OpEq, m_usernameTo);
			query.where("accountId", MojDbQuery::OpEq, m_accountId);
			query.from(IM_BUDDYSTATUS_KIND);
			MojObject queryObject;
			query.toObject(queryObject);
			IMServiceHandler::logMojObjectJsonString(_T("findAccountIdResult - buddyStatus query: %s"), queryObject);

			// call find
			//virtual MojErr find(Signal::SlotRef handler, const MojDbQuery& query,
			//					bool watch = false, bool returnCount = false) = 0;
			MojErr err = m_tempdbClient.find(this->m_findBuddySlot, query, /* watch */ false );
			if (err) {
				MojString error;
				MojErrToString(err, error);
				MojLogError(IMServiceApp::s_log, _T("findAccountIdResult dbClient.find() failed: error %d - %s"), err, error.data());
				failMessage(ERROR_SEND_GENERIC_ERROR);
			}
		}
		else {
			MojLogError(IMServiceApp::s_log, _T("findAccountIdResult: no matching loginState record found for %s"), m_serviceName.data());
			// tell the outgoing Command handler we are done
			failMessage(ERROR_SEND_GENERIC_ERROR);
		}
	}

	return MojErrNone;
}
/*
 * Result of query for loginState entry with given username and serviceName when adding a buddy
 *
 * Save buddy in buddyStatus and contact DB
 */
MojErr SendOneCommandHandler::findAccountIdForAddResult(MojObject& result, MojErr findErr)
{

	if (findErr) {

		MojString error;
		MojErrToString(findErr, error);
		MojLogError(IMServiceApp::s_log, _T("findAccountIdResult failed: error %d - %s"), findErr, error.data());

	} else {

		// parse out the accountId
		readAccountIdFromResults(result);

		if (!m_accountId.empty()){
			MojObject buddyStatus;
			buddyStatus.putString("_kind", IM_BUDDYSTATUS_KIND);
			buddyStatus.put("accountId", m_accountId);
			buddyStatus.put("username", m_buddyName);
			buddyStatus.put("serviceName", m_serviceName);

			// log it
			MojString json;
			buddyStatus.toJson(json);
			MojLogInfo(IMServiceApp::s_log, _T("saving buddy status to db: %s"), json.data());

			// save it
			// the save generates a call to the save result handler
			MojErr err = m_tempdbClient.put(m_addBuddySlot, buddyStatus);
			if (err) {
				MojString error;
				MojErrToString(err, error);
				MojLogError(IMServiceApp::s_log, _T("findAccountIdResult: dbClient.put() failed: error %d - %s"), err, error.data());
				// tell the outgoing Command handler we are done
				m_outgoingIMHandler->messageFinished();
			}
		}
		else {
			MojLogError(IMServiceApp::s_log, _T("findAccountIdResult: no matching loginState record found for %s %s"), m_username.data(), m_serviceName.data());
			// tell the outgoing Command handler we are done
			m_outgoingIMHandler->messageFinished();
		}
	}

	return MojErrNone;
}
/*
 * Result of query for loginState entry with given username and serviceName when removing a buddy
 *
 * Remove buddy from buddyStatus and contact DB
 */
MojErr SendOneCommandHandler::findAccountIdForRemoveResult(MojObject& result, MojErr findErr)
{

	if (findErr) {

		MojString error;
		MojErrToString(findErr, error);
		MojLogError(IMServiceApp::s_log, _T("findAccountIdResult failed: error %d - %s"), findErr, error.data());

	} else {

		// parse out the accountId
		readAccountIdFromResults(result);

		if (!m_accountId.empty()){

			// construct our where clause - find by username, accountId and servicename
			MojDbQuery query;
			query.where("username", MojDbQuery::OpEq, m_buddyName);
			query.where("accountId", MojDbQuery::OpEq, m_accountId);
			query.from(IM_BUDDYSTATUS_KIND);

			// call del
			// virtual MojErr del(Signal::SlotRef handler, const MojDbQuery& query,
			//					   MojUInt32 flags = MojDb::FlagNone);
			MojErr err = m_tempdbClient.del(m_removeBuddySlot, query);
			if (err) {
				MojString error;
				MojErrToString(err, error);
				MojLogError(IMServiceApp::s_log, _T("removeBuddy: dbClient.del() failed: error %d - %s"), err, error.data());
				// tell the outgoing Command handler we are done
				m_outgoingIMHandler->messageFinished();
			}
		}
		else {
			MojLogError(IMServiceApp::s_log, _T("findAccountIdResult: no matching loginState record found for %s %s"), m_username.data(), m_serviceName.data());
			// tell the outgoing Command handler we are done
			m_outgoingIMHandler->messageFinished();
		}
	}

	return MojErrNone;
}