/*
 * 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 DESKTOPLISTMODEL_H
#define DESKTOPLISTMODEL_H

#include <QObject>
#include <QList>
#include <QRect>
#include <QVariantList>
#include <QString>
#include <QAbstractListModel>
#include <QMetaType>
#include "multitaskviewabstractmodelhandler.h"

class Window;
class Desktop;


class DesktopListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum Role {
        Id = Qt::UserRole + 1, /* desktop index, 从 1 开始计数，易于和界面显示对应 */
        Uuid, /* desktop uuid */
        CustomName, /* 用户自定义名称 */
        ItemType /*DesktopType*/
    };
    Q_ENUM(Role)

    explicit DesktopListModel(QObject *parent = nullptr);
    ~DesktopListModel() = 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;
    bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
    bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
    bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
                              const QModelIndex &destinationParent, int destinationChild) override;

    bool rowIsValid(int row) const;
    void connectSignals();
    void init();

private:
    QStringList getDesktopNames();

    QList<Desktop> m_desktopList;
    /* 最大虚拟桌面数量为15: 15个工作区 或 14个工作区+新建工作区*/
    static constexpr int MAX_DESKTOP_COUNT = 15;

signals:
    /* 用户手动添加工作区时，需要额外执行动画使最后一个工作区（或新建工作区）显示完整；删除工作区则不需要 */
    void desktopAppended(int desktopIndex);


private slots:
    void onDesktopAppend(int desktopIndex);
    void onDesktopRemoved(int desktopIndex);
    void onDesktopMoved(int srcDesktopIndex, int destDesktopIndex);
};

#endif // DESKTOPLISTMODEL_H
