// RightFunctionPanel.h
#ifndef RIGHTFUNCTIONPANEL_H
#define RIGHTFUNCTIONPANEL_H

#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QFrame>
#include <QProgressBar>
#include <QSlider>

class ModuleCard : public QFrame
{
    Q_OBJECT
    
public:
    explicit ModuleCard(const QString &title, const QString &icon, QWidget *parent = nullptr);
    void addContent(QWidget *content);
    
private:
    QVBoxLayout *m_contentLayout;
    QLabel *m_titleLabel;
};

class DeviceCard : public QFrame
{
    Q_OBJECT
    
public:
    explicit DeviceCard(const QString &name, const QString &iconPath, QWidget *parent = nullptr);
    void setStatus(const QString &status, const QColor &color);
    void setActive(bool active);
    
signals:
    void deviceSelected(const QString &deviceName);
    
protected:
    void mousePressEvent(QMouseEvent *event) override;
    void paintEvent(QPaintEvent *event) override;
    
private:
    QString m_deviceName;
    QLabel *m_iconLabel;
    QLabel *m_nameLabel;
    QLabel *m_statusLabel;
    bool m_isActive = false;
};

class RightFunctionPanel : public QWidget
{
    Q_OBJECT
    
public:
    explicit RightFunctionPanel(QWidget *parent = nullptr);
    
signals:
    // 战场探索信号
    void startMapping();
    void stopMapping();
    void startNavigation();
    void stopNavigation();
    void startPhotoTransmission();
    void stopPhotoTransmission();
    void startPersonRecognition();
    void stopPersonRecognition();
    
    // 情报传输信号
    void startVoiceCall();
    void endVoiceCall();
    void muteCall(bool muted);
    void setCallVolume(int volume);
    
    // 敌情统计信号
    void refreshEnemyStats();
    void exportReport();
    void requestAIAnalysis();
    
public slots:
    void updateEnemyStats(int totalEnemies, const QString &threatLevel);
    void updateDeviceStatus(const QString &deviceName, bool online, int battery);
    
private slots:
    void onDeviceSelected(const QString &deviceName);
    void onMappingToggle();
    void onNavigationToggle();
    void onPhotoTransmissionToggle();
    void onPersonRecognitionToggle();
    void onVoiceCallToggle();
    void onRefreshStats();
    void onAIAnalysis();
    
private:
    void setupUI();
    void setupBattlefieldExplorationModule();
    void setupIntelligenceModule();
    void setupEnemyStatsModule();
    void applyStyles();
    
    // UI组件
    QVBoxLayout *m_mainLayout;
    
    // 战场探索模块
    ModuleCard *m_explorationCard;
    DeviceCard *m_robotDogCard;
    DeviceCard *m_droneCard;
    QPushButton *m_mappingBtn;
    QPushButton *m_navigationBtn;
    QPushButton *m_photoBtn;
    QPushButton *m_recognitionBtn;
    QString m_selectedDevice;
    
    // 情报传输模块
    ModuleCard *m_intelligenceCard;
    QPushButton *m_voiceCallBtn;
    QPushButton *m_muteBtn;
    QSlider *m_volumeSlider;
    QLabel *m_callStatusLabel;
    bool m_isInCall = false;
    
    // 敌情统计模块
    ModuleCard *m_statsCard;
    QLabel *m_totalEnemiesLabel;
    QLabel *m_threatLevelLabel;
    QPushButton *m_refreshBtn;
    QPushButton *m_aiAnalysisBtn;
    QPushButton *m_exportBtn;
};

#endif // RIGHTFUNCTIONPANEL_H

// RightFunctionPanel.cpp
#include "RightFunctionPanel.h"
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>

