/**
 * @file RightFunctionPanel.cpp
 * @brief 右侧功能面板组件实现
 * @author BattlefieldExplorationSystem Team
 * @date 2024-01-15
 * @version 2.0 - UI优化版本
 */

#include "ui/components/RightFunctionPanel.h"
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
#include <QStyle>
#include <QPropertyAnimation>
#include <QGraphicsDropShadowEffect>
#include <QFile>
#include <QDebug>

// ModuleCard实现
ModuleCard::ModuleCard(const QString &title, const QString &icon, QWidget *parent)
    : QFrame(parent)
{
    setObjectName("ModuleCard");
    setFrameStyle(QFrame::NoFrame);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSpacing(16);
    layout->setContentsMargins(16, 16, 16, 16);
    
    // 标题栏 - 改进设计
    QHBoxLayout *headerLayout = new QHBoxLayout();
    headerLayout->setSpacing(12);
    
    QLabel *iconLabel = new QLabel();
    iconLabel->setObjectName("ModuleIcon");
    iconLabel->setText(icon.isEmpty() ? "📋" : icon);
    iconLabel->setFixedSize(24, 24);
    iconLabel->setAlignment(Qt::AlignCenter);
    
    m_titleLabel = new QLabel(title);
    m_titleLabel->setObjectName("ModuleTitle");

    headerLayout->addWidget(iconLabel);
    headerLayout->addWidget(m_titleLabel);
    headerLayout->addStretch();

    layout->addLayout(headerLayout);
    
    // 分隔线
    QFrame *separator = new QFrame();
    separator->setFrameShape(QFrame::HLine);
    separator->setObjectName("ModuleSeparator");
    layout->addWidget(separator);
    
    // 内容区域
    m_contentLayout = new QVBoxLayout();
    m_contentLayout->setSpacing(12);
    layout->addLayout(m_contentLayout);
}

void ModuleCard::addContent(QWidget *content)
{
    m_contentLayout->addWidget(content);
}

// RightDeviceCard实现 - 改进版本
RightDeviceCard::RightDeviceCard(const QString &name, const QString &iconPath, QWidget *parent)
    : QFrame(parent), m_deviceName(name)
{
    setObjectName("RightDeviceCard");
    setFrameStyle(QFrame::NoFrame);
    setCursor(Qt::PointingHandCursor);
    setFixedHeight(90);
    
    // 添加阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setBlurRadius(8);
    shadowEffect->setColor(QColor(0, 0, 0, 80));
    shadowEffect->setOffset(0, 2);
    setGraphicsEffect(shadowEffect);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setAlignment(Qt::AlignCenter);
    layout->setSpacing(6);
    layout->setContentsMargins(12, 12, 12, 12);
    
    m_iconLabel = new QLabel();
    m_iconLabel->setObjectName("DeviceIcon");
    m_iconLabel->setFixedSize(40, 40);
    m_iconLabel->setAlignment(Qt::AlignCenter);
    
    // 设置图标，使用更大更清晰的图标
    if (name.contains("机器狗") || name.contains("robot") || name.contains("dog")) {
        m_iconLabel->setText("🐕");
        m_iconLabel->setStyleSheet("font-size: 36px; margin-bottom: 8px;");
    } else if (name.contains("无人机") || name.contains("drone") || name.contains("uav")) {
        m_iconLabel->setText("🚁");
        m_iconLabel->setStyleSheet("font-size: 36px; margin-bottom: 8px;");
    } else {
        m_iconLabel->setText("📡");
        m_iconLabel->setStyleSheet("font-size: 36px; margin-bottom: 8px;");
    }
    
    m_nameLabel = new QLabel(name);
    m_nameLabel->setObjectName("DeviceName");
    m_nameLabel->setAlignment(Qt::AlignCenter);
    m_nameLabel->setWordWrap(false); // 禁止换行，确保单行显示
    m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    m_statusLabel = new QLabel("离线");
    m_statusLabel->setObjectName("DeviceStatus");
    m_statusLabel->setAlignment(Qt::AlignCenter);

    // 调整布局，确保文字有足够显示空间
    layout->addStretch(1); // 较小的上方弹性空间
    layout->addWidget(m_iconLabel);
    layout->addWidget(m_nameLabel);
    layout->addWidget(m_statusLabel);
    layout->addStretch(1); // 较小的下方弹性空间
}

