// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef UPDATELISTMODEL_H
#define UPDATELISTMODEL_H

#include "updateiteminfo.h"
#include "common.h"

#include <QAbstractListModel>

class UpdateListModel : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(bool anyVisible READ anyVisible NOTIFY visibilityChanged)
    Q_PROPERTY(bool isUpdateEnable READ isUpdateEnable NOTIFY isUpdateEnableChanged)
    Q_PROPERTY(QString downloadSize READ downloadSize NOTIFY downloadSizeChanged)

public:
    enum updateRoles {
        Title = Qt::UserRole + 1,
        Version,
        Explain,
        ReleaseTime,
        Checked,
        UpdateStatus,
        IconName,
        Expanded,
        DetailInfos
    };

    explicit UpdateListModel(QObject *parent = nullptr);

    // Basic functionality:
    Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    // 用目标列表整体同步模型内容；当内容与当前一致时不发出任何信号，
    // 避免无意义的 beginResetModel/endResetModel 造成视图重建与滚动跳动
    void syncData(const QList<UpdateItemInfo *> &items);

    void clearAllData();

    Q_INVOKABLE void setChecked(int index, bool checked);

    Q_INVOKABLE void setExpanded(int index, bool expanded);
    Q_INVOKABLE void collapseAll();

    Q_INVOKABLE int getAllUpdateType() const;

    QString getIconName(UpdateType type) const;

    QHash<int, QByteArray> roleNames() const override
    {
        QHash<int, QByteArray> roles;
        roles[Title] = "title";
        roles[Version] = "version";
        roles[Explain] = "explain";
        roles[ReleaseTime] = "releaseTime";
        roles[Checked] = "checked";
        roles[UpdateStatus] = "updateStatus";
        roles[IconName] = "iconName";
        roles[Expanded] = "expanded";
        roles[DetailInfos] = "detailInfos";
        return roles;
    }

    bool anyVisible() const;
    bool isUpdateEnable() const;
    QString downloadSize() const;

    Q_INVOKABLE UpdateType getUpdateType(int index) const;

public Q_SLOTS:
    void refreshDownloadSize();

signals:
    void visibilityChanged();
    void isUpdateEnableChanged();
    void downloadSizeChanged();

private:
    QList<UpdateItemInfo *> m_updateLists;
    QString m_downloadSize;
};
#endif // UPDATELISTMODEL_H