// ModuleCard实现
ModuleCard::ModuleCard(const QString &title, const QString &icon, QWidget *parent)
    : QFrame(parent)
{
    setObjectName("ModuleCard");
    setFrameStyle(QFrame::StyledPanel);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSpacing(12);
    layout->setContentsMargins(12, 12, 12, 12);
    
    // 标题栏
    QHBoxLayout *headerLayout = new QHBoxLayout();
    QLabel *iconLabel = new QLabel();
    iconLabel->setObjectName("ModuleIcon");
    iconLabel->setText(icon); // 使用Unicode图标或设置图片
    iconLabel->setFixedSize(20, 20);
    
    m_titleLabel = new QLabel(title);
    m_titleLabel->setObjectName("ModuleTitle");
    
    headerLayout->addWidget(iconLabel);
    headerLayout->addWidget(m_titleLabel);
    headerLayout->addStretch();
    
    layout->addLayout(headerLayout);
    
    // 内容区域
    m_contentLayout = new QVBoxLayout();
    m_contentLayout->setSpacing(8);
    layout->addLayout(m_contentLayout);
}

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

// DeviceCard实现
DeviceCard::DeviceCard(const QString &name, const QString &iconPath, QWidget *parent)
    : QFrame(parent), m_deviceName(name)
{
    setObjectName("DeviceCard");
    setFrameStyle(QFrame::StyledPanel);
    setCursor(Qt::PointingHandCursor);
    setFixedHeight(80);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setAlignment(Qt::AlignCenter);
    layout->setSpacing(4);
    
    m_iconLabel = new QLabel();
    m_iconLabel->setObjectName("DeviceIcon");
    m_iconLabel->setFixedSize(32, 32);
    m_iconLabel->setAlignment(Qt::AlignCenter);
    // 设置图标，这里用文字代替
    m_iconLabel->setText(name.contains("机器狗") ? "🐕" : "🚁");
    
    m_nameLabel = new QLabel(name);
    m_nameLabel->setObjectName("DeviceName");
    m_nameLabel->setAlignment(Qt::AlignCenter);
    
    m_statusLabel = new QLabel("离线");
    m_statusLabel->setObjectName("DeviceStatus");
    m_statusLabel->setAlignment(Qt::AlignCenter);
    
    layout->addWidget(m_iconLabel);
    layout->addWidget(m_nameLabel);
    layout->addWidget(m_statusLabel);
}

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

void DeviceCard::setActive(bool active)
{
    m_isActive = active;
    update();
}

void DeviceCard::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        emit deviceSelected(m_deviceName);
    }
    QFrame::mousePressEvent(event);
}

void DeviceCard::paintEvent(QPaintEvent *event)
{
    QFrame::paintEvent(event);
    
    if (m_isActive) {
        QPainter painter(this);
        painter.setPen(QPen(QColor("#2E5D31"), 2));
        painter.drawRect(rect().adjusted(1, 1, -1, -1));
    }
}

// RightFunctionPanel实现
RightFunctionPanel::RightFunctionPanel(QWidget *parent)
    : QWidget(parent)
{
    setupUI();
    applyStyles();
}