void RightDeviceCard::setStatus(const QString &status, const QColor &color)
{
    m_statusLabel->setText(status);
    m_statusLabel->setStyleSheet(QString("color: %1; font-weight: 600;").arg(color.name()));
}

void RightDeviceCard::setActive(bool active)
{
    m_isActive = active;
    setProperty("active", active);
    style()->unpolish(this);
    style()->polish(this);
    update();
}

void RightDeviceCard::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        // 添加点击动画效果
        QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
        animation->setDuration(100);
        animation->setStartValue(geometry());
        QRect targetGeometry = geometry().adjusted(2, 2, -2, -2);
        animation->setEndValue(targetGeometry);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
        
        QTimer::singleShot(100, [this]() {
            QPropertyAnimation *backAnimation = new QPropertyAnimation(this, "geometry");
            backAnimation->setDuration(100);
            backAnimation->setStartValue(geometry());
            QRect originalGeometry = geometry().adjusted(-2, -2, 2, 2);
            backAnimation->setEndValue(originalGeometry);
            backAnimation->start(QAbstractAnimation::DeleteWhenStopped);
        });
        
        emit deviceSelected(m_deviceName);
    }
    QFrame::mousePressEvent(event);
}

void RightDeviceCard::paintEvent(QPaintEvent *event)
{
    QFrame::paintEvent(event);
    
    if (m_isActive) {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        QPen pen(QColor("#00a8ff"), 3);
        painter.setPen(pen);
        painter.drawRoundedRect(rect().adjusted(2, 2, -2, -2), 8, 8);
        
        // 添加发光效果
        QPen glowPen(QColor("#00a8ff"));
        glowPen.setWidth(1);
        painter.setPen(glowPen);
        painter.drawRoundedRect(rect().adjusted(4, 4, -4, -4), 6, 6);
    }
}

// RightFunctionPanel实现 - 全面优化版本
RightFunctionPanel::RightFunctionPanel(QWidget *parent)
    : QWidget(parent)
{
    setupUI();
    applyStyles();
}

void RightFunctionPanel::setupUI()
{
    setFixedWidth(380);  // 进一步增加宽度，给更多空间
    setObjectName("rightFunctionPanel");
    
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setSpacing(32);  // 进一步增加模块间距
    m_mainLayout->setContentsMargins(26, 26, 26, 26);  // 增加内边距
    
    // 面板标题 - 军事风格
    QLabel *titleLabel = new QLabel("⚔️ 作战控制面板");
    titleLabel->setObjectName("PanelTitle");
    titleLabel->setAlignment(Qt::AlignCenter);
    m_mainLayout->addWidget(titleLabel);

    setupBattlefieldExplorationModule();
    setupIntelligenceModule();
    setupEnemyStatsModule();

    m_mainLayout->addStretch();
}

