// SPDX-FileCopyrightText: 2020 - 2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause

#ifndef SELECTGROUPMODEL_H
#define SELECTGROUPMODEL_H

#include <QAbstractListModel>
#include <QUrl>
#include <QSet>
#include <QJsonArray>

class SelectGroupModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(bool exclusive READ exclusive WRITE setExclusive NOTIFY exclusiveChanged)
    Q_PROPERTY(QJsonArray selectedValues READ selectedValues NOTIFY selectedValuesChanged)
    Q_PROPERTY(int selectedCount READ selectedCount NOTIFY selectedValuesChanged)

public:
    enum Role { SelectedRole = Qt::UserRole + 1, ValueRole, NameRole, DescriptionRole, IconRole };

    explicit SelectGroupModel(QObject *parent = nullptr);

    int rowCount(const QModelIndex &parent) const override;
    QHash<int, QByteArray> roleNames() const override;
    QVariant data(const QModelIndex &index, int role) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;

    bool exclusive() const;
    void setExclusive(bool exclusive);
    QJsonArray selectedValues() const;
    int selectedCount() const;

public slots:
    void clear();
    void append(const QJsonValue &item);

signals:
    void exclusiveChanged();
    void selectedValuesChanged();

private:
    class Item
    {
    public:
        explicit Item(const QJsonValue &_value);
        bool selected() const;
        const QJsonValue &value() const;
        bool setValue(const QJsonValue &value);
        QString name() const;
        bool setName(const QString &name);
        QString description() const;
        bool setDescription(const QString &description);
        QUrl icon() const;
        bool setIcon(const QUrl &icon);

    private:
        bool _selected;
        QJsonValue _value;
        QString _name;
        QString _description;
        QUrl _icon;
    };

    bool _exclusive;
    QList<Item> _items;
    QSet<int> _selectedItems;

    bool updateSelected(int iItem, bool selected);
};

#endif // SELECTGROUPMODEL_H