void RightFunctionPanel::setupUI()
{
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setSpacing(12);
    m_mainLayout->setContentsMargins(16, 16, 16, 16);
    
    // 面板标题
    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);
    
    // 设备选择
    QHBoxLayout *deviceLayout = new QHBoxLayout();
    m_robotDogCard = new DeviceCard("机器狗", "robot_dog.png", this);
    m_droneCard = new DeviceCard("无人机", "drone.png", this);
    
    connect(m_robotDogCard, &DeviceCard::deviceSelected, this, &RightFunctionPanel::onDeviceSelected);
    connect(m_droneCard, &DeviceCard::deviceSelected, this, &RightFunctionPanel::onDeviceSelected);
    
    deviceLayout->addWidget(m_robotDogCard);
    deviceLayout->addWidget(m_droneCard);
    
    QWidget *deviceWidget = new QWidget();
    deviceWidget->setLayout(deviceLayout);
    m_explorationCard->addContent(deviceWidget);
    
    // 功能按钮
    QGridLayout *buttonLayout = new QGridLayout();
    m_mappingBtn = new QPushButton("自主建图");
    m_navigationBtn = new QPushButton("导航避障");
    m_photoBtn = new QPushButton("照片传输");
    m_recognitionBtn = new QPushButton("人物识别");
    
    // 设置按钮样式类名
    m_mappingBtn->setObjectName("FunctionBtn");
    m_navigationBtn->setObjectName("FunctionBtn");
    m_photoBtn->setObjectName("FunctionBtn");
    m_recognitionBtn->setObjectName("FunctionBtn");
    
    buttonLayout->addWidget(m_mappingBtn, 0, 0);
    buttonLayout->addWidget(m_navigationBtn, 0, 1);
    buttonLayout->addWidget(m_photoBtn, 1, 0);
    buttonLayout->addWidget(m_recognitionBtn, 1, 1);
    
    connect(m_mappingBtn, &QPushButton::clicked, this, &RightFunctionPanel::onMappingToggle);
    connect(m_navigationBtn, &QPushButton::clicked, this, &RightFunctionPanel::onNavigationToggle);
    connect(m_photoBtn, &QPushButton::clicked, this, &RightFunctionPanel::onPhotoTransmissionToggle);
    connect(m_recognitionBtn, &QPushButton::clicked, this, &RightFunctionPanel::onPersonRecognitionToggle);
    
    QWidget *buttonWidget = new QWidget();
    buttonWidget->setLayout(buttonLayout);
    m_explorationCard->addContent(buttonWidget);
    
    m_mainLayout->addWidget(m_explorationCard);
}

void RightFunctionPanel::setupIntelligenceModule()
{
    m_intelligenceCard = new ModuleCard("情报传输", "📡", this);
    
    // 通话控制
    QHBoxLayout *callLayout = new QHBoxLayout();
    m_voiceCallBtn = new QPushButton("开始通话");
    m_muteBtn = new QPushButton("静音");
    m_voiceCallBtn->setObjectName("FunctionBtn");
    m_muteBtn->setObjectName("FunctionBtn");
    m_muteBtn->setEnabled(false);
    
    callLayout->addWidget(m_voiceCallBtn);
    callLayout->addWidget(m_muteBtn);
    
    connect(m_voiceCallBtn, &QPushButton::clicked, this, &RightFunctionPanel::onVoiceCallToggle);
    
    QWidget *callWidget = new QWidget();
    callWidget->setLayout(callLayout);
    m_intelligenceCard->addContent(callWidget);
    
    // 音量控制
    QHBoxLayout *volumeLayout = new QHBoxLayout();
    QLabel *volumeLabel = new QLabel("音量:");
    m_volumeSlider = new QSlider(Qt::Horizontal);
    m_volumeSlider->setRange(0, 100);
    m_volumeSlider->setValue(70);
    
    volumeLayout->addWidget(volumeLabel);
    volumeLayout->addWidget(m_volumeSlider);
    
    QWidget *volumeWidget = new QWidget();
    volumeWidget->setLayout(volumeLayout);
    m_intelligenceCard->addContent(volumeWidget);
    
    // 通话状态
    m_callStatusLabel = new QLabel("未连接");
    m_callStatusLabel->setObjectName("CallStatus");
    m_intelligenceCard->addContent(m_callStatusLabel);
    
    m_mainLayout->addWidget(m_intelligenceCard);
}