void RightFunctionPanel::setupBattlefieldExplorationModule()
{
    m_explorationCard = new ModuleCard("🎯 战场探索", "🎯", this);
    m_explorationCard->setObjectName("ModuleCard");
    m_explorationCard->setProperty("data-module", "battlefield");
    
    // 设备选择器 - 全新设计
    QWidget *deviceSelectorWidget = new QWidget();
    deviceSelectorWidget->setObjectName("device-selector");
    QHBoxLayout *deviceLayout = new QHBoxLayout(deviceSelectorWidget);
    deviceLayout->setSpacing(16);  // 增加设备卡片间距
    deviceLayout->setContentsMargins(12, 12, 12, 12);  // 增加容器内边距
    
    m_robotDogCard = new RightDeviceCard("🐕 机器狗-01", "", this);
    m_droneCard = new RightDeviceCard("🚁 侦察机-01", "", this);
    
    connect(m_robotDogCard, &RightDeviceCard::deviceSelected, this, &RightFunctionPanel::onDeviceSelected);
    connect(m_droneCard, &RightDeviceCard::deviceSelected, this, &RightFunctionPanel::onDeviceSelected);

    deviceLayout->addWidget(m_robotDogCard);
    deviceLayout->addWidget(m_droneCard);
    m_explorationCard->addContent(deviceSelectorWidget);
    
    // 主要功能按钮 - 突出显示
    m_mappingBtn = new QPushButton("🗺️ 开始建图");
    m_mappingBtn->setObjectName("FunctionBtn");
    m_mappingBtn->setProperty("class", "primary-large");
    m_mappingBtn->setMinimumHeight(55);  // 与样式表中的设置保持一致
    m_mappingBtn->setEnabled(false);
    connect(m_mappingBtn, &QPushButton::clicked, this, &RightFunctionPanel::onMappingToggle);
    m_explorationCard->addContent(m_mappingBtn);
    
    // 次要功能按钮 - 三列布局
    QWidget *secondaryWidget = new QWidget();
    QHBoxLayout *secondaryLayout = new QHBoxLayout(secondaryWidget);
    secondaryLayout->setSpacing(12);  // 增加按钮间距
    secondaryLayout->setContentsMargins(0, 12, 0, 0);  // 增加上边距
    
    m_navigationBtn = new QPushButton("🧭 导航");
    m_photoBtn = new QPushButton("📸 传输");
    m_recognitionBtn = new QPushButton("👁️ 识别");
    
    // 设置次要按钮样式
    QList<QPushButton*> secondaryBtns = {m_navigationBtn, m_photoBtn, m_recognitionBtn};
    for(auto btn : secondaryBtns) {
        btn->setObjectName("FunctionBtn");
        btn->setProperty("class", "secondary-small");
        btn->setMinimumHeight(42);  // 与样式表中的设置保持一致
        btn->setEnabled(false);
    }
    
    connect(m_navigationBtn, &QPushButton::clicked, this, &RightFunctionPanel::onNavigationToggle);
    connect(m_photoBtn, &QPushButton::clicked, this, &RightFunctionPanel::onPhotoTransmissionToggle);
    connect(m_recognitionBtn, &QPushButton::clicked, this, &RightFunctionPanel::onPersonRecognitionToggle);
    
    secondaryLayout->addWidget(m_navigationBtn);
    secondaryLayout->addWidget(m_photoBtn);
    secondaryLayout->addWidget(m_recognitionBtn);
    m_explorationCard->addContent(secondaryWidget);
    
    m_mainLayout->addWidget(m_explorationCard);
}

void RightFunctionPanel::setupIntelligenceModule()
{
    m_intelligenceCard = new ModuleCard("📡 情报传输", "📡", this);
    m_intelligenceCard->setObjectName("ModuleCard");
    m_intelligenceCard->setProperty("data-module", "intelligence");
    
    // 情报传达说明
    QLabel *descLabel = new QLabel("🎯 远程音频控制系统");
    descLabel->setObjectName("intelligence-description");
    descLabel->setAlignment(Qt::AlignCenter);
    descLabel->setStyleSheet("color: #00a8ff; font-size: 14px; font-weight: bold; padding: 8px;");
    m_intelligenceCard->addContent(descLabel);
    
    // 主功能按钮 - 打开音频控制界面
    m_voiceCallBtn = new QPushButton("🔊 打开音频控制界面");
    m_voiceCallBtn->setObjectName("FunctionBtn");
    m_voiceCallBtn->setProperty("class", "primary-large");
    m_voiceCallBtn->setMinimumHeight(55);
    m_voiceCallBtn->setStyleSheet(
        "QPushButton {"
        "  background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,"
        "              stop:0 #00a8ff, stop:1 #0078d4);"
        "  color: white;"
        "  font-size: 16px;"
        "  font-weight: bold;"
        "  border: 2px solid #00a8ff;"
        "  border-radius: 8px;"
        "  padding: 12px;"
        "}"
        "QPushButton:hover {"
        "  background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,"
        "              stop:0 #0078d4, stop:1 #005a9f);"
        "}"
        "QPushButton:pressed {"
        "  background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,"
        "              stop:0 #005a9f, stop:1 #003d6b);"
        "}"
    );
    
    connect(m_voiceCallBtn, &QPushButton::clicked, this, &RightFunctionPanel::onOpenIntelligenceUI);
    m_intelligenceCard->addContent(m_voiceCallBtn);
    
    // 功能介绍
    QLabel *featureLabel = new QLabel("• SSH双跳连接\n• 音频文件播放\n• 实时录音制作\n• TTS语音合成");
    featureLabel->setObjectName("feature-list");
    featureLabel->setAlignment(Qt::AlignLeft);
    featureLabel->setStyleSheet("color: #b0b0b0; font-size: 12px; padding: 10px; line-height: 1.4;");
    m_intelligenceCard->addContent(featureLabel);

    m_mainLayout->addWidget(m_intelligenceCard);
}

