/**
 * @file DeviceListPanel.cpp
 * @brief 设备列表面板组件实现
 * @author CasualtySightPlus Team
 * @date 2024-12-01
 * @version 1.0
 */

#include "ui/components/DeviceListPanel.h"

// Qt GUI头文件
#include <QDebug>
#include <QMessageBox>
#include <QApplication>
#include <QSplitter>

DeviceListPanel::DeviceListPanel(QWidget *parent)
    : QWidget(parent)
    , m_titleLabel(nullptr)
    , m_searchEdit(nullptr)
    , m_filterComboBox(nullptr)
    , m_deviceCountLabel(nullptr)
    , m_addUAVButton(nullptr)
    , m_addDogButton(nullptr)
    , m_refreshButton(nullptr)
    , m_scrollArea(nullptr)
    , m_scrollWidget(nullptr)
    , m_deviceListLayout(nullptr)
    , m_mainLayout(nullptr)
    , m_headerLayout(nullptr)
    , m_searchLayout(nullptr)
    , m_buttonLayout(nullptr)
    , m_currentFilterType(DeviceFilterType::All)
    , m_isMonitoringActive(false)
    , m_uavDatabase(nullptr)
    , m_dogDatabase(nullptr)
    , m_totalDeviceCount(0)
    , m_onlineDeviceCount(0)
    , m_uavCount(0)
    , m_dogCount(0)
{
    // 初始化数据库连接
    m_uavDatabase = UAVDatabase::getInstance();
    m_dogDatabase = DogDatabase::getInstance();
    
    // 创建状态监控定时器
    m_statusMonitorTimer = new QTimer(this);
    m_statusMonitorTimer->setInterval(STATUS_MONITOR_INTERVAL);
    
    setupUI();
    setupStyle();
    connectSignals();
    
    // 初始加载设备列表
    refreshDeviceList();
    
    qDebug() << "DeviceListPanel created successfully";
}

DeviceListPanel::~DeviceListPanel()
{
    stopStatusMonitoring();
    clearAllDeviceCards();
    qDebug() << "DeviceListPanel destroyed";
}

void DeviceListPanel::setupUI()
{
    // 创建主布局
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setContentsMargins(PANEL_PADDING, PANEL_PADDING, PANEL_PADDING, PANEL_PADDING);
    m_mainLayout->setSpacing(12);
    
    // === 头部区域 ===
    m_headerLayout = new QHBoxLayout();
    
    // 面板标题
    m_titleLabel = new QLabel("🤖 设备管理");
    m_titleLabel->setFont(QFont("Arial", 16, QFont::Bold));
    
    // 设备数量标签
    m_deviceCountLabel = new QLabel("设备: 0");
    m_deviceCountLabel->setFont(QFont("Arial", 10));
    
    m_headerLayout->addWidget(m_titleLabel);
    m_headerLayout->addStretch();
    m_headerLayout->addWidget(m_deviceCountLabel);
    
    // === 搜索和过滤区域 ===
    m_searchLayout = new QHBoxLayout();
    
    // 搜索框
    m_searchEdit = new QLineEdit();
    m_searchEdit->setPlaceholderText("搜索设备...");
    m_searchEdit->setMaximumHeight(32);
    
    // 过滤下拉框
    m_filterComboBox = new QComboBox();
    m_filterComboBox->addItem("全部设备", static_cast<int>(DeviceFilterType::All));
    m_filterComboBox->addItem("无人机", static_cast<int>(DeviceFilterType::UAV));
    m_filterComboBox->addItem("机器狗", static_cast<int>(DeviceFilterType::Dog));
    m_filterComboBox->addItem("在线设备", static_cast<int>(DeviceFilterType::Online));
    m_filterComboBox->addItem("离线设备", static_cast<int>(DeviceFilterType::Offline));
    m_filterComboBox->setMaximumHeight(32);
    m_filterComboBox->setMaximumWidth(100);
    
    m_searchLayout->addWidget(m_searchEdit);
    m_searchLayout->addWidget(m_filterComboBox);
    
    // === 操作按钮区域 ===
    m_buttonLayout = new QHBoxLayout();
    
    m_addUAVButton = new QPushButton("+ 无人机");
    m_addUAVButton->setMaximumHeight(32);
    m_addUAVButton->setMaximumWidth(80);
    
    m_addDogButton = new QPushButton("+ 机器狗");
    m_addDogButton->setMaximumHeight(32);
    m_addDogButton->setMaximumWidth(80);
    
    m_refreshButton = new QPushButton("🔄");
    m_refreshButton->setMaximumHeight(32);
    m_refreshButton->setMaximumWidth(40);
    m_refreshButton->setToolTip("刷新设备列表");
    
    m_buttonLayout->addWidget(m_addUAVButton);
    m_buttonLayout->addWidget(m_addDogButton);
    m_buttonLayout->addStretch();
    m_buttonLayout->addWidget(m_refreshButton);
    
    // === 设备列表滚动区域 ===
    m_scrollArea = new QScrollArea();
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_scrollArea->setMinimumHeight(300); // 确保足够的显示空间
    
    m_scrollWidget = new QWidget();
    m_deviceListLayout = new QVBoxLayout(m_scrollWidget);
    m_deviceListLayout->setSpacing(8); // 减小间距使卡片更紧凑
    m_deviceListLayout->setContentsMargins(4, 4, 4, 4); // 小边距
    m_deviceListLayout->addStretch(); // 底部弹性空间
    
    m_scrollArea->setWidget(m_scrollWidget);
    
    // === 组装主布局 ===
    m_mainLayout->addLayout(m_headerLayout);
    
    // 添加分隔线
    QLabel *separatorLine = new QLabel();
    separatorLine->setFixedHeight(2);
    separatorLine->setStyleSheet("background-color: rgba(82, 194, 242, 0.3);");
    m_mainLayout->addWidget(separatorLine);
    
    m_mainLayout->addLayout(m_searchLayout);
    m_mainLayout->addLayout(m_buttonLayout);
    m_mainLayout->addWidget(m_scrollArea, 1); // 占据剩余空间
}

