#ifndef GLOBAL_H
#define GLOBAL_H

#include <QString>
#include <QUrl>
#include <QUrlQuery>

#include "logger.h"
#include "mqtt/mqttclient.h"

extern MqttClient* mainMqttClient;

/* global variables which can configured in config file */
extern QUrl apiBackendUrl;                           // 声明全局变量
extern QString firewidgetPort;                       // 点火装置串口号
extern QString gpsPort;                              // gps串口号
extern QString databaseHost;                         // pc数据库地址
extern QString mqttClientId;                         // mqtt客户端ID
extern QString MQTT_TOPIC_COMPANY_PROJECTS_SUB;      // 每个部署点(公司)唯一地址
extern QString MQTT_TOPIC_COMPANY_PROJECTS_PUBLISH;  // 发布工程的topic
extern QString COMPANY_CODE;
extern bool isShowTriggeringWidget;  // 是否显示点火装置触发中调试组件
// 用来获取雷管参数
extern QString DET_PARAM_XMBH;  // 项目编号
extern QString DET_PARAM_DWDM;  // 单位代码
extern QString DET_PARAM_HTID;  // 合同ID
extern QString DET_PARAM_SBBH;  // 设备编号

extern void loadConfig();  // 声明全局函数

extern QString globalAuthority;
extern QString labLat;  // 新增的全局变量声明
extern QString labLon;  // 新增的全局变量声明

// BlastingProjManager namespace for managing blasting projects
namespace BlastingProjManager {
inline std::vector<QString> blastingProjectUuids;

inline void addBlastingProject(const QString& uuid) { blastingProjectUuids.push_back(uuid); }

inline void removeBlastingProject(const QString& uuid) {
    auto it = std::find(blastingProjectUuids.begin(), blastingProjectUuids.end(), uuid);
    if (it != blastingProjectUuids.end()) {
        blastingProjectUuids.erase(it);
    }
}

inline int getBlastingProjectCount() { return blastingProjectUuids.size(); }

inline const std::vector<QString>& getBlastingProjectUuids() { return blastingProjectUuids; }
}  // namespace BlastingProjManager

/* BlastStatus*/
namespace BlastStatus {
const QString Created = "1";
const QString Registered = "2";
const QString Blasted = "3";
const QString ErrorOccurred = "4";
const QString SafeChecked = "5";
}  // namespace BlastStatus

namespace BlastStatusNames {
const QString Created = "未注册";
const QString Registered = "已注册";
const QString Blasted = "爆破完成";
const QString ErrorOccurred = "爆破异常";
const QString SafeChecked = "已安全检查";
}  // namespace BlastStatusNames

namespace ErrorBlastStatus {
// Error code constants
const int DEV_ERROR = 0xA0;
const int BUS_SHORT = 0xA1;
const int BUS_VOLTAGE_ERR = 0xA2;
const int DET_OFFLINE_ERR = 0xA3;

// Internal function to get the error map (lazy initialization)
inline const std::map<int, QString>& getErrorMap() {
    static const std::map<int, QString> errorMap = {{DEV_ERROR, "设备异常"},
                                                    {BUS_SHORT, "总线短路"},
                                                    {BUS_VOLTAGE_ERR, "总线电压异常"},
                                                    {DET_OFFLINE_ERR, "雷管掉线"}};
    return errorMap;
}

// Check if a status code exists in the error list
inline bool isErrorStatus(int code) { return getErrorMap().find(code) != getErrorMap().end(); }

// Get error message for a status code
inline QString getErrorMessage(int code) {
    const auto& map = getErrorMap();
    auto it = map.find(code);
    return (it != map.end()) ? it->second : "Unknown error";
}
}  // namespace ErrorBlastStatus

// namespace FiringStages
namespace FiringStages {
const static int Starting = 0;
const static int QuickTesting = 1;                // 起爆检测测试中
const static int QuickTestFinished = 2;           // 起爆检测测试完成
const static int NetCharging = 3;                 // 起爆检测充电中
const static int NetChargingFinished = 4;         // 起爆检测充电完成
const static int Blasting = 5;                    // 爆破中
const static int BlastFinished = 6;               // 爆破完成
const static int CancelConfirmed = 7;             // 确认取消
const static int PendingTriggerButtonClick = 10;  // 待触发按钮触发起爆
}  // namespace FiringStages
// namespace FiringStages

#endif  // GLOBAL_H
