/*
 * Copyright (C) 2014-2021 CZ.NIC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#include <QList>
#include <QString>

#include "src/tags/tag_assignment.h"
#include "src/tags/tag_entry.h"

#define DLFT_TESTING false
#define DLFT_DMID -1
static const QString nullString;
static const QList<TagEntry> nullEntries;

/*!
 * @brief PIMPL TagAssignment class.
 */
class TagAssignmentPrivate {
	//Q_DISABLE_COPY(TagAssignmentPrivate)
public:
	TagAssignmentPrivate(void)
	    : m_username(), m_testing(DLFT_TESTING), m_dmId(DLFT_DMID), m_entries()
	{ }

	~TagAssignmentPrivate(void)
	{ }

	TagAssignmentPrivate &operator=(const TagAssignmentPrivate &other) Q_DECL_NOTHROW
	{
		m_username = other.m_username;
		m_testing = other.m_testing;
		m_dmId = other.m_dmId;
		m_entries = other.m_entries;

		return *this;
	}

	bool operator==(const TagAssignmentPrivate &other) const
	{
		return (m_username == other.m_username) &&
		    (m_testing == other.m_testing) &&
		    (m_dmId == other.m_dmId) &&
		    (m_entries == other.m_entries);
	}

	QString m_username; /*!< User account login. */
	bool m_testing; /*!< Whether this is a testing environment account. */
	qint64 m_dmId; /*!< Message identifier. */
	QList<TagEntry> m_entries; /*!< Assigned tag entries. */
};

TagAssignment::TagAssignment(void)
    : d_ptr(Q_NULLPTR)
{
}

TagAssignment::TagAssignment(const TagAssignment &other)
    : d_ptr((other.d_func() != Q_NULLPTR) ? (new (::std::nothrow) TagAssignmentPrivate) : Q_NULLPTR)
{
	Q_D(TagAssignment);
	if (d == Q_NULLPTR) {
		return;
	}

	*d = *other.d_func();
}

#ifdef Q_COMPILER_RVALUE_REFS
TagAssignment::TagAssignment(TagAssignment &&other) Q_DECL_NOEXCEPT
    : d_ptr(other.d_ptr.take()) //d_ptr(::std::move(other.d_ptr))
{
}
#endif /* Q_COMPILER_RVALUE_REFS */

TagAssignment::~TagAssignment(void)
{
}

TagAssignment::TagAssignment(const QString &username, bool testing,
    qint64 dmId, const QList<TagEntry> &entries)
    : d_ptr(new (::std::nothrow) TagAssignmentPrivate)
{
	Q_D(TagAssignment);
	d->m_username = username;
	d->m_testing = testing;
	d->m_dmId = dmId;
	d->m_entries = entries;
}

#ifdef Q_COMPILER_RVALUE_REFS
TagAssignment::TagAssignment(QString &&username, bool testing,
    qint64 dmId, QList<TagEntry> &&entries)
    : d_ptr(new (::std::nothrow) TagAssignmentPrivate)
{
	Q_D(TagAssignment);
	d->m_username = ::std::move(username);
	d->m_testing = testing;
	d->m_dmId = dmId;
	d->m_entries = ::std::move(entries);
}
#endif /* Q_COMPILER_RVALUE_REFS */

/*!
 * @brief Ensures private assignment presence.
 *
 * @note Returns if private assignment could not be allocated.
 */
#define ensureTagAssignmentPrivate(_x_) \
	do { \
		if (Q_UNLIKELY(d_ptr == Q_NULLPTR)) { \
			TagAssignmentPrivate *p = new (::std::nothrow) TagAssignmentPrivate; \
			if (Q_UNLIKELY(p == Q_NULLPTR)) { \
				Q_ASSERT(0); \
				return _x_; \
			} \
			d_ptr.reset(p); \
		} \
	} while (0)

TagAssignment &TagAssignment::operator=(const TagAssignment &other) Q_DECL_NOTHROW
{
	if (other.d_func() == Q_NULLPTR) {
		d_ptr.reset(Q_NULLPTR);
		return *this;
	}
	ensureTagAssignmentPrivate(*this);
	Q_D(TagAssignment);

	*d = *other.d_func();

	return *this;
}

#ifdef Q_COMPILER_RVALUE_REFS
TagAssignment &TagAssignment::operator=(TagAssignment &&other) Q_DECL_NOTHROW
{
	swap(*this, other);
	return *this;
}
#endif /* Q_COMPILER_RVALUE_REFS */

bool TagAssignment::operator==(const TagAssignment &other) const
{
	Q_D(const TagAssignment);
	if ((d == Q_NULLPTR) && ((other.d_func() == Q_NULLPTR))) {
		return true;
	} else if ((d == Q_NULLPTR) || ((other.d_func() == Q_NULLPTR))) {
		return false;
	}

	return *d == *other.d_func();
}

bool TagAssignment::operator!=(const TagAssignment &other) const
{
	return !operator==(other);
}

bool TagAssignment::isNull(void) const
{
	Q_D(const TagAssignment);
	return d == Q_NULLPTR;
}

const QString &TagAssignment::username(void) const
{
	Q_D(const TagAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return nullString;
	}

	return d->m_username;
}

void TagAssignment::setUsername(const QString &n)
{
	ensureTagAssignmentPrivate();
	Q_D(TagAssignment);
	d->m_username = n;
}

#ifdef Q_COMPILER_RVALUE_REFS
void TagAssignment::setUsername(QString &&n)
{
	ensureTagAssignmentPrivate();
	Q_D(TagAssignment);
	d->m_username = ::std::move(n);
}
#endif /* Q_COMPILER_RVALUE_REFS */

bool TagAssignment::testing(void) const
{
	Q_D(const TagAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return DLFT_TESTING;
	}

	return d->m_testing;
}

void TagAssignment::setTesting(bool t)
{
	ensureTagAssignmentPrivate();
	Q_D(TagAssignment);
	d->m_testing = t;
}

qint64 TagAssignment::dmId(void) const
{
	Q_D(const TagAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return DLFT_DMID;
	}

	return d->m_dmId;
}

void TagAssignment::setDmId(qint64 di)
{
	ensureTagAssignmentPrivate();
	Q_D(TagAssignment);
	d->m_dmId = di;
}

const QList<TagEntry> &TagAssignment::entries(void) const
{
	Q_D(const TagAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return nullEntries;
	}

	return d->m_entries;
}

void TagAssignment::setEntries(const QList<TagEntry> &e)
{
	ensureTagAssignmentPrivate();
	Q_D(TagAssignment);
	d->m_entries = e;
}

#ifdef Q_COMPILER_RVALUE_REFS
void TagAssignment::setEntries(QList<TagEntry> &&e)
{
	ensureTagAssignmentPrivate();
	Q_D(TagAssignment);
	d->m_entries = ::std::move(e);
}
#endif /* Q_COMPILER_RVALUE_REFS */

void swap(TagAssignment &first, TagAssignment &second) Q_DECL_NOTHROW
{
	using ::std::swap;
	swap(first.d_ptr, second.d_ptr);
}
