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

#pragma once

#include <QDialog>
#include <QList>
#include <QMap>

#include "src/datovka_shared/identifiers/account_id.h"
#include "src/delegates/tags_delegate.h"
#include "src/models/tags_model.h"
#include "src/tags/tag_assignment.h" /* TagAssignment is used in slots. Q_MOC_INCLUDE */
#include "src/tags/tag_entry.h" /* TagEntry is used in slots. Q_MOC_INCLUDE */

class TagDb; /* Forward declaration. */
class TagItemList; /* Forward declaration. */

namespace Ui {
	class DlgTags;
}

/*!
 * @brief Tags management dialogue.
 */
class DlgTags : public QDialog {
	Q_OBJECT

public:
	/*!
	 * Describes the action the dialogue has performed.
	 */
	enum ReturnCode {
		NO_ACTION, /*!< Nothing happened. */
		ASSIGMENT_CHANGED, /*!< Assignment of tags to messages have changed. */
		TAGS_CHANGED /*!< Actual tags have been deleted or changed. */
	};

private:
	/*!
	 * @brief Constructor.
	 *
	 * @param[in] acntId Account identifier.
	 * @param[in] tagDb Pointer to tag database.
	 * @param[in] msgIdList List of message IDs, empty list if no assignment
	 *                      should be edited.
	 * @param[in] parent Parent widget.
	 */
	explicit DlgTags(const AcntId &acntId, TagDb *tagDb,
	    const QList<qint64> &msgIdList, QWidget *parent = Q_NULLPTR);

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

	/*!
	 * @brief Edit all tags.
	 *
	 * @param[in] acntId Account identifier.
	 * @param[in] tagDb Pointer to tag database.
	 * @param[in] parent Parent widget.
	 * @return Return code.
	 */
	static
	enum ReturnCode editAvailable(const AcntId &acntId, TagDb *tagDb,
	    QWidget *parent = Q_NULLPTR);

	/*!
	 * @brief Edit assigned tags.
	 *
	 * @param[in] acntId Account identifier.
	 * @param[in] tagDb Pointer to tag database.
	 * @param[in] msgIdList List of message IDs.
	 * @param[in] parent Parent widget.
	 * @return Return code.
	 */
	static
	enum ReturnCode editAssignment(const AcntId &acntId, TagDb *tagDb,
	    const QList<qint64> &msgIdList, QWidget *parent = Q_NULLPTR);

private slots:
	/*!
	 * @brief Add tag (insert into database).
	 */
	void addTag(void);

	/*!
	 * @brief Update tag (update data in database).
	 */
	void updateTag(void);

	/*!
	 * @brief Delete tag (delete tag data from database).
	 */
	void deleteTag(void);

	/*!
	 * @brief Assign selected tag(s) to messages (insert into database).
	 */
	void assignSelectedTagsToMsgs(void);

	/*!
	 * @brief Remove selected tag(s) from messages
	 *        (delete message tag records from database).
	 */
	void removeSelectedTagsFromMsgs(void);

	/*!
	 * @brief Remove all tags from messages
	 *        (delete message tag records from database).
	 */
	void removeAllTagsFromMsgs(void);

	/*!
	 * @brief Activate/deactivate buttons on available selection change.
	 */
	void handleAvailableSelectionChange(void);

	/*!
	 * @brief Activate/deactivate buttons on assigned selection change.
	 */
	void handleAssignedSelectionChange(void);

/* Handle database signals. */
	/*!
	 * @brief Update model data when new tag is created.
	 *
	 * @param[in] entries Newly added tag entries.
	 */
	void watchTagInserted(const QList<TagEntry> &entries);

	/*!
	 * @brief Update model data when tags updated.
	 *
	 * @param[in] entries Updated tag entries.
	 */
	void watchTagUpdated(const QList<TagEntry> &entries);

	/*!
	 * @brief Update model data when tags deleted.
	 *
	 * @param[in] ids Deleted tag identifiers.
	 */
	void watchTagDeleted(const QList<int> &ids);

	/*!
	 *  @brief Update model data when tag assignment changes.
	 *
	 *  @param[in] assignments Updated tag assignments.
	 */
	void watchTagAssignmentChanged(const QList<TagAssignment> &assignments);

private:
	/*!
	 * @brief Fill all tag entries into list views from database.
	 *
	 * @param[in] msgIdList List of message IDs.
	 */
	void fillTagsToListViews(const QList<qint64> &msgIdList);

	/*!
	 * @brief Initialises the dialogue.
	 *
	 * @param[in] msgIdList List of message IDs.
	 */
	void initDlg(const QList<qint64> &msgIdList);

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

	/*!
	 * @brief Choose (select) all tags in the list view
	 *        which are assigned in selected messages.
	 */
	void selectAllAssingedTagsFromMsgs(void);

	/*!
	 * @brief Set icon set to buttons.
	 */
	void setIcons(void);

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

	const AcntId m_acntId; /*!< Account identifier. */
	TagDb *m_tagDbPtr; /*!< Tag db pointer. */
	QMap<qint64, TagItemList> m_msgTagItemsMap; /*!< Maps message identifiers to tag entry lists. */

	TagsDelegate m_availableTagsDelegate; /*!< Responsible for painting. */
	TagsModel m_availableTagsModel; /*!< Available tags model. */

	TagsDelegate m_assignedTagsDelegate; /*!< Responsible for painting. */
	TagsModel m_assignedTagsModel; /*!< Assigned tags model. */

	enum ReturnCode m_retCode; /*!< Dialogue return code. */
};