void RightFunctionPanel::setupEnemyStatsModule()
{
    m_statsCard = new ModuleCard("敌情统计", "📊", this);
    
    // 统计信息
    QVBoxLayout *statsLayout = new QVBoxLayout();
    
    m_totalEnemiesLabel = new QLabel("已发现目标: 0");
    m_threatLevelLabel = new QLabel("威胁等级: 无");
    m_totalEnemiesLabel->setObjectName("StatLabel");
    m_threatLevelLabel->setObjectName("StatLabel");
    
    statsLayout->addWidget(m_totalEnemiesLabel);
    statsLayout->addWidget(m_threatLevelLabel);
    
    QWidget *statsWidget = new QWidget();
    statsWidget->setLayout(statsLayout);
    m_statsCard->addContent(statsWidget);
    
    // 操作按钮
    QHBoxLayout *statsButtonLayout = new QHBoxLayout();
    m_refreshBtn = new QPushButton("刷新");
    m_aiAnalysisBtn = new QPushButton("AI分析");
    m_exportBtn = new QPushButton("导出报告");
    
    m_refreshBtn->setObjectName("FunctionBtn");
    m_aiAnalysisBtn->setObjectName("FunctionBtn");
    m_exportBtn->setObjectName("FunctionBtn");
    
    statsButtonLayout->addWidget(m_refreshBtn);
    statsButtonLayout->addWidget(m_aiAnalysisBtn);
    
    connect(m_refreshBtn, &QPushButton::clicked, this, &RightFunctionPanel::onRefreshStats);
    connect(m_aiAnalysisBtn, &QPushButton::clicked, this, &RightFunctionPanel::onAIAnalysis);
    
    QWidget *statsButtonWidget = new QWidget();
    statsButtonWidget->setLayout(statsButtonLayout);
    m_statsCard->addContent(statsButtonWidget);
    
    // 导出按钮单独一行
    m_statsCard->addContent(m_exportBtn);
    
    m_mainLayout->addWidget(m_statsCard);
}

void RightFunctionPanel::applyStyles()
{
    QString styles = R"(
        QWidget {
            font-family: "Microsoft YaHei", "SimHei", sans-serif;
        }
        
        RightFunctionPanel {
            background-color: #2A3F47;
            border-left: 2px solid #546E7A;
        }
        
        #PanelTitle {
            font-size: 16px;
            font-weight: bold;
            color: #FFFFFF;
            border-bottom: 1px solid #546E7A;
            padding-bottom: 8px;
            margin-bottom: 12px;
        }
        
        #ModuleCard {
            background-color: #354A54;
            border: 1px solid #546E7A;
            border-radius: 8px;
            padding: 12px;
        }
        
        #ModuleCard:hover {
            border-color: #2E5D31;
            background-color: #4A6572;
        }
        
        #ModuleTitle {
            font-size: 14px;
            font-weight: 500;
            color: #FFFFFF;
        }
        
        #ModuleIcon {
            font-size: 16px;
            color: #2E5D31;
        }
        
        #DeviceCard {
            background-color: #3D525E;
            border: 1px solid #546E7A;
            border-radius: 6px;
            padding: 8px;
        }
        
        #DeviceCard:hover {
            background-color: #4A6572;
            border-color: #2E5D31;
        }
        
        #DeviceName {
            font-size: 12px;
            color: #FFFFFF;
            font-weight: 500;
        }
        
        #DeviceStatus {
            font-size: 10px;
            color: #78909C;
        }
        
        #FunctionBtn {
            background-color: #2E5D31;
            color: #FFFFFF;
            border: none;
            border-radius: 4px;
            padding: 8px 12px;
            font-size: 12px;
            font-weight: 500;
            min-height: 32px;
        }
        
        #FunctionBtn:hover {
            background-color: #245429;
        }
        
        #FunctionBtn:pressed {
            background-color: #1a3d1d;
        }
        
        #FunctionBtn:disabled {
            background-color: #78909C;
        }
        
        #StatLabel {
            font-size: 12px;
            color: #B0BEC5;
            padding: 4px 0;
        }
        
        #CallStatus {
            font-size: 11px;
            color: #78909C;
            font-style: italic;
            text-align: center;
        }
        
        QSlider::groove:horizontal {
            border: 1px solid #546E7A;
            height: 4px;
            background: #3D525E;
            border-radius: 2px;
        }
        
        QSlider::handle:horizontal {
            background: #2E5D31;
            border: 1px solid #546E7A;
            width: 12px;
            margin: -4px 0;
            border-radius: 6px;
        }
        
        QSlider::handle:horizontal:hover {
            background: #245429;
        }
    )";
    
    setStyleSheet(styles);
}

