#ifndef DATAPANEL_H
#define DATAPANEL_H

#include <QDialog>
#include "global.h"
#include "dataPanel/dpGlobals.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class panelToolWidget;
}
QT_END_NAMESPACE

class CustomMenu;
class QBoxLayout;
class QScrollArea;
class PanelToolWidget;
class PanelConfigurationWidget;
class CustomBorderContainer;

class PanelToolWidget : public QWidget
{
    Q_OBJECT

public:
    PanelToolWidget(QWidget *parent = nullptr);
    ~PanelToolWidget();

    friend class DataPanel;

private:
    Ui::panelToolWidget*  ui;
};

class DataPanel : public QDialog
{
    Q_OBJECT

public:
    DataPanel(QWidget *parent = nullptr, DataPanelType type = lineChart);
    ~DataPanel();

    /**
     * 设置内容widget时的插入方式，widget的实际大小和panel的大小设定可能不一致，因此默认采用scrollarea来进行承载
     * AutoScrollArea 根据widget的类型来自动执行，如果widget输入scrollarea，则直接使用，若不是，则创建一个scrollarea来承载
     * ForceScrollArea 无论widget什么类型，都强行创建一个scrollarea进行承载
     * ForceNoScrollArea 无论widget什么类型，都不使用scrollarea进行承载
     */
    enum eInsertMode
    {
        AutoScrollArea,
        ForceScrollArea,
        ForceNoScrollArea
    };

    void setWidget(QWidget*, eInsertMode InsertMode = ForceNoScrollArea);
    QWidget* takeWidget();

    void setName(const QString&);
    const QString& getName();

    DataPanelType getType();

    void setInitialSize(const QSize&);
    void setDisplayAreaSize(const QSize&);
    void resizeByRatio(double, double); //通过缩放比例做resize，在所属dasboard的resizeEvent中调用
    void resizeByNewSize(const QSize&); //通过新的尺寸(所在区域)做resize，当所属dashboard被激活(显示)时调用
    void setDateTime(const QDateTime&);
    void setTimeRange(TimeUnit);

    void configurationComplete();

protected:
    bool event(QEvent*);
    /*void paintEvent(QPaintEvent*);
    void keyPressEvent(QKeyEvent*);
    void mousePressEvent(QMouseEvent*);
    void enterEvent(QEnterEvent*);
    void leaveEvent(QEvent*);
    void resizeEvent(QResizeEvent*);*/

public slots:
    void onToolBtnClicked_setting();
    void onToolBtnClicked_fullScreen();
    void onToolBtnClicked_menu();

    void onAction_remove();
    void onAction_moveToFront();
    void onAction_moveToBack();

    void onAboutToHide_toolMenu();

    void onSignal_configure();

    void onSignal_dataUpdated(const QString& dataKey, const QVariant& data);

signals:
    void sgl_remove(const QString&);
    void sgl_openCofigurationDialog(DataPanel*);

private:
    void setupScrollArea();
    void autoSetGeometry(); //在缩放和移动时以其它panel和parent的border为依据自动调整
    void createDataWidget();

    bool             m_bMouseEnter;
    bool             m_bConfigurationComplete;
    QString          m_strName;
    QSize            m_displayAreaSize;
    QRect            m_curGeometry;
    CustomMenu*      m_pToolMenu;
    QWidget*         m_pContentWidget;
    QBoxLayout*      m_pLayout;
    QScrollArea*     m_pScrollArea;
    PanelToolWidget* m_pToolWidget;
    DataPanelType    m_type;
    PanelConfigurationWidget*  m_pConfigurationWidget;
    CustomBorderContainer*     m_pCustomBorderContainer;

    friend class               dpConfigurationDialog;
    configurationResults       m_cofigurationResults;
};

#endif
