/*
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 * Authors: zy-yuan1 <zhangyuanyuan1@kylinos.cn>
 *
 */
#ifndef APPWINDOWLISTMODEL_H
#define APPWINDOWLISTMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QIcon>
#include "multitaskviewabstractmodelhandler.h"

class Window;
class AppWindowListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum Role {
        Id = Qt::UserRole + 1,
        Uuid,
        Caption,
        X,
        Y,
        Width,
        Height,
        Minimized,
        DesktopsId,
        ScreenId,
        Icon
    };
    Q_ENUM(Role)

    explicit AppWindowListModel(QObject *parent = nullptr);
    ~AppWindowListModel() = default;

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


    /* for TabletView
     * qml里无法直接读取QMap<int, QVariant>，需要使用iterator，因此使用QVariant::fromValve(window)
     * TODO: TabletView 加载动画优化
     */
    Q_INVOKABLE QVariant getItemData(const QModelIndex &index);

private:
    void connectSignals();
    void loadWindows();
    void addItem(Window window);
    QList<Window> getAllAppWindowList() const;

    QList<Window> m_windowList;

private Q_SLOTS:
    void onAppWindowRemoved(QString windowId);
    void onAppWindowDesktopChanged(QString windowId, int oldDesktopIndex, int newDesktopIndex);
    void onDesktopMoved(int oldDesktopIndex, int newDesktopIndex);
    void onDesktopRemoved(int desktopIndex);
    void onAppWindowAdd(const Window &window);
    void onAppWindowActived();
    void onAppWindowUpdated(const Window &window);
};

#endif // APPWINDOWLISTMODEL_H