void DeviceListPanel::setupStyle()
{
    // 面板整体样式 - 更现代的军用风格
    setStyleSheet(
        "DeviceListPanel {"
        "    background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1,"
        "                stop:0 rgb(15, 22, 32), stop:1 rgb(25, 35, 45));"
        "    border: 1px solid rgba(82, 194, 242, 0.3);"
        "    border-radius: 8px;"
        "}"
    );
    
    // 标题样式 - 增强军用特色
    m_titleLabel->setStyleSheet(
        "QLabel {"
        "    color: rgb(255, 255, 255);"
        "    background: transparent;"
        "    border: none;"
        "    font-weight: bold;"
        "    text-shadow: 0px 1px 2px rgba(0, 0, 0, 0.8);"
        "}"
    );
    
    // 设备数量标签样式 - 添加背景和边框
    m_deviceCountLabel->setStyleSheet(
        "QLabel {"
        "    color: rgb(82, 194, 242);"
        "    background: rgba(82, 194, 242, 0.1);"
        "    border: 1px solid rgba(82, 194, 242, 0.3);"
        "    border-radius: 4px;"
        "    padding: 2px 6px;"
        "    font-weight: bold;"
        "}"
    );
    
    // 搜索框样式 - 改善视觉效果
    m_searchEdit->setStyleSheet(
        "QLineEdit {"
        "    background: rgba(45, 65, 95, 0.9);"
        "    color: rgb(220, 230, 242);"
        "    border: 2px solid rgba(82, 194, 242, 0.4);"
        "    border-radius: 6px;"
        "    padding: 6px 10px;"
        "    font-size: 13px;"
        "}"
        "QLineEdit:focus {"
        "    border-color: rgba(82, 194, 242, 0.8);"
        "    background: rgba(45, 65, 95, 1.0);"
        "}"
        "QLineEdit:hover {"
        "    border-color: rgba(82, 194, 242, 0.6);"
        "}"
    );
    
    // 过滤下拉框样式
    m_filterComboBox->setStyleSheet(
        "QComboBox {"
        "    background: rgba(45, 65, 95, 0.9);"
        "    color: rgb(220, 230, 242);"
        "    border: 2px solid rgba(82, 194, 242, 0.4);"
        "    border-radius: 6px;"
        "    padding: 4px 8px;"
        "    font-size: 12px;"
        "}"
        "QComboBox:hover {"
        "    border-color: rgba(82, 194, 242, 0.6);"
        "}"
        "QComboBox::drop-down {"
        "    border: none;"
        "}"
        "QComboBox::down-arrow {"
        "    color: rgb(82, 194, 242);"
        "}"
        "QComboBox QAbstractItemView {"
        "    background: rgba(45, 65, 95, 0.95);"
        "    color: rgb(220, 230, 242);"
        "    border: 1px solid rgba(82, 194, 242, 0.5);"
        "    selection-background-color: rgba(82, 194, 242, 0.3);"
        "}"
    );
    
    // 按钮统一样式 - 军用风格
    QString buttonStyle = 
        "QPushButton {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                stop:0 rgba(45, 65, 95, 0.8), "
        "                stop:1 rgba(25, 40, 65, 0.8));"
        "    color: rgb(220, 230, 242);"
        "    border: 2px solid rgba(82, 194, 242, 0.5);"
        "    border-radius: 6px;"
        "    padding: 6px 12px;"
        "    font-size: 12px;"
        "    font-weight: bold;"
        "}"
        "QPushButton:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                stop:0 rgba(82, 194, 242, 0.6), "
        "                stop:1 rgba(45, 120, 180, 0.6));"
        "    border-color: rgba(82, 194, 242, 0.8);"
        "    color: white;"
        "}"
        "QPushButton:pressed {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                stop:0 rgba(82, 194, 242, 0.8), "
        "                stop:1 rgba(45, 120, 180, 0.8));"
        "    border-color: rgba(82, 194, 242, 1.0);"
        "}";
    
    m_addUAVButton->setStyleSheet(buttonStyle);
    m_addDogButton->setStyleSheet(buttonStyle);
    m_refreshButton->setStyleSheet(buttonStyle);
    
    // 滚动区域样式
    m_scrollArea->setStyleSheet(
        "QScrollArea {"
        "    background: transparent;"
        "    border: none;"
        "}"
        "QScrollBar:vertical {"
        "    background: rgba(25, 35, 45, 0.5);"
        "    width: 8px;"
        "    border-radius: 4px;"
        "}"
        "QScrollBar::handle:vertical {"
        "    background: rgba(82, 194, 242, 0.6);"
        "    border-radius: 4px;"
        "    min-height: 20px;"
        "}"
        "QScrollBar::handle:vertical:hover {"
        "    background: rgba(82, 194, 242, 0.8);"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "    border: none;"
        "    background: none;"
        "}"
    );
}

