#pragma once

#include <string>
#include <vector>
#include <cstdint>

// ── 融合威胁等级 ──
enum class FusionThreatLevel {
    SAFE     = 0,
    CAUTION  = 1,
    WARNING  = 2,
    DANGER   = 3,
    CRITICAL = 4,
};

struct FusionThreatLevelInfo {
    static const char* label(FusionThreatLevel l) {
        switch (l) {
            case FusionThreatLevel::SAFE:     return "Safe";
            case FusionThreatLevel::CAUTION:  return "Caution";
            case FusionThreatLevel::WARNING:  return "Warning";
            case FusionThreatLevel::DANGER:   return "Danger";
            case FusionThreatLevel::CRITICAL: return "Critical";
        }
        return "Unknown";
    }
    static bool is_dangerous(FusionThreatLevel l) {
        return l >= FusionThreatLevel::WARNING;
    }
};

// ── 融合威胁 ──
struct FusedThreat {
    std::string threat_id;
    FusionThreatLevel level = FusionThreatLevel::SAFE;
    double confidence = 0.0;
    double bearing    = 0.0;   // 方位角 (度), 0=北, 顺时针
    double elevation  = 0.0;   // 仰角 (度)
    double distance   = -1.0;  // 米, -1=未知
    std::vector<std::string> sources;
    std::string description;
    double timestamp = 0.0;
};

// ── 闪光检测数据 ──
struct FlashDetection {
    double cx = 0, cy = 0;
    double area = 0;
    double sight_x = 0, sight_y = 0;
    double timestamp = 0;
};

// ── 声源威胁数据 ──
struct AcousticThreatData {
    std::string threat_id;
    std::string sound_type;
    double confidence          = 0;
    double azimuth             = 0;
    double elevation           = 0;
    double distance            = -1;
    double distance_confidence = 0;
    double timestamp           = 0;
};

// ── 吊舱图像类型 (SpireCV) ──
enum class GimbalImageType {
    EO             = 0,   // 可见光
    IR_WHITEHOT    = 1,   // 红外白热
    IR_PSEUDOCOLOR = 2,   // 红外伪彩
    EO_IR_WHITEHOT = 3,   // 可见光 + 红外白热
    EO_IR_COLOR    = 4,   // 可见光 + 红外伪彩
};

inline const char* gimbal_image_type_str(GimbalImageType t) {
    switch (t) {
        case GimbalImageType::EO:             return "EO";
        case GimbalImageType::IR_WHITEHOT:    return "IR WhiteHot";
        case GimbalImageType::IR_PSEUDOCOLOR: return "IR PseudoColor";
        case GimbalImageType::EO_IR_WHITEHOT: return "EO+IR WhiteHot";
        case GimbalImageType::EO_IR_COLOR:    return "EO+IR Color";
    }
    return "Unknown";
}

// ── Prometheus uav_control 飞行命令模式 ──
// 对应 prometheus_msgs/UAVCommand.Move_mode
enum class PrometheusMoveMode {
    XYZ_POS          = 0,  // ENU 位置控制
    XY_VEL_Z_POS     = 1,  // ENU XY 速度 + Z 位置
    XYZ_VEL          = 2,  // ENU 速度控制 (避障使用)
    XYZ_POS_BODY     = 3,  // 机体坐标系位置控制
    XYZ_VEL_BODY     = 4,  // 机体坐标系速度控制
    XY_VEL_Z_POS_BODY = 5, // 机体 XY 速度 + Z 位置
    TRAJECTORY       = 6,  // 轨迹跟踪
};

// ── 融合配置 ──
struct FusionConfig {
    // 模态权重
    double weight_thermal  = 0.35;
    double weight_flash    = 0.30;
    double weight_acoustic = 0.35;
    double cross_boost_2   = 0.25;
    double cross_boost_3   = 0.15;

    // 阈值
    double fusion_time_window           = 0.5;   // 秒
    double fusion_confidence_threshold  = 0.5;
    double acoustic_confidence_threshold = 0.6;
    double thermal_hfov                 = 32.0;  // 度

    // 热成像
    double thermal_max_rate = 2.0;  // Hz

    // 避障
    bool   avoidance_enabled         = true;
    double avoidance_speed           = 2.0;   // m/s (不超过 maximum_safe_vel_xy)
    double avoidance_decel_distance  = 10.0;  // 米
    double avoidance_cooldown        = 5.0;   // 秒
    double avoidance_hold_time       = 2.0;   // 秒
    double avoidance_smoothing       = 0.3;
    bool   avoidance_vertical_enabled = true;

    // 吊舱 (SpireCV AT10 Pro, 参见 P600 双光吊舱使用说明)
    std::string gimbal_type = "AT10";
    std::string gimbal_port = "/dev/ttyUSB1";
    double gimbal_kp            = 1.0;
    double tracking_kp_x        = 0.3;
    double tracking_kp_z        = 0.1;
    double tracking_max_velocity = 2.5;   // m/s
    double tracking_distance     = 10.0;  // 米
    int    gimbal_max_yaw_rate   = 40;    // 度/秒

    // ── Prometheus uav_control 框架集成 ──
    // 参考: Prometheus/Modules/uav_control
    //   话题: /uav{N}/prometheus/command (UAVCommand)
    //   话题: /uav{N}/prometheus/state   (UAVState)
    //   话题: /uav{N}/prometheus/control_state (UAVControlState)
    //   消息: prometheus_msgs/
    int    uav_id               = 1;        // 无人机编号 (用于话题命名空间)
    std::string uav_namespace   = "/uav1";  // 自动生成: /uav{uav_id}

    // Prometheus 话题 (自动拼接 uav_namespace)
    // 命令: {uav_namespace}/prometheus/command
    // 状态: {uav_namespace}/prometheus/state
    // 控制状态: {uav_namespace}/prometheus/control_state
    // 地面站信息: {uav_namespace}/prometheus/text_info

    // SpireCV 吊舱图像话题
    // SpireCV at10_gimbal_server 发布:
    //   {uav_namespace}/gimbal/image_original  (原始图像)
    //   {uav_namespace}/spirecv/image_det       (检测结果叠加图)
    std::string thermal_image_topic = "/uav1/gimbal/image_original";

    // 安全限制 (对齐 uav_control_outdoor.yaml)
    double maximum_safe_vel_xy = 5.0;   // m/s, 与 uav_control 配置对齐
    double maximum_safe_vel_z  = 4.0;   // m/s
    // 地理围栏 (对齐 uav_control geo_fence)
    double geo_fence_x_min = -1000.0, geo_fence_x_max = 1000.0;
    double geo_fence_y_min = -1000.0, geo_fence_y_max = 1000.0;
    double geo_fence_z_min = -50.0,   geo_fence_z_max = 1000.0;

    // 模态开关
    bool   thermal_enabled  = true;
    bool   flash_enabled    = true;
    bool   acoustic_enabled = true;
};
