/*
 * 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 <utility> /* ::std::swap */

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

#define DLFT_TEST_ENV Isds::Type::BOOL_NULL
#define nullInt -1
static const Json::NoteEntryList nullEntries;

/*!
 * @brief PIMPL Json::NoteAssignment class.
 */
class Json::NoteAssignmentPrivate {
	//Q_DISABLE_COPY(NoteAssignmentPrivate)
public:
	NoteAssignmentPrivate(void)
	    : m_testEnv(DLFT_TEST_ENV), m_dmId(nullInt), m_entries()
	{ }

	NoteAssignmentPrivate &operator=(const NoteAssignmentPrivate &other) Q_DECL_NOTHROW
	{
		m_testEnv = other.m_testEnv;
		m_dmId = other.m_dmId;
		m_entries = other.m_entries;

		return *this;
	}

	bool operator==(const NoteAssignmentPrivate &other) const
	{
		return (m_testEnv == other.m_testEnv) &&
		    (m_dmId == other.m_dmId) &&
		    (m_entries == other.m_entries);
	}

	enum Isds::Type::NilBool m_testEnv; /*!< Test environment. */
	qint64 m_dmId; /*!< Message identifier. */
	NoteEntryList m_entries;
};

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

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

	*d = *other.d_func();
}

#ifdef Q_COMPILER_RVALUE_REFS
Json::NoteAssignment::NoteAssignment(NoteAssignment &&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::NoteAssignment::~NoteAssignment(void)
{
}

Json::NoteAssignment::NoteAssignment(enum Isds::Type::NilBool testEnv,
    qint64 dmId, const NoteEntryList &entries)
    : Object(),
    d_ptr(new (::std::nothrow) NoteAssignmentPrivate)
{
	Q_D(NoteAssignment);
	d->m_testEnv = testEnv;
	d->m_dmId = dmId;
	d->m_entries = entries;
}

#ifdef Q_COMPILER_RVALUE_REFS
Json::NoteAssignment::NoteAssignment(enum Isds::Type::NilBool testEnv,
    qint64 dmId, NoteEntryList &&entries)
    : Object(),
    d_ptr(new (::std::nothrow) NoteAssignmentPrivate)
{
	Q_D(NoteAssignment);
	d->m_testEnv = testEnv;
	d->m_dmId = dmId;
	d->m_entries = ::std::move(entries);
}
#endif /* Q_COMPILER_RVALUE_REFS */

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

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

	*d = *other.d_func();

	return *this;
}

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

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

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

bool Json::NoteAssignment::isValid(void) const
{
	return !isNull() && (testEnv() != Isds::Type::BOOL_NULL) && (dmId() >= 0);
}

enum Isds::Type::NilBool Json::NoteAssignment::testEnv(void) const
{
	Q_D(const NoteAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return DLFT_TEST_ENV;
	}

	return d->m_testEnv;
}

void Json::NoteAssignment::setTestEnv(enum Isds::Type::NilBool t)
{
	ensureNoteAssignmentPrivate();
	Q_D(NoteAssignment);
	d->m_testEnv = t;
}

qint64 Json::NoteAssignment::dmId(void) const
{
	Q_D(const NoteAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return nullInt;
	}

	return d->m_dmId;
}

void Json::NoteAssignment::setDmId(qint64 di)
{
	ensureNoteAssignmentPrivate();
	Q_D(NoteAssignment);
	d->m_dmId = di;
}

const Json::NoteEntryList &Json::NoteAssignment::entries(void) const
{
	Q_D(const NoteAssignment);
	if (Q_UNLIKELY(d == Q_NULLPTR)) {
		return nullEntries;
	}

	return d->m_entries;
}

void Json::NoteAssignment::setEntries(const NoteEntryList &l)
{
	ensureNoteAssignmentPrivate();
	Q_D(NoteAssignment);
	d->m_entries = l;
}

#ifdef Q_COMPILER_RVALUE_REFS
void Json::NoteAssignment::setEntries(NoteEntryList &&l)
{
	ensureNoteAssignmentPrivate();
	Q_D(NoteAssignment);
	d->m_entries = ::std::move(l);
}
#endif /* Q_COMPILER_RVALUE_REFS */

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

	return fromJsonVal(jsonObj, ok);
}

static const QString keyTestEnv("testEnv");
static const QString keyMessageId("messageId");
static const QString keyNoteEntries("noteEntries");

Json::NoteAssignment Json::NoteAssignment::fromJsonVal(const QJsonValue &jsonVal, bool *ok)
{
	NoteAssignment ta;

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

	{
		const QJsonObject jsonObj = jsonVal.toObject();
		{
			enum Isds::Type::NilBool valNilBool = Isds::Type::BOOL_NULL;
			if (Q_UNLIKELY(!Helper::readNilBool(jsonObj,
			        keyTestEnv, valNilBool, Helper::ACCEPT_NULL))) {
				goto fail;
			}
			ta.setTestEnv(valNilBool);
		}
		{
			qint64 valInt = 0;
			if (Q_UNLIKELY(!Helper::readQint64String(jsonObj,
			        keyMessageId, valInt, Helper::ACCEPT_VALID))) {
				goto fail;
			}
			ta.setDmId(valInt);
		}
		{
			QJsonArray arr;
			bool iOk = false;
			if (Q_UNLIKELY(!Helper::readArray(jsonObj,
			        keyNoteEntries, arr, Helper::ACCEPT_VALID))) {
				goto fail;
			}
			iOk = false;
			ta.setEntries(NoteEntryList::fromJsonVal(arr, &iOk));
			if (Q_UNLIKELY(!iOk)) {
				goto fail;
			}
		}
	}

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

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

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

	jsonObj.insert(keyTestEnv, (testEnv() != Isds::Type::BOOL_NULL) ?
	    QJsonValue(testEnv() == Isds::Type::BOOL_TRUE) : QJsonValue());
	jsonObj.insert(keyMessageId, (dmId() >= 0) ? QString::number(dmId()) : QJsonValue());
	{
		QJsonValue jsonVal;
		if (Q_UNLIKELY(!entries().toJsonVal(jsonVal))) {
			return false;
		}
		jsonObj.insert(keyNoteEntries, jsonVal);
	}

	jsonVal = jsonObj;
	return true;
}

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

Json::NoteAssignmentList::NoteAssignmentList(void)
    : Object(),
    QList<NoteAssignment>()
{
}

Json::NoteAssignmentList::NoteAssignmentList(const QList<NoteAssignment> &other)
    : Object(),
    QList<NoteAssignment>(other)
{
}

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

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

	return fromJsonVal(jsonArr, ok);
}

Json::NoteAssignmentList Json::NoteAssignmentList::fromJsonVal(
    const QJsonValue &jsonVal, bool *ok)
{
	NoteAssignmentList mil;

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

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

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

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

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

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

	jsonVal = arr;
	return true;

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