void DeviceListPanel::connectSignals()
{
    // 搜索和过滤信号
    connect(m_searchEdit, &QLineEdit::textChanged, this, &DeviceListPanel::onSearchTextChanged);
    connect(m_filterComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &DeviceListPanel::onFilterTypeChanged);
    
    // 按钮信号
    connect(m_addUAVButton, &QPushButton::clicked, this, &DeviceListPanel::onAddUAVClicked);
    connect(m_addDogButton, &QPushButton::clicked, this, &DeviceListPanel::onAddDogClicked);
    connect(m_refreshButton, &QPushButton::clicked, this, &DeviceListPanel::refreshDeviceList);
    
    // 状态监控定时器
    connect(m_statusMonitorTimer, &QTimer::timeout, this, &DeviceListPanel::onStatusMonitorTimer);
}

QString DeviceListPanel::getSelectedDeviceId() const
{
    return m_selectedDeviceId;
}

int DeviceListPanel::getDeviceCountByType(const QString &type) const
{
    if (type == "uav") return m_uavCount;
    else if (type == "dog") return m_dogCount;
    else return m_totalDeviceCount;
}

int DeviceListPanel::getOnlineDeviceCount() const
{
    return m_onlineDeviceCount;
}

void DeviceListPanel::refreshDeviceList()
{
    qDebug() << "Refreshing device list...";
    
    // 清除现有设备卡片
    clearAllDeviceCards();
    
    // 从数据库加载设备
    m_allDevices = loadDevicesFromDatabase();
    
    // 创建设备卡片
    for (const auto &device : m_allDevices) {
        DeviceCard *card = createDeviceCard(device);
        if (card) {
            m_deviceCards[device.id] = card;
        }
    }
    
    // 应用当前的搜索和过滤条件
    applySearchAndFilter();
    
    // 更新统计信息
    updateDeviceCountStats();
    
    qDebug() << "Device list refreshed. Total devices:" << m_allDevices.size();
}

