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

#include "src/datovka_shared/identifiers/account_id.h"
#include "src/datovka_shared/json/basic.h" /* Json::Int64StringList is used in slots. Q_MOC_INCLUDE */
#include "src/json/note_assignment.h" /* Json::NoteAssignmentList is used in slots. Q_MOC_INCLUDE */
#include "src/json/note_entry.h" /* Json::NoteEntryList is used in slots. Q_MOC_INCLUDE */

namespace Ui {
	class DlgNote;
}
class TagContainer; /* Forward declaration. */

/*!
 * @brief Note dialogue.
 */
class DlgNote : public QDialog {
	Q_OBJECT

private:
	/*!
	 * @brief Constructor.
	 *
	 * @param[in] acntId Account identifier.
	 * @param[in] tagCont Pointer to tag container.
	 * @param[in] dmId Message id.
	 * @param[in] parent Parent widget.
	 */
	explicit DlgNote(const AcntId &acntId, TagContainer *tagCont,
	    qint64 dmId, QWidget *parent = Q_NULLPTR);

public:
	/*!
	 * @brief Destructor.
	 */
	~DlgNote(void);

	/*!
	 * @brief Edit note - modal dialogue.
	 *
	 * @param[in] acntId Account identifier.
	 * @param[in] tagCont Pointer to tag container.
	 * @param[in] dmId Message id.
	 * @param[in] parent Parent widget.
	 */
	static
	void editNote(const AcntId &acntId, TagContainer *tagCont,
	    qint64 dmId, QWidget *parent = Q_NULLPTR);

	/*!
	 * @brief Edit note - non-modal dialogue, one dialogue per message.
	 *
	 * @param[in] acntId Account identifier.
	 * @param[in] tagCont Pointer to tag container.
	 * @param[in] dmId Message id.
	 * @param[in] parent Parent widget.
	 */
	static
	void editNoteNonModal(const AcntId &acntId, TagContainer *tagCont,
	    qint64 dmId, QWidget *parent = Q_NULLPTR);

public Q_SLOTS:
	/*!
	 * @brief Check if a worker is working on the background and show
	 *     a dialogue whether the user wants to close this dialogue.
	 *
	 * @note Used instead of closeEvent().
	 *     https://stackoverflow.com/questions/13129380/show-a-dialog-before-closing-the-program-in-qt
	 *
	 * @param[in] resultCode Dialog result code.
	 */
	virtual
	void done(int resultCode) Q_DECL_OVERRIDE;

private Q_SLOTS:
	/*!
	 * @brief Add note (insert into db) or update if already exists and
	 *     close the dialogue.
	 */
	void assignNoteAndClose(void);

	/*!
	 * @brief Delete note (delete note from db) and close the dialogue.
	 */
	void deleteNoteAndClose(void);

	/*!
	 * @brief Calling when text of note changed.
	 */
	void onTextChanged(void);

/* Handle database signals. */
	/*!
	 * @brief Update data when new notes are created.
	 *
	 * @param[in] entries Newly added note entries.
	 */
	void watchNotesInserted(const Json::NoteEntryList &entries);

	/*!
	 * @brief Update data when notes updated.
	 *
	 * @param[in] entries Updated note entries.
	 */
	void watchNotesUpdated(const Json::NoteEntryList &entries);

	/*!
	 * @brief Update data when note deleted.
	 *
	 * @param[in] ids Deleted note identifiers.
	 */
	void watchNotesDeleted(const Json::Int64StringList &ids);

	/*!
	 *  @brief Update data when note assignment changes.
	 *
	 *  @param[in] assignments Updated note assignments.
	 */
	void watchNoteAssignmentChanged(const Json::NoteAssignmentList &assignments);

	/*!
	 * @brief Action when tag/note client is connected.
	 */
	void watchTagContConnected(void);

	/*!
	 * @brief Action when tag/note client is disconnected.
	 */
	void watchTagContDisconnected(void);

	/*!
	 * @brief Action when tag/note container is reset.
	 */
	void watchTagContReset(void);

private:
	/*!
	 * @brief Set button visibility according to the existence of a note.
	 *
	 * @param[in] existentNote True if note to give message exists.
	 */
	void setButtons(bool existentNote);

	/*!
	 *  @brief Store note and generate error dialogue when fails.
	 *
	 *  @return True on success.
	 */
	bool assignNote(void);

	/*!
	 *  @brief Delete note and generate error dialogue when fails.
	 *
	 *  @return True on success.
	 */
	bool deleteNote(void);

	/*!
	 * @brief Connect database container signals to appropriate slots.
	 */
	void databaseConnectActions(void);

	Ui::DlgNote *m_ui; /*!< UI generated from UI file. */

	const AcntId m_acntId; /*!< Account identifier. */
	const qint64 m_dmId; /*!< Message identifier. */
	TagContainer *m_tagContPtr; /*!< Tag database/client pointer. */
	Json::NoteEntry m_editedNoteEntry; /*!< Edited note entry. */
	bool m_interruptClosing; /*!< Whether to ask before closing if text has changed. */

	QSize m_dfltSize; /*!< Remembered dialogue default size. */
};