void RightFunctionPanel::setupEnemyStatsModule()
{
    m_statsCard = new ModuleCard("📊 敌情统计", "📊", this);
    m_statsCard->setObjectName("ModuleCard");
    m_statsCard->setProperty("data-module", "statistics");
    
    // 统计信息显示区域 - 全新设计
    QWidget *statsDisplayWidget = new QWidget();
    statsDisplayWidget->setObjectName("stats-display");
    
    QVBoxLayout *statsLayout = new QVBoxLayout(statsDisplayWidget);
    statsLayout->setContentsMargins(20, 16, 20, 16);
    statsLayout->setSpacing(12);
    
    // 已发现目标 - 突出显示
    QHBoxLayout *targetLayout = new QHBoxLayout();
    QLabel *targetLabel = new QLabel("已发现目标:");
    targetLabel->setObjectName("stat-label");
    
    m_totalEnemiesLabel = new QLabel("3");
    m_totalEnemiesLabel->setObjectName("stat-value");
    m_totalEnemiesLabel->setAlignment(Qt::AlignRight);
    
    targetLayout->addWidget(targetLabel);
    targetLayout->addWidget(m_totalEnemiesLabel);
    
    // 威胁等级
    QHBoxLayout *threatLayout = new QHBoxLayout();
    QLabel *threatLabel = new QLabel("威胁等级:");
    threatLabel->setObjectName("stat-label");
    
    m_threatLevelLabel = new QLabel("中等");
    m_threatLevelLabel->setObjectName("threat-level");
    m_threatLevelLabel->setAlignment(Qt::AlignRight);
    
    threatLayout->addWidget(threatLabel);
    threatLayout->addWidget(m_threatLevelLabel);
    
    statsLayout->addLayout(targetLayout);
    statsLayout->addLayout(threatLayout);
    m_statsCard->addContent(statsDisplayWidget);
    
    // 操作按钮 - 改进布局
    QWidget *analysisWidget = new QWidget();
    QHBoxLayout *analysisLayout = new QHBoxLayout(analysisWidget);
    analysisLayout->setSpacing(12);
    analysisLayout->setContentsMargins(0, 8, 0, 0);
    
    m_refreshBtn = new QPushButton("🔍 刷新");
    m_aiAnalysisBtn = new QPushButton("🤖 AI分析");
    
    m_refreshBtn->setObjectName("FunctionBtn");
    m_aiAnalysisBtn->setObjectName("FunctionBtn");
    m_refreshBtn->setProperty("class", "secondary-medium");
    m_aiAnalysisBtn->setProperty("class", "secondary-medium");
    m_refreshBtn->setMinimumHeight(40);
    m_aiAnalysisBtn->setMinimumHeight(40);
    
    analysisLayout->addWidget(m_refreshBtn);
    analysisLayout->addWidget(m_aiAnalysisBtn);
    
    connect(m_refreshBtn, &QPushButton::clicked, this, &RightFunctionPanel::onRefreshStats);
    connect(m_aiAnalysisBtn, &QPushButton::clicked, this, &RightFunctionPanel::onAIAnalysis);
    m_statsCard->addContent(analysisWidget);
    
    // 导出报告按钮 - 主要操作
    m_exportBtn = new QPushButton("📄 导出报告");
    m_exportBtn->setObjectName("FunctionBtn");
    m_exportBtn->setProperty("class", "primary-large");
    m_exportBtn->setMinimumHeight(52);  // 突出重要性
    connect(m_exportBtn, &QPushButton::clicked, this, &RightFunctionPanel::exportReport);
    m_statsCard->addContent(m_exportBtn);

    m_mainLayout->addWidget(m_statsCard);
}