void DeviceListPanel::addDevice(const DeviceInfo &device)
{
    // 检查设备是否已存在
    if (m_deviceCards.contains(device.id)) {
        qWarning() << "Device already exists:" << device.id;
        return;
    }
    
    // 添加到设备列表
    m_allDevices.append(device);
    
    // 创建设备卡片
    DeviceCard *card = createDeviceCard(device);
    if (card) {
        m_deviceCards[device.id] = card;
        
        // 应用搜索和过滤
        applySearchAndFilter();
        updateDeviceCountStats();
        
        qDebug() << "Device added:" << device.name;
    }
}

void DeviceListPanel::removeDevice(const QString &deviceId)
{
    if (!m_deviceCards.contains(deviceId)) {
        qWarning() << "Device not found:" << deviceId;
        return;
    }
    
    // 移除设备卡片
    DeviceCard *card = m_deviceCards.take(deviceId);
    card->deleteLater();
    
    // 从设备列表中移除
    for (int i = m_allDevices.size() - 1; i >= 0; --i) {
        if (m_allDevices[i].id == deviceId) {
            m_allDevices.removeAt(i);
            break;
        }
    }
    
    // 清除选择状态
    if (m_selectedDeviceId == deviceId) {
        m_selectedDeviceId.clear();
    }
    
    updateDeviceCountStats();
    
    qDebug() << "Device removed:" << deviceId;
}

void DeviceListPanel::updateDevice(const DeviceInfo &device)
{
    if (!m_deviceCards.contains(device.id)) {
        qWarning() << "Device not found for update:" << device.id;
        return;
    }
    
    // 更新设备信息
    for (auto &existingDevice : m_allDevices) {
        if (existingDevice.id == device.id) {
            existingDevice = device;
            break;
        }
    }
    
    // 更新设备卡片
    DeviceCard *card = m_deviceCards[device.id];
    card->updateDeviceInfo(device);
    
    updateDeviceCountStats();
    
    qDebug() << "Device updated:" << device.name;
}

void DeviceListPanel::updateDeviceStatus(const QString &deviceId, DeviceStatus status)
{
    if (!m_deviceCards.contains(deviceId)) {
        qWarning() << "Device not found for status update:" << deviceId;
        return;
    }
    
    // 更新设备状态
    for (auto &device : m_allDevices) {
        if (device.id == deviceId) {
            device.status = status;
            break;
        }
    }
    
    // 更新设备卡片状态
    DeviceCard *card = m_deviceCards[deviceId];
    card->updateDeviceStatus(status);
    
    updateDeviceCountStats();
    
    qDebug() << "Device status updated:" << deviceId << "status:" << static_cast<int>(status);
}

void DeviceListPanel::startStatusMonitoring()
{
    if (!m_isMonitoringActive) {
        m_statusMonitorTimer->start();
        m_isMonitoringActive = true;
        qDebug() << "Status monitoring started";
    }
}

void DeviceListPanel::stopStatusMonitoring()
{
    if (m_isMonitoringActive) {
        m_statusMonitorTimer->stop();
        m_isMonitoringActive = false;
        qDebug() << "Status monitoring stopped";
    }
}

DeviceCard* DeviceListPanel::createDeviceCard(const DeviceInfo &device)
{
    DeviceCard *card = new DeviceCard(device, this);
    
    // 连接设备卡片信号
    connect(card, &DeviceCard::deviceSelected, this, &DeviceListPanel::onDeviceCardSelected);
    connect(card, &DeviceCard::deviceControlRequested, this, &DeviceListPanel::onDeviceCardControlRequested);
    connect(card, &DeviceCard::deviceLocationRequested, this, &DeviceListPanel::onDeviceCardLocationRequested);
    connect(card, &DeviceCard::deviceDetailsRequested, this, &DeviceListPanel::onDeviceCardDetailsRequested);
    
    return card;
}

