/*
  SPDX-FileCopyrightText: 2011 Tobias Koenig <tokoe@kde.org>

  SPDX-License-Identifier: LGPL-2.0-or-later
*/

#pragma once

#include "mailcommon_export.h"
#include "mailfilter.h"

#include <Akonadi/Item>
#include <Akonadi/ServerManager>

#include <QObject>

namespace MailCommon
{
class FilterActionDict;
/*!
 * \class MailCommon::FilterManager
 * \inmodule MailCommon
 * \inheaderfile MailCommon/FilterManager
 *
 * \brief A wrapper class that allows easy access to the mail filters
 *
 * This class communicates with the mailfilter agent via DBus.
 */
class MAILCOMMON_EXPORT FilterManager : public QObject
{
    Q_OBJECT

public:
    /*!
     */
    ~FilterManager() override;
    /*!
     * Describes the list of filters.
     */
    enum FilterSet {
        NoSet = 0x0,
        Inbound = 0x1,
        Outbound = 0x2,
        Explicit = 0x4,
        BeforeOutbound = 0x8,
        AllFolders = 0x16, ///< Apply the filter on all folders, not just inbox
        All = Inbound | BeforeOutbound | Outbound | Explicit | AllFolders
    };

    /*!
     * Returns the global filter manager object.
     */
    static FilterManager *instance();

    /*!
     * Returns whether the filter manager is in a usable state.
     */
    [[nodiscard]] bool isValid() const;

    /*!
     * Checks for existing filters with the \a name and extend the
     * "name" to "name (i)" until no match is found for i=1..n
     */
    [[nodiscard]] QString createUniqueFilterName(const QString &name) const;

    /*!
     * Returns the global filter action dictionary.
     */
    static FilterActionDict *filterActionDict();

    /*!
     * Shows the filter log dialog.
     *
     * This is used to debug problems with filters.
     */
    void showFilterLogDialog(qlonglong windowId);

    /// Apply filters interface

    /*!
     * Applies filter with the given \a identifier on the message \a item.
     * Returns \\ true on success, \\ false otherwise.
     */
    void filter(const Akonadi::Item &item, const QString &identifier, const QString &resourceId) const;

    /*!
     * Process given message item by applying the filter rules one by
     * one. You can select which set of filters (incoming or outgoing)
     * should be used.
     *
     * \a item The message item to process.
     * \a set Select the filter set to use.
     * \a account \\ true if an account id is specified else \\ false
     * \a accountId The id of the resource that the message was retrieved from
     */
    void filter(const Akonadi::Item &item, FilterSet set = Inbound, bool account = false, const QString &resourceId = QString()) const;

    /*!
     * Process all messages in given collection by applying the filters rules one
     * by one. You can select which set of filters (incoming or outgoing)
     * should be used.
     */
    void filter(const Akonadi::Collection &collection, FilterSet set = Explicit) const;

    /*!
     * Apply specified filters on all messages in given collection
     */
    void filter(const Akonadi::Collection &collection, const QStringList &listFilters) const;

    /*!
     * Process all messages in given collections by applying the filters rules one
     * by one. You can select which set of filters (incoming or outgoing)
     * should be used.
     */
    void filter(const Akonadi::Collection::List &collections, FilterSet set = Explicit) const;

    /*!
     * Apply specified filters on all messages in given collection
     */
    void filter(const Akonadi::Collection::List &collections, const QStringList &listFilters, FilterSet set = Explicit) const;

    /*!
     * Process given \a messages by applying the filter rules one by
     * one. You can select which set of filters (incoming or outgoing)
     * should be used.
     *
     * \a item The message item to process.
     * \a set Select the filter set to use.
     */
    void filter(const Akonadi::Item::List &messages, FilterSet set = Explicit) const;

    void filter(const Akonadi::Item::List &messages, SearchRule::RequiredPart requiredPart, const QStringList &listFilters) const;

    /// Manage filters interface

    /*!
     * Appends the list of \a filters to the current list of filters and
     * write everything back into the configuration. The filter manager
     * takes ownership of the filters in the list.
     */
    void appendFilters(const QList<MailCommon::MailFilter *> &filters, bool replaceIfNameExists = false);

    /*!
     * Removes the given \a filter from the list.
     * The filter object is not deleted.
     */
    void removeFilter(MailCommon::MailFilter *filter);

    /*!
     * Replace the list of filters of the filter manager with the given list of \a filters.
     * The manager takes ownership of the filters.
     */
    void setFilters(const QList<MailCommon::MailFilter *> &filters);

    /*!
     * Returns the filter list of the manager.
     */
    [[nodiscard]] QList<MailCommon::MailFilter *> filters() const;

    /*!
     * Should be called at the beginning of an filter list update.
     */
    void beginUpdate();

    /*!
     * Should be called at the end of an filter list update.
     */
    void endUpdate();

    [[nodiscard]] QMap<QUrl, QString> tagList() const;

    [[nodiscard]] bool initialized() const;

    void cleanup();
private Q_SLOTS:
    MAILCOMMON_NO_EXPORT void slotServerStateChanged(Akonadi::ServerManager::State);
    MAILCOMMON_NO_EXPORT void slotFinishedTagListing(KJob *);
    MAILCOMMON_NO_EXPORT void slotReadConfig();
    MAILCOMMON_NO_EXPORT void updateTagList();

    MAILCOMMON_NO_EXPORT void slotTagAdded(const Akonadi::Tag &);
    MAILCOMMON_NO_EXPORT void slotTagChanged(const Akonadi::Tag &);
    MAILCOMMON_NO_EXPORT void slotTagRemoved(const Akonadi::Tag &);

Q_SIGNALS:
    /*!
     * This signal is emitted whenever the filter list has been updated.
     */
    void filtersChanged();

    void tagListingFinished();

    void loadingFiltersDone();

private:
    MAILCOMMON_NO_EXPORT FilterManager();

    class FilterManagerPrivate;
    FilterManagerPrivate *const d;
};
}