void RightFunctionPanel::applyStyles()
{
    // 直接使用蓝色配色的完整样式
    QString blueStyles = R"(
        /* 全局字体和基础样式 */
        QWidget {
            font-family: "Microsoft YaHei", "SimHei", sans-serif;
            color: #ffffff;
            font-weight: 500;
        }
        
        /* 主面板样式 */
        #rightFunctionPanel {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, 
                       stop:0 #0f1419, stop:1 #1a252f);
            border-left: 3px solid #00a8ff;
            border-radius: 0px;
        }
        
        /* 面板标题 */
        #PanelTitle {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,
                        stop:0 #00a8ff, stop:1 #0078d4);
            color: #ffffff;
            font-size: 18px;
            font-weight: bold;
            padding: 16px 20px;
            border-radius: 10px;
            margin-bottom: 20px;
            text-align: center;
            border: 2px solid #00a8ff;
            /* text-shadow 不支持，已移除 */
        }
        
        /* 模块卡片 */
        #ModuleCard {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, 
                       stop:0 #1e2832, stop:1 #2a3441);
            border-radius: 12px;
            border: 2px solid #3c4a59;
            border-left: 4px solid #00a8ff;
            padding: 0px;
            margin-bottom: 28px;
        }

        #ModuleCard:hover {
            border-color: #00a8ff;
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, 
                       stop:0 #243340, stop:1 #304050);
        }
        
        /* 模块标题 */
        #ModuleTitle {
            color: #00a8ff;
            font-size: 16px;
            font-weight: 700;
            /* text-shadow 不支持，用边框替代发光效果 */
            border: 1px solid rgba(0, 168, 255, 0.3);
            border-radius: 4px;
            padding: 2px 4px;
        }

        #ModuleIcon {
            color: #00a8ff;
            font-size: 20px;
            /* text-shadow 不支持，用边框替代发光效果 */
            border: 1px solid rgba(0, 168, 255, 0.5);
            border-radius: 4px;
            padding: 2px 4px;
        }
        
        /* 模块分隔线 */
        #ModuleSeparator {
            border: none;
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,
                        stop:0 transparent, stop:0.5 #3c4a59, stop:1 transparent);
            height: 1px;
            margin: 8px 0px;
        }
        
        /* 设备选择器 */
        #device-selector {
            background: #2a3441;
            border: 1px solid #3c4a59;
            border-radius: 8px;
            padding: 8px;
        }
        
        /* 设备卡片 */
        #RightDeviceCard {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #2a3441, stop:1 #34404f);
            border-radius: 10px;
            border: 2px solid #3c4a59;
            padding: 16px;
            margin: 6px;
            min-height: 100px;
        }

        #RightDeviceCard:hover {
            border-color: #66d6ff;
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #34404f, stop:1 #3e4a5f);
        }
        
        #RightDeviceCard[active="true"] {
            border-color: #00a8ff;
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 rgba(0, 168, 255, 0.1), stop:1 rgba(0, 168, 255, 0.05));
            /* box-shadow 不支持，用边框加粗替代 */
            border-width: 3px;
        }

        #DeviceName {
            color: #ffffff;
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 4px;
        }

        #DeviceStatus {
            color: #a4b0be;
            font-size: 14px;
            font-weight: 500;
        }
        
        /* 功能按钮基础样式 */
        #FunctionBtn {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #2a3441, stop:1 #34404f);
            color: #ffffff;
            font-size: 16px;
            font-weight: 600;
            padding: 16px 20px;
            border-radius: 8px;
            border: 2px solid #3c4a59;
            margin: 6px;
            text-align: center;
            min-height: 45px;
        }

        #FunctionBtn:hover {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #34404f, stop:1 #3e4a5f);
            border-color: #66d6ff;
        }
        
        /* 主要按钮样式 */
        #FunctionBtn[class="primary-large"] {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #00a8ff, stop:1 #0078d4);
            color: #ffffff;
            font-size: 18px;
            font-weight: 700;
            border: 2px solid #00a8ff;
            padding: 18px 24px;
            min-height: 55px;
        }
        
        #FunctionBtn[class="primary-large"]:hover {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #0078d4, stop:1 #005a9e);
            /* box-shadow 不支持，用边框替代 */
            border-bottom: 4px solid rgba(0, 168, 255, 0.4);
        }
        
        #FunctionBtn[class="primary-medium"] {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #00a8ff, stop:1 #0078d4);
            color: #ffffff;
            font-size: 16px;
            font-weight: 700;
            border: 2px solid #00a8ff;
            padding: 16px 20px;
            min-height: 48px;
        }
        
        #FunctionBtn[class="primary-medium"]:hover {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #0078d4, stop:1 #005a9e);
            /* box-shadow 不支持，用边框替代 */
            border-bottom: 3px solid rgba(0, 168, 255, 0.3);
        }
        
        /* 次要按钮样式 */
        #FunctionBtn[class="secondary-medium"] {
            background: #2a3441;
            border: 2px solid #3c4a59;
            color: #ffffff;
            font-size: 16px;
            padding: 16px 20px;
            min-height: 48px;
        }
        
        #FunctionBtn[class="secondary-medium"]:hover {
            border-color: #66d6ff;
            background: #34404f;
        }
        
        #FunctionBtn[class="secondary-small"] {
            background: #2a3441;
            border: 2px solid #3c4a59;
            color: #ffffff;
            font-size: 15px;
            padding: 14px 18px;
            min-height: 42px;
        }
        
        #FunctionBtn[class="secondary-small"]:hover {
            border-color: #66d6ff;
            background: #34404f;
        }
        
        /* 危险按钮样式 */
        #FunctionBtn[class="danger"] {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #ff3838, stop:1 #c44569);
            border: 2px solid #ff3838;
            color: #ffffff;
        }
        
        #FunctionBtn[class="danger"]:hover {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #e53e3e, stop:1 #b83b5e);
            /* box-shadow 不支持，用边框替代 */
            border-bottom: 4px solid rgba(255, 56, 56, 0.4);
        }
        
        #FunctionBtn:disabled {
            background: #1e2832;
            color: #556983;
            border-color: #2a3441;
        }
        
        /* 加载状态按钮 */
        #FunctionBtn[class="loading"] {
            background: #34404f;
            border-color: #3c4a59;
            color: #a4b0be;
        }
        
        /* 统计显示区域 */
        #stats-display {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #2a3441, stop:1 #34404f);
            border-radius: 8px;
            border: 2px solid #3c4a59;
            border-left: 4px solid #00a8ff;
            margin-bottom: 16px;
        }
        
        #stat-label {
            color: #a4b0be;
            font-size: 13px;
            font-weight: 500;
        }
        
        #stat-value {
            color: #00a8ff;
            font-size: 24px;
            font-weight: bold;
            /* text-shadow 不支持，用边框替代发光效果 */
            border: 1px solid rgba(0, 168, 255, 0.5);
        }

        #threat-level {
            color: #ffa502;
            font-size: 15px;
            font-weight: 700;
            /* text-shadow 不支持，用边框替代发光效果 */
            border: 1px solid rgba(255, 165, 2, 0.3);
        }
        
        /* 通话状态 */
        #call-status {
            background: #2a3441;
            border: 2px solid #3c4a59;
            border-radius: 6px;
            padding: 12px 16px;
            color: #a4b0be;
            font-size: 13px;
            font-weight: 500;
            margin-top: 12px;
        }
        
        /* 通话状态样式类 */
        QLabel[class="call-status"] {
            background: #2a3441;
            border: 2px solid #3c4a59;
            border-radius: 6px;
            padding: 12px 16px;
            color: #a4b0be;
            font-size: 13px;
            font-weight: 500;
            margin-top: 12px;
        }
        
        QLabel[class="call-status-active"] {
            background: #2a3441;
            border: 2px solid #00a8ff;
            border-radius: 6px;
            padding: 12px 16px;
            color: #00a8ff;
            font-size: 13px;
            font-weight: 600;
            margin-top: 12px;
        }
        
        /* 音量控制 */
        #volume-label {
            color: #a4b0be;
            font-size: 13px;
            font-weight: 600;
        }
        
        #volume-percent {
            color: #00a8ff;
            font-size: 13px;
            font-weight: 700;
        }
        
        /* 音量滑块样式 */
        #volume-slider::groove:horizontal {
            border: 2px solid #3c4a59;
            height: 8px;
            background: #2a3441;
            border-radius: 4px;
            margin: 2px 0;
        }
        
        #volume-slider::handle:horizontal {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,
                        stop:0 #00a8ff, stop:1 #0078d4);
            border: 2px solid #00a8ff;
            width: 20px;
            height: 20px;
            margin: -8px 0;
            border-radius: 10px;
        }
        
        #volume-slider::handle:horizontal:hover {
            background: #0078d4;
            /* box-shadow 不支持，用边框替代发光效果 */
            border: 2px solid rgba(0, 168, 255, 0.5);
        }
        
        #volume-slider::sub-page:horizontal {
            background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,
                        stop:0 #00a8ff, stop:1 #66d6ff);
            border-radius: 4px;
        }
    )";
    
    setStyleSheet(blueStyles);
    qDebug() << "已应用蓝色配色样式";
}