QList<DeviceInfo> DeviceListPanel::loadDevicesFromDatabase()
{
    QList<DeviceInfo> devices;
    
    qDebug() << "Loading devices from database...";
    
    // 添加测试数据以便查看效果
    DeviceInfo uav1;
    uav1.id = "UAV001";
    uav1.name = "侦察机-01";
    uav1.type = "uav";
    uav1.ipAddress = "192.168.1.101";
    uav1.port = 8080;
    uav1.longitude = 116.40;
    uav1.latitude = 39.90;
    uav1.signalStrength = 85;
    uav1.batteryLevel = 90;
    uav1.status = DeviceStatus::Online;
    uav1.lastHeartbeat = QDateTime::currentDateTime();
    uav1.createdAt = QDateTime::currentDateTime().addDays(-7);
    uav1.updatedAt = QDateTime::currentDateTime();
    devices.append(uav1);
    
    DeviceInfo uav2;
    uav2.id = "UAV002";
    uav2.name = "侦察机-02";
    uav2.type = "uav";
    uav2.ipAddress = "192.168.1.102";
    uav2.port = 8080;
    uav2.longitude = 116.42;
    uav2.latitude = 39.92;
    uav2.signalStrength = 72;
    uav2.batteryLevel = 65;
    uav2.status = DeviceStatus::Warning;
    uav2.lastHeartbeat = QDateTime::currentDateTime().addSecs(-30);
    uav2.createdAt = QDateTime::currentDateTime().addDays(-5);
    uav2.updatedAt = QDateTime::currentDateTime();
    devices.append(uav2);
    
    DeviceInfo dog1;
    dog1.id = "DOG001";
    dog1.name = "巡逻犬-Alpha";
    dog1.type = "dog";
    dog1.ipAddress = "192.168.1.201";
    dog1.port = 9090;
    dog1.longitude = 116.38;
    dog1.latitude = 39.88;
    dog1.signalStrength = 95;
    dog1.batteryLevel = 80;
    dog1.status = DeviceStatus::Online;
    dog1.lastHeartbeat = QDateTime::currentDateTime();
    dog1.createdAt = QDateTime::currentDateTime().addDays(-3);
    dog1.updatedAt = QDateTime::currentDateTime();
    devices.append(dog1);
    
    DeviceInfo dog2;
    dog2.id = "DOG002";
    dog2.name = "巡逻犬-Beta";
    dog2.type = "dog";
    dog2.ipAddress = "192.168.1.202";
    dog2.port = 9090;
    dog2.longitude = 116.44;
    dog2.latitude = 39.86;
    dog2.signalStrength = 0;
    dog2.batteryLevel = 25;
    dog2.status = DeviceStatus::Offline;
    dog2.lastHeartbeat = QDateTime::currentDateTime().addSecs(-300);
    dog2.createdAt = QDateTime::currentDateTime().addDays(-1);
    dog2.updatedAt = QDateTime::currentDateTime().addSecs(-120);
    devices.append(dog2);
    
    // TODO: 实现真实的数据库查询
    // auto uavList = m_uavDatabase->getAllDevices();
    // auto dogList = m_dogDatabase->getAllDevices();
    
    return devices;
}

void DeviceListPanel::applySearchAndFilter()
{
    qDebug() << "Applying search and filter. Keyword:" << m_currentSearchKeyword
             << "Filter:" << static_cast<int>(m_currentFilterType);
    
    // 隐藏所有设备卡片
    for (auto card : m_deviceCards) {
        card->hide();
        m_deviceListLayout->removeWidget(card);
    }
    
    // 根据条件显示匹配的设备卡片
    for (const auto &device : m_allDevices) {
        if (isDeviceMatchFilter(device)) {
            DeviceCard *card = m_deviceCards[device.id];
            if (card) {
                m_deviceListLayout->insertWidget(m_deviceListLayout->count() - 1, card);
                card->show();
            }
        }
    }
}

