#ifndef PLATFORMWINDOW_H
#define PLATFORMWINDOW_H

#include <QQuickView>
#include "peony-dbus-service.h"

class UkuiWindowHelper;

namespace Peony
{
class  DesktopItemProxyModel;
class AdvancedDesktopItemModel;
}

class PlatformWindow : public QQuickView
{
    Q_OBJECT
public:
    explicit PlatformWindow(int id, QScreen* screen, QWindow *parent = nullptr);
    bool event(QEvent *event) override;
    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();
    QScreen *bindScreen() {
        return m_screen;
    }
    QScreen *destoryScreen() {
        return m_screen = nullptr;
    }

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, bool forceUpdate = true);
    void showHiddenFile();

    void showStandardFile();

    bool fileRemoved(const QString &uri);
    bool filesRemoved(const QStringList &uris);
    bool idChanged(int id, int oldId, const QStringList &uris);

    void drop(const QStringList &selectedUris, Qt::DropAction action, int insertRow, const QPoint &pos);
    bool dragFromOtherScreen(QStringList selectedUris, int insertRow, const QPoint &offPos);

private:
    bool m_initData = false;
    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;
    QScreen *m_screen = nullptr;
#ifdef KY_SDK_WAYLANDHELPER
    UkuiWindowHelper *m_wmHelper = nullptr;
#endif
};

#endif // PLATFORMWINDOW_H