// 槽函数实现
void RightFunctionPanel::onDeviceSelected(const QString &deviceName)
{
    m_selectedDevice = deviceName;
    
    // 更新设备选择状态
    m_robotDogCard->setActive(deviceName.contains("机器狗") || deviceName.contains("robot") || deviceName.contains("dog"));
    m_droneCard->setActive(deviceName.contains("无人机") || deviceName.contains("drone") || deviceName.contains("uav"));
    
    // 根据设备类型启用/禁用相应按钮
    bool isRobotDog = deviceName.contains("机器狗") || deviceName.contains("robot") || deviceName.contains("dog");
    m_mappingBtn->setEnabled(isRobotDog);
    m_navigationBtn->setEnabled(isRobotDog);
    m_photoBtn->setEnabled(!isRobotDog);
    m_recognitionBtn->setEnabled(!isRobotDog);
}

void RightFunctionPanel::onMappingToggle()
{
    static bool isMappingActive = false;
    isMappingActive = !isMappingActive;
    
    m_mappingBtn->setText(isMappingActive ? "⏹️ 停止建图" : "🗺️ 开始建图");
    m_mappingBtn->setProperty("class", isMappingActive ? "danger" : "");
    m_mappingBtn->style()->unpolish(m_mappingBtn);
    m_mappingBtn->style()->polish(m_mappingBtn);
    
    if (isMappingActive) {
        emit startMapping();
    } else {
        emit stopMapping();
    }
}