bool DeviceListPanel::isDeviceMatchFilter(const DeviceInfo &device) const
{
    // 检查搜索关键词
    if (!m_currentSearchKeyword.isEmpty()) {
        if (!device.name.contains(m_currentSearchKeyword, Qt::CaseInsensitive) &&
            !device.ipAddress.contains(m_currentSearchKeyword, Qt::CaseInsensitive)) {
            return false;
        }
    }
    
    // 检查过滤类型
    switch (m_currentFilterType) {
        case DeviceFilterType::UAV:
            return device.type == "uav";
        case DeviceFilterType::Dog:
            return device.type == "dog";
        case DeviceFilterType::Online:
            return device.isOnline();
        case DeviceFilterType::Offline:
            return !device.isOnline();
        case DeviceFilterType::All:
        default:
            return true;
    }
}

void DeviceListPanel::updateDeviceCountStats()
{
    m_totalDeviceCount = m_allDevices.size();
    m_onlineDeviceCount = 0;
    m_uavCount = 0;
    m_dogCount = 0;
    
    for (const auto &device : m_allDevices) {
        if (device.isOnline()) {
            m_onlineDeviceCount++;
        }
        if (device.type == "uav") {
            m_uavCount++;
        } else if (device.type == "dog") {
            m_dogCount++;
        }
    }
    
    // 更新显示
    m_deviceCountLabel->setText(QString("设备: %1 (在线: %2)")
                                .arg(m_totalDeviceCount)
                                .arg(m_onlineDeviceCount));
    
    // 发送统计信号
    emit deviceCountChanged(m_totalDeviceCount, m_onlineDeviceCount);
}

void DeviceListPanel::clearAllDeviceCards()
{
    for (auto card : m_deviceCards) {
        card->deleteLater();
    }
    m_deviceCards.clear();
    m_allDevices.clear();
    m_selectedDeviceId.clear();
}

// 槽函数实现
void DeviceListPanel::onSearchTextChanged(const QString &text)
{
    m_currentSearchKeyword = text.trimmed();
    applySearchAndFilter();
}

void DeviceListPanel::onFilterTypeChanged(int index)
{
    m_currentFilterType = static_cast<DeviceFilterType>(m_filterComboBox->itemData(index).toInt());
    applySearchAndFilter();
}

void DeviceListPanel::onAddUAVClicked()
{
    emit addDeviceRequested("uav");
}

void DeviceListPanel::onAddDogClicked()
{
    emit addDeviceRequested("dog");
}

void DeviceListPanel::onDeviceCardSelected(const QString &deviceId)
{
    // 清除之前的选择
    if (!m_selectedDeviceId.isEmpty() && m_deviceCards.contains(m_selectedDeviceId)) {
        m_deviceCards[m_selectedDeviceId]->setSelected(false);
    }
    
    m_selectedDeviceId = deviceId;
    emit deviceSelected(deviceId);
}

void DeviceListPanel::onDeviceCardControlRequested(const QString &deviceId)
{
    emit deviceControlRequested(deviceId);
}

void DeviceListPanel::onDeviceCardLocationRequested(const QString &deviceId)
{
    emit deviceLocationRequested(deviceId);
}

void DeviceListPanel::onDeviceCardDetailsRequested(const QString &deviceId)
{
    emit deviceDetailsRequested(deviceId);
}

void DeviceListPanel::setSearchKeyword(const QString &keyword)
{
    m_currentSearchKeyword = keyword.trimmed();
    applySearchAndFilter();
}

void DeviceListPanel::setFilterType(DeviceFilterType filterType)
{
    m_currentFilterType = filterType;
    applySearchAndFilter();
}

void DeviceListPanel::selectDevice(const QString &deviceId)
{
    if (m_deviceCards.contains(deviceId)) {
        // 清除之前的选择
        if (!m_selectedDeviceId.isEmpty() && m_deviceCards.contains(m_selectedDeviceId)) {
            m_deviceCards[m_selectedDeviceId]->setSelected(false);
        }
        
        // 设置新选择
        m_selectedDeviceId = deviceId;
        m_deviceCards[deviceId]->setSelected(true);
        
        emit deviceSelected(deviceId);
    }
}

void DeviceListPanel::clearSelection()
{
    if (!m_selectedDeviceId.isEmpty() && m_deviceCards.contains(m_selectedDeviceId)) {
        m_deviceCards[m_selectedDeviceId]->setSelected(false);
    }
    m_selectedDeviceId.clear();
}

