#ifndef ALARMEVENTDATAVIEW_H
#define ALARMEVENTDATAVIEW_H

#include "alarmEventUtils.h"
#include <QWidget>
#include <QTableView>
#include <QAbstractTableModel>
#include <QStyledItemDelegate>

class QTimer;
class AlarmEventDataModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    enum Column
    {
        Index,
        ReceiveTime,
        SOETime,
        Station,
        Bay,
        Description,
        Type,
        Severity,
        Status,
        Operation,
    };

    struct RowData
    {
        QVector<QVariant> values;
    };

    struct SectionData
    {
        int width = -1;
        Column column;
        QString text;

        SectionData(QString t, int w, Column c)
            :text(t)
            ,column(c)
            ,width(w){}
    };

    explicit AlarmEventDataModel(AlarmDataMode mode, QObject* parent = nullptr);
    ~AlarmEventDataModel();

    //QAbstractTableModel接口实现
    QModelIndex index(int row, int column, const QModelIndex& parent) const override;
    QVariant data(const QModelIndex& index, int role) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    //bool setData(const QModelIndex& index, const QVariant &value, int role = Qt::EditRole) override;
    int rowCount(const QModelIndex& parent = QModelIndex()) const override;
    int columnCount(const QModelIndex& parent = QModelIndex()) const override;
    Qt::ItemFlags flags(const QModelIndex& index) const override;

    //自定义功能函数
    const QVector<SectionData> headerData() const {return m_headerData;}
    void setMode(AlarmDataMode mode);
    void setMaxRealTimeEvents(int value);
    const int getMaxRealTimeEvents() const {return m_maxRealTimeEvents;}
    void setFilter(const AlarmEventDataFilter& filter);
    void refresh();
    void confirmEvent(const QModelIndex);
    void confirmCurPageEvents();

    bool setCurrentPage(int);
    int currentPage() const;
    void previousPage();
    void nextPage();
    void firstPage();
    void lastPage();

signals:
    void receivedNewAlarm(const EventData& event);
    void loadDataError(const QString& error);
    void syncDataStatus(const PaginationInfo&);

private slots:
    void onTimeoutSimulateData();
    void onRealTimeEventReceived(const EventData& event);
    void onHistoricalEventsReceived(const QList<EventData>& events);
    void onHistoricalQueryError(const QString&);
    void onConfirmEventsResult(bool success, const QString& mesg, const QStringList& successUuids, QVariant context);

private:
    void iniHeaderData();
    void applyFilter();
    void updatePaginationInfo();
    int findEventDataIndexById(const QString& eventId);
    void updateEventData(int index, const EventData& updatedEvent);
    void updateCurPageData();

    QVector<EventData> m_allEvents; //所有事件
    QVector<EventData> m_filteredEvents; //过滤后的事件
    QVector<EventData> m_displayEvents; //当前(页)展示的事件
    QVector<SectionData> m_headerData;
    PaginationInfo m_paginationInfo;

    QTimer* m_simulatedDataTimer;

    AlarmDataMode m_dataMode;
    AlarmEventDataFilter m_currentFilter;
    int m_maxRealTimeEvents;
    quint64 m_lastRequestId; //请求ID，在实例化的不同对象调用某同一个对象(此处是AlarmEventDataService)的某个函数并触发其emit某个signal进而触发自身的某个slot时做判别用

    //QMap<qint64, QVector<QModelIndex>> m_confirmedEvents;
};

class AlarmEventDataDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit AlarmEventDataDelegate(QTableView* view, QObject* parent = nullptr);
    ~AlarmEventDataDelegate();

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override;

signals:
    void confirmBtnClicked(const QModelIndex& index);
    void replayBtnClicked(const QModelIndex& index);

private:
    struct ControlButton
    {
        QRect rect;
        QColor normalBgColor;
        QColor hoverBgColor;
        QColor pressedBgColor;
        QColor borderColor;
        QColor textColor;
        QFont textFont;
        int borderWidth;
        int borderRaius;
        QString text;
        //int mouseState = 0; //0Normal,1Hover,2Pressed
    };

    QTableView* m_tableView;
    QPoint m_mousePositon;
    bool m_buttonColumnIsPress;
    ControlButton m_btnConfirm;
    ControlButton m_btnReplay;
};

class QVBoxLayout;
class AlarmEventDataView : public QWidget
{
    Q_OBJECT

public:
    AlarmEventDataView(AlarmDataMode mode, QWidget* parent = nullptr);
    ~AlarmEventDataView();

    AlarmEventDataModel* model() const { return m_tableModel; }

    //void setModelMode(AlarmDataMode, int maxRealTimeEvents = 5);

public slots:
    void onBtnClicked_Confirm(const QModelIndex& index);
    void onBtnClicked_Replay(const QModelIndex& index);

private:
    QTableView* m_tableView;
    AlarmEventDataModel* m_tableModel;
    AlarmEventDataDelegate* m_delegate;
    QVBoxLayout* m_vLayout;
};

#endif
