/*
 * 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.
 */

#pragma once

#include <QByteArray>
#include <QList>
#include <QMetaType>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
#  include <memory> /* ::std::unique_ptr */
#else /* < Qt-5.12 */
#  include <QScopedPointer>
#endif /* >= Qt-5.12 */

#include "src/datovka_shared/json/object.h"

namespace Json {

	class NoteEntryPrivate;
	/*!
	 * @brief Encapsulates database note (tag) entry.
	 */
	class NoteEntry : public Object {
		Q_DECLARE_PRIVATE(NoteEntry)

	public:
		NoteEntry(void);
		NoteEntry(const NoteEntry &other);
#ifdef Q_COMPILER_RVALUE_REFS
		NoteEntry(NoteEntry &&other) Q_DECL_NOEXCEPT;
#endif /* Q_COMPILER_RVALUE_REFS */
		~NoteEntry(void);

		NoteEntry(qint64 id, const QString &content);
#ifdef Q_COMPILER_RVALUE_REFS
		NoteEntry(qint64 id, QString &&content);
#endif /* Q_COMPILER_RVALUE_REFS */

		NoteEntry &operator=(const NoteEntry &other) Q_DECL_NOTHROW;
#ifdef Q_COMPILER_RVALUE_REFS
		NoteEntry &operator=(NoteEntry &&other) Q_DECL_NOTHROW;
#endif /* Q_COMPILER_RVALUE_REFS */

		bool operator==(const NoteEntry &other) const;
		bool operator!=(const NoteEntry &other) const;

		friend void swap(NoteEntry &first, NoteEntry &second) Q_DECL_NOTHROW;

		bool isNull(void) const;

		/*!
		 * @brief Check for validity.
		 *
		 * @note Invalid note has negative id, or empty content.
		 *
		 * @return True if note contains valid data.
		 */
		bool isValid(void) const;

		/* identifier */
		qint64 id(void) const;
		void setId(qint64 id);
		/* note content */
		const QString &content(void) const;
		void setContent(const QString &co);
#ifdef Q_COMPILER_RVALUE_REFS
		void setContent(QString &&co);
#endif /* Q_COMPILER_RVALUE_REFS */

		/*!
		 * @brief Creates an instance from supplied JSON document.
		 *
		 * @param[in]  json JSON data.
		 * @param[out] ok Set to true on success.
		 * @return Invalid value on error a valid structure else.
		 */
		static
		NoteEntry fromJson(const QByteArray &json, bool *ok = Q_NULLPTR);

		/*!
		 * @brief Creates an instance from supplied JSON data.
		 *
		 * @param[in]  jsonObj JSON data.
		 * @param[out] ok Set to true on success.
		 * @return Invalid value on error a valid structure else.
		 */
		static
		NoteEntry fromJsonVal(const QJsonValue &jsonVal, bool *ok = Q_NULLPTR);

		/*!
		 * @brief Writes content of JSON object.
		 *
		 * @param[out] jsonVal JSON value to write to.
		 * @return True on success.
		 */
		virtual
		bool toJsonVal(QJsonValue &jsonVal) const Q_DECL_OVERRIDE;

	private:
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
		::std::unique_ptr<NoteEntryPrivate> d_ptr;
#else /* < Qt-5.12 */
		QScopedPointer<NoteEntryPrivate> d_ptr;
#endif /* >= Qt-5.12 */
	};

	void swap(NoteEntry &first, NoteEntry &second) Q_DECL_NOTHROW;

	/*!
	 * @brief List of tag entries.
	 */
	class NoteEntryList : public Object, public QList<NoteEntry> {
	public:
		/* Expose list constructors. */
		using QList<NoteEntry>::QList;

		/* Some older compilers complain about missing constructor. */
		NoteEntryList(void);

		NoteEntryList(const QList<NoteEntry> &other);
#ifdef Q_COMPILER_RVALUE_REFS
		NoteEntryList(QList<NoteEntry> &&other);
#endif /* Q_COMPILER_RVALUE_REFS */

		static
		NoteEntryList fromJson(const QByteArray &json, bool *ok = Q_NULLPTR);
		static
		NoteEntryList fromJsonVal(const QJsonValue &jsonVal, bool *ok = Q_NULLPTR);

		virtual
		bool toJsonVal(QJsonValue &jsonVal) const Q_DECL_OVERRIDE;
	};

	/*!
	 * @brief Return a hash value for the supplied tag entry.
	 *
	 * @param[in] key Tag entry to compute hash from.
	 * @param[in] seed Is used to initialise the hash if specified.
	 * @return Hashed tag entry.
	 */
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
	size_t qHash(const NoteEntry &key, size_t seed = 0);
#else /* < Qt-6.0 */
	uint qHash(const NoteEntry &key, uint seed = 0);
#endif /* >= Qt-6.0 */

};

Q_DECLARE_METATYPE(Json::NoteEntry)
Q_DECLARE_METATYPE(Json::NoteEntryList)