void DeviceListPanel::onStatusMonitorTimer()
{
    // 定期检查设备状态
    // 这里可以实现设备状态的定期更新逻辑
    qDebug() << "Status monitor timer triggered";
    
    // TODO: 实现真实的状态检查逻辑
    // 例如：检查设备心跳、网络连接状态等
}

void DeviceListPanel::updateDeviceCount()
{
    // 更新统计数据
    m_totalDeviceCount = m_deviceCards.size();
    m_onlineDeviceCount = 0;
    m_uavCount = 0;
    m_dogCount = 0;
    
    // 统计各类设备数量
    for (const auto& card : m_deviceCards) {
        const DeviceInfo& info = card->getDeviceInfo();
        
        if (info.isOnline()) {
            m_onlineDeviceCount++;
        }
        
        if (info.type == "uav") {
            m_uavCount++;
        } else if (info.type == "dog") {
            m_dogCount++;
        }
    }
    
    // 更新显示文本 - 更详细的信息
    QString statusText = QString("设备: %1 | 在线: %2 | 🚁UAV: %3 | 🤖DOG: %4")
                        .arg(m_totalDeviceCount)
                        .arg(m_onlineDeviceCount)
                        .arg(m_uavCount)
                        .arg(m_dogCount);
    
    m_deviceCountLabel->setText(statusText);
    
    // 根据在线率设置颜色
    double onlineRate = m_totalDeviceCount > 0 ? (double)m_onlineDeviceCount / m_totalDeviceCount : 0.0;
    QString color;
    if (onlineRate >= 0.8) {
        color = "rgb(0, 255, 0)";      // 绿色 - 状态良好
    } else if (onlineRate >= 0.5) {
        color = "rgb(255, 255, 0)";    // 黄色 - 状态一般
    } else {
        color = "rgb(255, 100, 100)";  // 红色 - 状态不佳
    }
    
    m_deviceCountLabel->setStyleSheet(
        QString("QLabel {"
                "    color: %1;"
                "    background: rgba(82, 194, 242, 0.1);"
                "    border: 1px solid rgba(82, 194, 242, 0.3);"
                "    border-radius: 4px;"
                "    padding: 2px 6px;"
                "    font-weight: bold;"
                "    font-size: 11px;"
                "}").arg(color)
    );
    
    // 发送设备数量变化信号
    emit deviceCountChanged(m_totalDeviceCount, m_onlineDeviceCount);
}

void DeviceListPanel::updateDeviceListDisplay()
{
    // 清空现有显示
    for (auto card : m_deviceCards) {
        card->hide();
        m_deviceListLayout->removeWidget(card);
    }
    
    // 根据过滤条件重新显示设备
    QString searchText = m_searchEdit->text().toLower();
    
    for (auto card : m_deviceCards) {
        const DeviceInfo& info = card->getDeviceInfo();
        bool shouldShow = true;
        
        // 应用搜索过滤
        if (!searchText.isEmpty()) {
            if (!info.name.toLower().contains(searchText) &&
                !info.ipAddress.toLower().contains(searchText) &&
                !info.id.toLower().contains(searchText)) {
                shouldShow = false;
            }
        }
        
        // 应用类型过滤
        switch (m_currentFilterType) {
            case DeviceFilterType::UAV:
                if (info.type != "uav") shouldShow = false;
                break;
            case DeviceFilterType::Dog:
                if (info.type != "dog") shouldShow = false;
                break;
            case DeviceFilterType::Online:
                if (!info.isOnline()) shouldShow = false;
                break;
            case DeviceFilterType::Offline:
                if (info.isOnline()) shouldShow = false;
                break;
            case DeviceFilterType::All:
            default:
                // 显示所有设备
                break;
        }
        
        if (shouldShow) {
            card->show();
            m_deviceListLayout->insertWidget(m_deviceListLayout->count() - 1, card);
        }
    }
    
    // 更新设备计数
    updateDeviceCount();
}

void DeviceListPanel::applyDeviceFilter()
{
    // 获取当前过滤类型
    int filterIndex = m_filterComboBox->currentIndex();
    QVariant filterData = m_filterComboBox->itemData(filterIndex);
    m_currentFilterType = static_cast<DeviceFilterType>(filterData.toInt());
    
    // 更新显示
    updateDeviceListDisplay();
}