/*
 * Copyright (C) 2014-2026 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 <QJsonObject>
#include <QString>

#include "note_entry.h"
#include "src/datovka_shared/json/helper.h"

#define INVALID_NOTE_ID -1

static const QString nullString;

/*!
 * @brief PIMPL NoteEntry class.
 */
class Json::NoteEntryPrivate {
	//Q_DISABLE_COPY(NoteEntryPrivate)
public:
	NoteEntryPrivate(void)
	    : m_id(INVALID_NOTE_ID), m_content()
	{ }

	NoteEntryPrivate &operator=(const NoteEntryPrivate &other) Q_DECL_NOTHROW
	{
		m_id = other.m_id;
		m_content = other.m_content;

		return *this;
	}

	bool operator==(const NoteEntryPrivate &other) const
	{
		return (m_id == other.m_id) &&
		    (m_content == other.m_content);
	}

	qint64 m_id; /*!< Note identifier. */
	QString m_content; /*!< Note content. */
};

Json::NoteEntry::NoteEntry(void)
    : Object(),
    d_ptr(Q_NULLPTR)
{
}

Json::NoteEntry::NoteEntry(const NoteEntry &other)
    : Object(),
    d_ptr((other.d_func() != Q_NULLPTR) ? (new (::std::nothrow) NoteEntryPrivate) : Q_NULLPTR)
{
	Q_D(NoteEntry);
	if (d == Q_NULLPTR) {
		return;
	}

	*d = *other.d_func();
}

#ifdef Q_COMPILER_RVALUE_REFS
Json::NoteEntry::NoteEntry(NoteEntry &&other) Q_DECL_NOEXCEPT
    : Object(),
#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 */

Json::NoteEntry::~NoteEntry(void)
{
}

Json::NoteEntry::NoteEntry(qint64 id, const QString &content)
    : Object(),
    d_ptr(new (::std::nothrow) NoteEntryPrivate)
{
	Q_D(NoteEntry);
	d->m_id = id;
	d->m_content = content;
}

#ifdef Q_COMPILER_RVALUE_REFS
Json::NoteEntry::NoteEntry(qint64 id, QString &&content)
    : Object(),
    d_ptr(new (::std::nothrow) NoteEntryPrivate)
{
	Q_D(NoteEntry);
	d->m_id = id;
	d->m_content = ::std::move(content);
}
#endif /* Q_COMPILER_RVALUE_REFS */

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

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

	*d = *other.d_func();

	return *this;
}

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

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

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

bool Json::NoteEntry::isValid(void) const
{
	return !isNull() && (id() >= 0) && !content().isEmpty();
}

qint64 Json::NoteEntry::id(void) const
{
	Q_D(const NoteEntry);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return INVALID_NOTE_ID;
	}

	return d->m_id;
}

void Json::NoteEntry::setId(qint64 id)
{
	ensureNoteEntryPrivate();
	Q_D(NoteEntry);
	d->m_id = id;
}

const QString &Json::NoteEntry::content(void) const
{
	Q_D(const NoteEntry);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return nullString;
	}

	return d->m_content;
}

void Json::NoteEntry::setContent(const QString &co)
{
	ensureNoteEntryPrivate();
	Q_D(NoteEntry);
	d->m_content = co;
}

#ifdef Q_COMPILER_RVALUE_REFS
void Json::NoteEntry::setContent(QString &&co)
{
	ensureNoteEntryPrivate();
	Q_D(NoteEntry);
	d->m_content = ::std::move(co);
}
#endif /* Q_COMPILER_RVALUE_REFS */

Json::NoteEntry Json::NoteEntry::fromJson(const QByteArray &json,
    bool *ok)
{
	QJsonObject jsonObj;
	if (!Helper::readRootObject(json, jsonObj)) {
		if (ok != Q_NULLPTR) {
			*ok = false;
		}
		return NoteEntry();
	}

	return fromJsonVal(jsonObj, ok);
}

static const QString keyId("id");
static const QString keyContent("content");

Json::NoteEntry Json::NoteEntry::fromJsonVal(const QJsonValue &jsonVal,
    bool *ok)
{
	NoteEntry ste;

	if (Q_UNLIKELY(!jsonVal.isObject())) {
		goto fail;
	}

	{
		const QJsonObject jsonObj = jsonVal.toObject();

		{
			qint64 valInt = 0;
			if (Q_UNLIKELY(!Helper::readQint64String(jsonObj, keyId,
			        valInt, Helper::ACCEPT_VALID))) {
				goto fail;
			}
			ste.setId(valInt);
		}
		{
			QString valStr;
			if (Q_UNLIKELY(!Helper::readString(jsonObj, keyContent,
			        valStr, Helper::ACCEPT_NULL))) {
				goto fail;
			}
			ste.setContent(::std::move(valStr));
		}
	}

	if (ok != Q_NULLPTR) {
		*ok = true;
	}
	return ste;

fail:
	if (ok != Q_NULLPTR) {
		*ok = false;
	}
	return NoteEntry();
}

bool Json::NoteEntry::toJsonVal(QJsonValue &jsonVal) const
{
	QJsonObject jsonObj;

	jsonObj.insert(keyId, (id() >= 0) ? QString::number(id()) : QJsonValue());
	jsonObj.insert(keyContent, (!content().isEmpty()) ? content() : QJsonValue());

	jsonVal = jsonObj;
	return true;
}

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

Json::NoteEntryList::NoteEntryList(void)
    : Object(),
    QList<NoteEntry>()
{
}

Json::NoteEntryList::NoteEntryList(const QList<NoteEntry> &other)
    : Object(),
    QList<NoteEntry>(other)
{
}

#ifdef Q_COMPILER_RVALUE_REFS
Json::NoteEntryList::NoteEntryList(QList<NoteEntry> &&other)
    : Object(),
    QList<NoteEntry>(::std::move(other))
{
}
#endif /* Q_COMPILER_RVALUE_REFS */

Json::NoteEntryList Json::NoteEntryList::fromJson(const QByteArray &json,
    bool *ok)
{
	QJsonArray jsonArr;
	if (!Helper::readRootArray(json, jsonArr)) {
		if (ok != Q_NULLPTR) {
			*ok = false;
		}
		return NoteEntryList();
	}

	return fromJsonVal(jsonArr, ok);
}

Json::NoteEntryList Json::NoteEntryList::fromJsonVal(
    const QJsonValue &jsonVal, bool *ok)
{
	NoteEntryList tel;

	if (Q_UNLIKELY(!jsonVal.isArray())) {
		goto fail;
	}

	{
		const QJsonArray jsonArr = jsonVal.toArray();
		for (const QJsonValue &v : jsonArr) {
			bool iOk = false;
			tel.append(NoteEntry::fromJsonVal(v, &iOk));
			if (Q_UNLIKELY(!iOk)) {
				goto fail;
			}
		}
	}

	if (ok != Q_NULLPTR) {
		*ok = true;
	}
	return tel;

fail:
	if (ok != Q_NULLPTR) {
		*ok = false;
	}
	return NoteEntryList();
}

bool Json::NoteEntryList::toJsonVal(QJsonValue &jsonVal) const
{
	QJsonArray arr;

	for (const NoteEntry &te : *this) {
		QJsonValue v;
		if (Q_UNLIKELY(!te.toJsonVal(v))) {
			goto fail;
		}
		arr.append(v);
	}

	jsonVal = arr;
	return true;

fail:
	jsonVal = QJsonArray();
	return false;
}

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