// 槽函数实现
void RightFunctionPanel::onDeviceSelected(const QString &deviceName)
{
    m_selectedDevice = deviceName;
    
    // 更新设备选择状态
    m_robotDogCard->setActive(deviceName.contains("机器狗"));
    m_droneCard->setActive(deviceName.contains("无人机"));
    
    // 根据设备类型启用/禁用相应按钮
    bool isRobotDog = deviceName.contains("机器狗");
    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 ? "停止建图" : "自主建图");
    
    if (isMappingActive) {
        emit startMapping();
    } else {
        emit stopMapping();
    }
}

void RightFunctionPanel::onNavigationToggle()
{
    static bool isNavigationActive = false;
    isNavigationActive = !isNavigationActive;
    
    m_navigationBtn->setText(isNavigationActive ? "停止导航" : "导航避障");
    
    if (isNavigationActive) {
        emit startNavigation();
    } else {
        emit stopNavigation();
    }
}

void RightFunctionPanel::onPhotoTransmissionToggle()
{
    static bool isTransmissionActive = false;
    isTransmissionActive = !isTransmissionActive;
    
    m_photoBtn->setText(isTransmissionActive ? "停止传输" : "照片传输");
    
    if (isTransmissionActive) {
        emit startPhotoTransmission();
    } else {
        emit stopPhotoTransmission();
    }
}

void RightFunctionPanel::onPersonRecognitionToggle()
{
    static bool isRecognitionActive = false;
    isRecognitionActive = !isRecognitionActive;
    
    m_recognitionBtn->setText(isRecognitionActive ? "停止识别" : "人物识别");
    
    if (isRecognitionActive) {
        emit startPersonRecognition();
    } else {
        emit stopPersonRecognition();
    }
}

void RightFunctionPanel::onVoiceCallToggle()
{
    m_isInCall = !m_isInCall;
    
    m_voiceCallBtn->setText(m_isInCall ? "结束通话" : "开始通话");
    m_muteBtn->setEnabled(m_isInCall);
    m_callStatusLabel->setText(m_isInCall ? "通话中..." : "未连接");
    
    if (m_isInCall) {
        emit startVoiceCall();
    } else {
        emit endVoiceCall();
    }
}

void RightFunctionPanel::onRefreshStats()
{
    emit refreshEnemyStats();
    
    // 模拟刷新效果
    m_refreshBtn->setText("刷新中...");
    m_refreshBtn->setEnabled(false);
    
    QTimer::singleShot(2000, [this]() {
        m_refreshBtn->setText("刷新");
        m_refreshBtn->setEnabled(true);
    });
}

void RightFunctionPanel::onAIAnalysis()
{
    emit requestAIAnalysis();
    
    // 显示分析状态
    m_aiAnalysisBtn->setText("分析中...");
    m_aiAnalysisBtn->setEnabled(false);
    
    QTimer::singleShot(3000, [this]() {
        m_aiAnalysisBtn->setText("AI分析");
        m_aiAnalysisBtn->setEnabled(true);
    });
}

void RightFunctionPanel::updateEnemyStats(int totalEnemies, const QString &threatLevel)
{
    m_totalEnemiesLabel->setText(QString("已发现目标: %1").arg(totalEnemies));
    m_threatLevelLabel->setText(QString("威胁等级: %1").arg(threatLevel));
    
    // 根据威胁等级设置颜色
    if (threatLevel == "高") {
        m_threatLevelLabel->setStyleSheet("color: #DC143C;");
    } else if (threatLevel == "中") {
        m_threatLevelLabel->setStyleSheet("color: #FF8C00;");
    } else {
        m_threatLevelLabel->setStyleSheet("color: #4CAF50;");
    }
}

void RightFunctionPanel::updateDeviceStatus(const QString &deviceName, bool online, int battery)
{
    DeviceCard *deviceCard = nullptr;
    if (deviceName.contains("机器狗")) {
        deviceCard = m_robotDogCard;
    } else if (deviceName.contains("无人机")) {
        deviceCard = m_droneCard;
    }
    
    if (deviceCard) {
        if (online) {
            deviceCard->setStatus(QString("在线 %1%").arg(battery), QColor("#4CAF50"));
        } else {
            deviceCard->setStatus("离线", QColor("#78909C"));
        }
    }
}

#include "RightFunctionPanel.moc"