#ifndef FIRINGWIDGET_H
#define FIRINGWIDGET_H
#include <QJsonDocument>
#include <QJsonObject>
#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 "../mqttthread.h"
#include "../serial/serialtool.h"
#include "navprogress.h"

namespace Ui {
class firingWidget;
}

class firingWidget : public QWidget {
    Q_OBJECT

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

   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 QByteArray &message, const QMqttTopicName &topic);

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

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

   private:
    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 startBlasting();
    void onSafeChecked(const QString projectUuid);

   private:
    Ui::firingWidget *ui;
    MqttClient *pcPorjectBC;
    HBlastRecordDao daoProj;
    HBlastRecordDetDao daoDet;
    HBlastRegRecordDao daoReg;
    NavProgress *navProgress;
    MqttThread *mqttThread;
    SerialTool *serialTool = nullptr;
    QString m_uuid;      // 用于存储 uuid
    QString reg_uuid;    // 用于存储 uuid
    QString blast_uuid;  // 用于存储 uuid
    QString topic;
    int m_row;
    int lastStage = -1;  // 用于记录上一次的 stage 状态
    bool m_isMqttConnected = false;
    bool m_select;
    QMetaObject::Connection connection;  // 声明 connection 变量
    QMetaObject::Connection connectionPress;
    QString lat;
    QString lon;
};

#endif  // FIRINGWIDGET_H
