#ifndef BLASTOPEPAGE_H
#define BLASTOPEPAGE_H

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLayout>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QProgressBar>
#include <QPushButton>
#include <QSet>
#include <QStandardItemModel>
#include <QWebEnginePage>
#include <QWebEngineView>
#include <QWidget>

#include "./serial/serialtool.h"
#include "PageWidget.h"
#include "countdownwidget.h"
#include "fireWidget/firingwidget.h"  // 包含 firingWidget 头文件
#include "hprojectdao.h"

// 自定义结构体
struct ProgressBarTriple {
    QProgressBar *bar1;
    QProgressBar *bar2;
    QProgressBar *bar3;

    // 构造函数
    ProgressBarTriple(QProgressBar *b1, QProgressBar *b2, QProgressBar *b3) : bar1(b1), bar2(b2), bar3(b3) {}
};
namespace Ui {
class BlastOpePage;
}

class BlastOpePage : public QWidget {
    Q_OBJECT

   public:
    explicit BlastOpePage(QWidget *parent = nullptr);
    ~BlastOpePage();
   private slots:
    void PageChanged(int page);
    void onComboBoxIndexChanged(int index);
    void updateProgressBar(int value, int row);
    void onFiringStageUpdated(int status, int row);
    void handleUpdateOpButton(int status, int row);
    void showCountDownWidget(QString uuid, const QString &topic, const QString &message);
    void handlerUpdateProjectStatus(QString uuid, const QString &status);
    void destroyFiringWidget(const QString &uuid, int row);
    void on_btnSelect_clicked();
    void onItemCheckboxChanged(QStandardItem *item);
    void setBatchBlastTrigger(QString uuid);
    void showCountDownForBatchBlast();
    void triggerBatchFiringBlast();
    void destroyBatchFiringWidget(const QString &uuid, int row);

   private:
    void handleFeaturePermission(QWebEnginePage *page, const QUrl &securityOrigin, QWebEnginePage::Feature feature);
    void RefreshData();
    void loadDataAndDrawTable(int currentPage, int pageSize);
    void InitFace();
    void closeWebViewAndRestoreUI();
    void parseJsonString(const QJsonDocument &jsonDoc);
    void onUrlChanged(const QUrl &newUrl);
    void initPagination();
    QJsonObject sendPostRequest(const QUrl &url, const QJsonObject &data);
    QJsonObject getMetaInfo();
    void handleSingleBlastButtonClicked(int row, QPushButton *button);
    bool checkUuidsSame();

   private:
    Ui::BlastOpePage *ui;
    QList<HeaderInfo> headers;
    QStandardItemModel *model;
    QMap<QString, firingWidget *> firingWidgetByUuid;
    QWebEngineView *view;
    QVBoxLayout *layout;
    QString certifyId;
    PageWidget *pageWidget;
    QJsonArray dataArray;
    HProjectDao dao;
    QVector<ProgressBarTriple> progressBars;  // 用于保存每行的两个进度条指针
    QMap<int, QString> uuidMap;               // 用于存储行号和对应的 uuid
    QMap<QString, firingWidget *> uuidWidgetSMap;
    SerialTool *serialTool;
    QSet<QString> selectedUuids;
    QMetaObject::Connection connectionItem;
    int pageSize;     // 每页显示的记录数量
    int currentPage;  // 当前页面
    int totalCount;
    QString stopBlastButtonTxt = "取消起爆";
    QString startBlastButtonTxt = "开启起爆";
};

#endif  // BLASTOPEPAGE_H
