#ifndef PLATFORMWINDOW_H
#define PLATFORMWINDOW_H

#include <QQuickView>
#include <KWayland/Client/plasmashell.h>
#include <QAbstractItemModel>
#include "peony-dbus-service.h"
namespace KWayland
{
namespace Client
{
class PlasmaShell;
class Surface;
}
}
namespace Peony
{
class  DesktopItemProxyModel;
class AdvancedDesktopItemModel;
}

class PlatformWindow : public QQuickView
{
    Q_OBJECT
public:
    explicit PlatformWindow(int id, QScreen* screen, QWindow *parent = nullptr);
    void rowsInserted(const QModelIndex &parent, int first, int last);
    bool isFull();
    void ajustIconsOnScreenSize();

    bool isInvalidPoint(const QPoint &point);
    QPoint invalidPoint() const;
    void relayoutExsitingItems(const QStringList &uris);
    void setFileMetaInfoPos(const QString &uri, const QPoint &pos);
    const QStringList getAllFileUris();
    /*!
     * \brief findNextEmptyGridPos
     * 获取下一个空网格所在的位置
     * \return
     *
     */
    QPoint findNextEmptyGridPos(const QPoint &startGridPos = QPoint());
    /*!
     * \brief getGridPosFromMousePos
     * 根据当前鼠标事件传入的位置返回网格的位置
     * \param mousePos
     * \return
     * 鼠标位置对应的网格位置
     */
    QPoint getGridPosFromMousePos(const QPoint &mousePos);

    /*!
     * \brief gridPosLesserThan
     * 从左上到右下比较两个网格位置的顺序
     * \param leftPos
     * \param rightPos
     * \return
     */
    bool gridPosLesserThan(const QPoint &leftPos, const QPoint &rightPos);

    void checkItemsOver();
    QPoint checkGridPos(const QPoint pos);
    void setSortType(int sortType);
    void setSortOrder(int sortOrder);
    void setId(int id);
    int id();
    int dataCount() const;
    bool checkEmptyPositon();

Q_SIGNALS:
    void viewUpdated();

public Q_SLOTS:
    void updateWindowGeometry();
    void reloadData();
    void loadData();
    void invalidate();
    void updateView();
    /**
     * @brief layoutItems
     * 自动排列
     * @param closest
     * @return
     * 返回自动排列的uri列表
     */
    QStringList layoutItems(bool closest = false);

    /**
     * @brief relayoutExsitingItemsAndUpdate
     * 设置自动排列后，
     * @param uris
     */
    void relayoutExsitingItemsAndUpdate(const QStringList uris);
    void showHiddenFile();

    void showStandardFile();

    bool fileRemoved(const QString &uri);
    bool fileCreated(int id, int oldId, const QString &uri);
    void drop(const QStringList &selectedUris, Qt::DropAction action, int insertRow, const QPoint &pos);
    bool dragFromOtherScreen(QStringList selectedUris, int insertRow, const QPoint &offPos);

protected:
    void initWindow();

protected:
   // void resizeEvent(QResizeEvent *ev) override;
    bool m_isWayland;

private:
    bool m_panelTakefocus;
    bool m_keepAbove;
    bool m_initData = false;
    KWayland::Client::PlasmaShellSurface::Role m_waylandSurfaceRole;
    KWayland::Client::PlasmaShellSurface *m_plasmaShellSurface = nullptr;
    KWayland::Client::Surface *m_surface = nullptr;
    KWayland::Client::PlasmaShell *m_plasmaShellInterface = nullptr;
    Peony::DesktopItemProxyModel *m_proxyModel = nullptr;
    Peony::AdvancedDesktopItemModel *m_model = nullptr;
    int m_availableColumnCount;
    int m_availableRowCount;
    QSize m_gridSize;
    QMargins m_margins;
    QHash<QString, QPoint> m_resolutionItemPosHash;
    QHash<QString, QPoint> m_itemPosHash;
    QStringList m_autoArrange;
    QStringList m_storageBox;
    Peony::PeonyDbusService *m_peonyDbusSer = nullptr;
};

#endif // PLATFORMWINDOW_H