void RightFunctionPanel::onNavigationToggle()
{
    static bool isNavigationActive = false;
    isNavigationActive = !isNavigationActive;
    
    m_navigationBtn->setText(isNavigationActive ? "⏹️ 停止导航" : "🧭 导航避障");
    m_navigationBtn->setProperty("class", isNavigationActive ? "danger" : "secondary-medium");
    m_navigationBtn->style()->unpolish(m_navigationBtn);
    m_navigationBtn->style()->polish(m_navigationBtn);
    
    if (isNavigationActive) {
        emit startNavigation();
    } else {
        emit stopNavigation();
    }
}

void RightFunctionPanel::onPhotoTransmissionToggle()
{
    static bool isTransmissionActive = false;
    isTransmissionActive = !isTransmissionActive;
    
    m_photoBtn->setText(isTransmissionActive ? "⏹️ 停止传输" : "📷 照片传输");
    m_photoBtn->setProperty("class", isTransmissionActive ? "danger" : "secondary-medium");
    m_photoBtn->style()->unpolish(m_photoBtn);
    m_photoBtn->style()->polish(m_photoBtn);
    
    if (isTransmissionActive) {
        emit startPhotoTransmission();
    } else {
        emit stopPhotoTransmission();
    }
}

void RightFunctionPanel::onPersonRecognitionToggle()
{
    static bool isRecognitionActive = false;
    isRecognitionActive = !isRecognitionActive;
    
    m_recognitionBtn->setText(isRecognitionActive ? "⏹️ 停止识别" : "👤 人物识别");
    m_recognitionBtn->setProperty("class", isRecognitionActive ? "danger" : "secondary-medium");
    m_recognitionBtn->style()->unpolish(m_recognitionBtn);
    m_recognitionBtn->style()->polish(m_recognitionBtn);
    
    if (isRecognitionActive) {
        emit startPersonRecognition();
    } else {
        emit stopPersonRecognition();
    }
}

void RightFunctionPanel::onOpenIntelligenceUI()
{
    emit openIntelligenceUI();
}

