#ifndef FIRINGWIDGET_H
#define FIRINGWIDGET_H

#include <QJsonDocument>
#include <QJsonObject>
#include <QMqttSubscription>
#include <QWidget>

#include "../backendapimanager.h"
#include "../blastRecord/hblastrecord.h"
#include "../blastRecord/hblastrecorddao.h"
#include "../blastRecordDet/hblastrecorddetdao.h"
#include "../blastRegRecord/hblastregrecorddao.h"
#include "../databasemanager.h"
#include "../des3encryption.h"
#include "../logger.h"
#include "../mqtt/mqttclient.h"
#include "../serial/serialtool.h"
#include "navprogress.h"

/* ref: 起爆器端协议
public class Reply{
   private int status=0;
   private String msg = "”;
   private int regcount = 0;
   private int errorCount =0;
   private int busVoltage = 0;
   private int buscurrent =0;
   private int stage = 1;//起爆检测中，起爆检测完成，充电中，充电完成， 起爆中，起爆完成
}
*/
namespace Ui {
class firingWidget;
}

// Error status codes for device and bus issues
class firingWidget : public QWidget {
    Q_OBJECT

   public:
    explicit firingWidget(const int &row, const bool &select, const QString &uuid = "", QWidget *parent = nullptr);
    ~firingWidget();
    void sendCancelFiringMsg();
    void testOnBlastSuccess(const QJsonObject &data);
    void startBlasting();

   public slots:
    void onCountdownFinished(const QString &topic, const QString &message);

   private slots:
    void on_pushButton_2_clicked();
    void on_sendTest_4_clicked();
    void handleProjectFiringMqttMessage(const QMqttMessage &qmessage);

    void onMqttConnected();
    void onMqttDisconnected();
    // 双键按下
    void onButtonPressedReceived(const QString &topic, const QString &message);
    void handleFiringStageChanged(int newStage);
    void onBlastSuccess(const QJsonObject &data);

   signals:
    void progressChanged(int value, int row);
    void lastStageChanged(int newStage);
    void projSafeCheckSuccess(QString projectUuid);
    void updateBlastStatus(int status, int row);
    void batchFiringSignal(QString uuid);
    void updateProjectStatus(QString uuid, QString status);
    void updateData(const QJsonObject &jsonObj);
    void safeChecked(const QJsonObject &jsonObj);
    void countdown(QString uuid, const QString &topic, const QString &message);
    void closeFiring(QString uuid, int row);

   private:
    void checkBlasterConnection();
    void uploadBlastRecordToServer(HBlastRecord *blastRecord);
    HBlastRecord *recordBlastProject(const QJsonObject &jsonObj);
    HBlastEquipmentRecord *recordBlastEquipment(const QString blastProjectUUID, const QJsonObject &regObj);
    QList<HBlastRecordDet *> recordBlastProjectDets(const QString projectUUID, const QString equipmentUUID,
                                                    const QJsonArray regsArray);
    bool uploadToDanLing(const QJsonObject &jsonObj);
    void saveAndUploadRecord(const QJsonObject &jsonObj);
    void sendMqttMessage(const QString &topic, const QByteArray &message);
    void onSafeChecked(const QString projectUuid);

   private:
    Ui::firingWidget *ui;
    MqttClient *pcPorjectBC;
    HBlastRecordDao daoProj;
    HBlastRecordDetDao daoDet;
    HBlastRegRecordDao daoReg;
    NavProgress *navProgress;
    SerialTool *serialTool = nullptr;
    QString m_curProjUuid;
    QString m_topic;  // firingwidget对应的 MQTT 主题
    int m_row;
    int m_lastStage = -1;  // 用于记录上一次的 stage 状态
    bool m_select;
    QMetaObject::Connection connection;  // 声明 connection 变量
    QMetaObject::Connection connectionPress;
    QString m_latitude;
    QString m_longitude;
    QDateTime m_lastMsgTime;
    QTimer *m_connectionCheckTimer;
    QMqttSubscription *m_subscription;  // 用于订阅 MQTT 消息
    bool m_isDestroying = false;        // Flag to prevent operations during destruction
};

#endif  // FIRINGWIDGET_H
