/*
 * Copyright (C) 2014-2022 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 <QHash> /* ::qHash */
#include <QString>

#include "src/tags/tag_entry.h"

#define INVALID_TAG_ID -1
#define DFLT_COLOUR "ffffff"
static const QString nullString;

/*!
 * @brief PIMPL TagEntry class.
 */
class TagEntryPrivate {
	//Q_DISABLE_COPY(TagEntryPrivate)
public:
	TagEntryPrivate(void)
	    : m_id(INVALID_TAG_ID), m_name(), m_colour(DFLT_COLOUR)
	{ }

	TagEntryPrivate &operator=(const TagEntryPrivate &other) Q_DECL_NOTHROW
	{
		m_id = other.m_id;
		m_name = other.m_name;
		m_colour = other.m_colour;

		return *this;
	}

	bool operator==(const TagEntryPrivate &other) const
	{
		return (m_id == other.m_id) &&
		    (m_name == other.m_name) &&
		    (m_colour == other.m_colour);
	}

	int m_id; /*!< Tag identifier. */
	QString m_name; /*!< Name of the tag. */
	QString m_colour; /*!<
	                   * Colour of the tag in hex format without
	                   * the leading hashtag.
	                   */
};

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

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

	*d = *other.d_func();
}

#ifdef Q_COMPILER_RVALUE_REFS
TagEntry::TagEntry(TagEntry &&other) Q_DECL_NOEXCEPT
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
    : d_ptr(other.d_ptr.release()) //d_ptr(::std::move(other.d_ptr))
#else /* < Qt-5.12 */
    : d_ptr(other.d_ptr.take())
#endif /* >= Qt-5.12 */
{
}
#endif /* Q_COMPILER_RVALUE_REFS */

TagEntry::~TagEntry(void)
{
}

TagEntry::TagEntry(int id, const QString &name, const QString &colour)
    : d_ptr(new (::std::nothrow) TagEntryPrivate)
{
	Q_D(TagEntry);
	d->m_id = id;
	d->m_name = name;
	d->m_colour = colour;
}

#ifdef Q_COMPILER_RVALUE_REFS
TagEntry::TagEntry(int id, QString &&name, QString &&colour)
    : d_ptr(new (::std::nothrow) TagEntryPrivate)
{
	Q_D(TagEntry);
	d->m_id = id;
	d->m_name = ::std::move(name);
	d->m_colour = ::std::move(colour);
}
#endif /* Q_COMPILER_RVALUE_REFS */

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

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

	*d = *other.d_func();

	return *this;
}

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

bool TagEntry::operator==(const TagEntry &other) const
{
	Q_D(const TagEntry);
	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 TagEntry::operator!=(const TagEntry &other) const
{
	return !operator==(other);
}

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

bool TagEntry::isValid(void) const
{
	/* TODO -- Use Colour::isValidColourStr(). */
	return !isNull() && (id() >= 0) && !name().isEmpty() && (6 == colour().size());
}

int TagEntry::id(void) const
{
	Q_D(const TagEntry);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return INVALID_TAG_ID;
	}

	return d->m_id;
}

void TagEntry::setId(int i)
{
	ensureTagEntryPrivate();
	Q_D(TagEntry);
	d->m_id = i;
}

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

	return d->m_name;
}

void TagEntry::setName(const QString &n)
{
	ensureTagEntryPrivate();
	Q_D(TagEntry);
	d->m_name = n;
}

#ifdef Q_COMPILER_RVALUE_REFS
void TagEntry::setName(QString &&n)
{
	ensureTagEntryPrivate();
	Q_D(TagEntry);
	d->m_name = ::std::move(n);
}
#endif /* Q_COMPILER_RVALUE_REFS */

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

	return d->m_colour;
}

void TagEntry::setColour(const QString &c)
{
	ensureTagEntryPrivate();
	Q_D(TagEntry);
	d->m_colour = c;
}

#ifdef Q_COMPILER_RVALUE_REFS
void TagEntry::setColour(QString &&c)
{
	ensureTagEntryPrivate();
	Q_D(TagEntry);
	d->m_colour = ::std::move(c);
}
#endif /* Q_COMPILER_RVALUE_REFS */

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

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
size_t qHash(const TagEntry &key, size_t seed)
#else /* < Qt-6.0 */
uint qHash(const TagEntry &key, uint seed)
#endif /* >= Qt-6.0 */
{
	return ::qHash(key.id(), seed);
}