void RightFunctionPanel::onRefreshStats()
{
    emit refreshEnemyStats();
    
    // 模拟刷新效果
    m_refreshBtn->setText("⏳ 刷新中...");
    m_refreshBtn->setProperty("class", "loading");
    m_refreshBtn->setEnabled(false);
    m_refreshBtn->style()->unpolish(m_refreshBtn);
    m_refreshBtn->style()->polish(m_refreshBtn);
    
    QTimer::singleShot(2000, [this]() {
        m_refreshBtn->setText("🔍 刷新");
        m_refreshBtn->setProperty("class", "secondary-medium");
        m_refreshBtn->setEnabled(true);
        m_refreshBtn->style()->unpolish(m_refreshBtn);
        m_refreshBtn->style()->polish(m_refreshBtn);
    });
}

void RightFunctionPanel::onAIAnalysis()
{
    emit requestAIAnalysis();
    
    // 显示分析状态
    m_aiAnalysisBtn->setText("🧠 分析中...");
    m_aiAnalysisBtn->setProperty("class", "loading");
    m_aiAnalysisBtn->setEnabled(false);
    m_aiAnalysisBtn->style()->unpolish(m_aiAnalysisBtn);
    m_aiAnalysisBtn->style()->polish(m_aiAnalysisBtn);
    
    QTimer::singleShot(3000, [this]() {
        m_aiAnalysisBtn->setText("🤖 AI分析");
        m_aiAnalysisBtn->setProperty("class", "secondary-medium");
        m_aiAnalysisBtn->setEnabled(true);
        m_aiAnalysisBtn->style()->unpolish(m_aiAnalysisBtn);
        m_aiAnalysisBtn->style()->polish(m_aiAnalysisBtn);
    });
}

void RightFunctionPanel::updateEnemyStats(int totalEnemies, const QString &threatLevel)
{
    m_totalEnemiesLabel->setText(QString::number(totalEnemies));
    m_threatLevelLabel->setText(threatLevel);
    
    // 根据威胁等级设置颜色和样式
    if (threatLevel == "高" || threatLevel == "高等") {
        m_threatLevelLabel->setStyleSheet(
            "color: #ff3838; "
            "font-size: 15px; "
            "font-weight: 700; "
            "border: 1px solid rgba(255, 56, 56, 0.5); "
            "border-radius: 4px; "
            "padding: 2px 4px;"
        );
    } else if (threatLevel == "中" || threatLevel == "中等") {
        m_threatLevelLabel->setStyleSheet(
            "color: #ffa502; "
            "font-size: 15px; "
            "font-weight: 700; "
            "border: 1px solid rgba(255, 165, 2, 0.3); "
            "border-radius: 4px; "
            "padding: 2px 4px;"
        );
    } else {
        m_threatLevelLabel->setStyleSheet(
            "color: #00a8ff; "
            "font-size: 15px; "
            "font-weight: 700; "
            "border: 1px solid rgba(0, 168, 255, 0.3); "
            "border-radius: 4px; "
            "padding: 2px 4px;"
        );
    }
}

void RightFunctionPanel::updateDeviceStatus(const QString &deviceName, bool online, int battery)
{
    RightDeviceCard *deviceCard = nullptr;
    if (deviceName.contains("机器狗") || deviceName.contains("robot") || deviceName.contains("dog")) {
        deviceCard = m_robotDogCard;
    } else if (deviceName.contains("无人机") || deviceName.contains("drone") || deviceName.contains("uav")) {
        deviceCard = m_droneCard;
    }
    
    if (deviceCard) {
        if (online) {
            if (battery > 80) {
                deviceCard->setStatus(QString("📶 连接质量 %1%").arg(battery), QColor("#4CAF50"));
            } else if (battery > 50) {
                deviceCard->setStatus(QString("📶 连接质量 %1%").arg(battery), QColor("#8BC34A"));
            } else if (battery > 20) {
                deviceCard->setStatus(QString("⚠️ 连接质量 %1%").arg(battery), QColor("#FF8C00"));
            } else {
                deviceCard->setStatus(QString("⚠️ 连接质量 %1%").arg(battery), QColor("#DC143C"));
            }
        } else {
            deviceCard->setStatus("❌ 设备离线", QColor("#78909C"));
        }
    }
}

