#ifndef TOPWIN8DESKTOP
#define TOPWIN8DESKTOP

#include "topdesktopitemabs.h"
#include <QVariant>
#include <QFrame>

class TopWin8DesktopGroup;
class TMessageBar;
class TLineEdit;
class QLabel;
class TopWin8DesktopPrivate;
class TopWin8Desktop : public QFrame
{
    Q_OBJECT

public:
    enum DockArea{
        DockArea_TopLeft,
        DockArea_Top,
        DockArea_TopRight,
        DockArea_Left,
        DockArea_Center,
        DockArea_Right,
        DockArea_BottomLeft,
        DockArea_Bottom,
        DockArea_BottomRight,
        DockArea_Position,
    };

    enum Alignment{
        Alignment_Horizontal,
        Alignment_Vertical,
        Alignment_None,
    };

    enum Popup{
        Popup_Default,
        Popup_Menu,
        Popup_Left,
        Popup_Right,
        Popup_Top,
        Popup_Bottom,
        Popup_Center,
    };

    explicit TopWin8Desktop(QWidget *iParent = NULL);
    ~TopWin8Desktop();

signals:
    void urlRequested(const QString &iUrlStr, const QVariant &iConfigVar);
    void dataChanged();
    void sizeChanged();

public slots:
    void doLayout();

    void setConfigData(const QVariantMap &iConfigDataVarMap);
    QVariantMap configData();
    void setConfig(const QString &iKeyStr, const QVariant &iValueVar);
    QVariant config(const QString &iKeyStr) const;

    void onDataChanged();

    void setDataModified(bool iModifedBol = true);
    bool isDataModified() const;

    // Size 、SizeString 和 Row Col的转换
    QSize sizeStr2QSize(const QString &iSizeStr);
    QString rowColStr2SizeStr(const QString &iRowStr, const QString &iColStr);

    TopDesktopItemAbs::State state() const;
    void setState(TopDesktopItemAbs::State iState);

    int colSpan() const;
    int rowSpan() const;

    void setModuleClickType(TopDesktopItemAbs::ClickType iClickType);
    TopDesktopItemAbs::ClickType moduleClickType() const;

    QString selectImageDialog();

    void setColorList(TComboBox *iBackgroundCombox);
    void setLabelAdjustSize(QLabel *label);

    QString ttr(const QString &iStr);

private slots:
    void onDoSearch(const QString &iTextStr);

    // 桌面右击菜单
    void refresh();
    void editDesktop();
    void saveDesktop();
    void showConfigDialog();
    void addGroup();

    void setBackground();

    void deleteGroup(TopWin8DesktopGroup* iGroup);

protected:
    QList<TopWin8DesktopGroup *> createGroups(const QVariantList &iGroupsVarLst);
    TopWin8DesktopGroup *createGroup(const QVariant &iGroupVar);
    const QScopedPointer<TopWin8DesktopPrivate> d_ptr;

    void mouseReleaseEvent(QMouseEvent *iEvent);
    void wheelEvent(QWheelEvent *iEvent);
    void dragEnterEvent(QDragEnterEvent *iEvent);
    void dropEvent(QDropEvent *iEvent);
    void resizeEvent(QResizeEvent *iEvent);

private:
    QPoint classPointToCenterWidgetPoint(QPoint iPoint);
    DockArea str2DockArea(const QString &iSequenceStr);
    Alignment str2Alignment(const QString &iAlignmentStr);
    Alignment alignmentAtDockArea(DockArea iDockArea);
    TopDesktopItemAbs::ClickType str2ModuleClickType(const QString &iClickTypeStr) const;
    QString moduleClickType2Str(TopDesktopItemAbs::ClickType iClickType) const;
    int bottomYAtDockArea(DockArea iDockArea, QList<TopWin8DesktopGroup *> &iGroupsLst, int iStartYInt);
    int rightXAtDockArea(DockArea iDockArea, QList<TopWin8DesktopGroup *> &iGroupsLst, int iStratXInt);

    void layoutDockAreaGroup(QSize iMinWgtSize);
    void putGroupsAtDockArea(DockArea iDockArea, QList<TopWin8DesktopGroup *> &iLayoutedGroupsLst, QPoint iAlignmentPosition);
    QList<TopWin8DesktopGroup *> groupsOrderByOneDockArea(DockArea iDockArea, QList<TopWin8DesktopGroup *> &iLayoutedGroupsLst);

    QSize getDockAreaSize(Alignment iAlignment, const QList<TopWin8DesktopGroup *> &iGroupsLst);
    QSize calcDesktopSize();
    QList<TopWin8DesktopGroup *> getDockAreaGroupList(DockArea iDockArea);
    void layoutDockAreaGroup();

private:
    Q_DISABLE_COPY(TopWin8Desktop)
    Q_DECLARE_PRIVATE(TopWin8Desktop)
};
#endif // TOPWIN8DESKTOP