/**
 * @file MainWindow.cpp
 * @brief SkyLink Qt 地面站 - 主窗口实现
 * 
 * @author flowzl
 * @date 2026-01-19
 */

#include "MainWindow.h"
#include "GroundReceiver.h"
#include "DataLogger.h"
#include "DeviceInfoReceiver.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QStatusBar>
#include <QFileDialog>
#include <QDateTime>
#include <QMessageBox>
#include <QDebug>
#include <QPixmap>
#include <iomanip>
#include <sstream>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent) {
    
    this->setWindowTitle("SkyLink 地面站 - 实时视频接收系统");
    this->setGeometry(100, 100, 1280, 800);
    
    // 初始化
    init_ui();
    init_connections();
    create_menus();
    
    // 创建接收器（设置parent以确保Qt对象树正确管理）
    receiver_ = std::make_shared<GroundReceiver>(this);
    connect(receiver_.get(), &GroundReceiver::error,
            this, [this](const QString& error_msg) {
                QMessageBox::warning(this, "UDP接收错误", error_msg);
                statusBar()->showMessage("错误: " + error_msg);
            });
    receiver_->start();

    // 设备信息接收器
    device_info_receiver_ = std::make_shared<DeviceInfoReceiver>(this, 10000);
    connect(device_info_receiver_.get(), &DeviceInfoReceiver::deviceInfoReceived,
            this, &MainWindow::on_device_info_received);
    connect(device_info_receiver_.get(), &DeviceInfoReceiver::error,
            this, [this](const QString& msg) {
                statusBar()->showMessage("设备信息错误: " + msg, 2000);
            });
    device_info_receiver_->start();
    device_info_receiver_->request_info();
    
    // 创建记录器
    logger_ = std::make_shared<DataLogger>("./skylink_data");
    
    // 连接信号
    bool connected1 = connect(receiver_.get(), &GroundReceiver::frameDecoded,
                                this, &MainWindow::on_frame_received);
    bool connected2 = connect(receiver_.get(), &GroundReceiver::statsUpdated,
                               this, &MainWindow::on_stats_updated);
    
    if (!connected1) {
        qWarning() << "frameDecoded信号连接失败";
    }
    if (!connected2) {
        qWarning() << "statsUpdated信号连接失败";
    }
    
    qDebug() << "信号连接状态 - frameDecoded:" << connected1 << "statsUpdated:" << connected2;
    
    statusBar()->showMessage("就绪，等待数据...");
    
    // 确保窗口显示在最前面
    this->raise();
    this->activateWindow();
}

MainWindow::~MainWindow() {
    if (receiver_) {
        receiver_->stop();
    }
}

void MainWindow::init_ui() {
    // 中央部件
    QWidget* central = new QWidget(this);
    this->setCentralWidget(central);
    
    QVBoxLayout* main_layout = new QVBoxLayout(central);
    
    // ========================================================================
    // 视频显示区
    // ========================================================================
    
    QGroupBox* video_group = new QGroupBox("实时视频", this);
    QVBoxLayout* video_layout = new QVBoxLayout(video_group);
    
    video_label_ = new QLabel(this);
    video_label_->setMinimumSize(640, 480);
    video_label_->setAlignment(Qt::AlignCenter);
    video_label_->setText("等待视频数据...");
    video_label_->setStyleSheet(
        "background-color: #000000; "
        "color: #888888; "
        "border: 2px solid #333333; "
        "font-size: 16px;"
    );
    // 确保标签可以显示图片
    video_label_->setScaledContents(false);  // 不使用自动缩放，我们手动缩放
    video_label_->setTextFormat(Qt::PlainText);
    
    video_layout->addWidget(video_label_);
    main_layout->addWidget(video_group, 1);
    
    // ========================================================================
    // 信息和控制区
    // ========================================================================
    
    QHBoxLayout* info_control_layout = new QHBoxLayout();
    
    // GPS 信息
    QGroupBox* gps_group = new QGroupBox("GPS 信息", this);
    QVBoxLayout* gps_layout = new QVBoxLayout(gps_group);
    gps_label_ = new QLabel("纬度: -- \n经度: --\n海拔: --", this);
    gps_label_->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    gps_layout->addWidget(gps_label_);
    info_control_layout->addWidget(gps_group);
    
    // 统计信息
    QGroupBox* stats_group = new QGroupBox("实时统计", this);
    QVBoxLayout* stats_layout = new QVBoxLayout(stats_group);
    stats_label_ = new QLabel("FPS: 0\n速率: 0 KB/s\n丢包率: 0%", this);
    stats_label_->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    stats_layout->addWidget(stats_label_);
    info_control_layout->addWidget(stats_group);

    // 设备信息
    QGroupBox* device_group = new QGroupBox("设备信息", this);
    QVBoxLayout* device_layout = new QVBoxLayout(device_group);
    device_info_label_ = new QLabel("分辨率: --x--\n帧率: --\n格式: --\n设备: --\n当前FPS: --\n总帧: --", this);
    device_info_label_->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    device_layout->addWidget(device_info_label_);
    info_control_layout->addWidget(device_group);
    
    // 按钮组
    QGroupBox* btn_group = new QGroupBox("控制", this);
    QVBoxLayout* btn_layout = new QVBoxLayout(btn_group);
    
    save_frame_btn_ = new QPushButton("📸 保存帧", this);
    record_btn_ = new QPushButton("⏺ 开始录制", this);
    settings_btn_ = new QPushButton("⚙ 设置", this);
    
    record_btn_->setCheckable(true);
    
    btn_layout->addWidget(save_frame_btn_);
    btn_layout->addWidget(record_btn_);
    btn_layout->addWidget(settings_btn_);
    btn_layout->addStretch();
    
    info_control_layout->addWidget(btn_group);
    main_layout->addLayout(info_control_layout);
}

void MainWindow::init_connections() {
    connect(save_frame_btn_, &QPushButton::clicked,
            this, &MainWindow::on_save_frame_clicked);
    
    connect(record_btn_, &QPushButton::toggled,
            this, &MainWindow::on_record_toggled);
    
    connect(settings_btn_, &QPushButton::clicked,
            this, &MainWindow::on_settings_clicked);
}

void MainWindow::create_menus() {
    QMenuBar* menu = menuBar();
    
    // 文件菜单
    QMenu* file_menu = menu->addMenu("文件(&F)");
    QAction* exit_action = file_menu->addAction("退出");
    connect(exit_action, &QAction::triggered, this, &QWidget::close);
    
    // 帮助菜单
    QMenu* help_menu = menu->addMenu("帮助(&H)");
    QAction* about_action = help_menu->addAction("关于");
    connect(about_action, &QAction::triggered, [this]() {
        QMessageBox::information(this, "关于",
            "SkyLink 地面站 v1.0\n\n"
            "实时多平台视频传输系统\n"
            "Copyright 2026 flowzl"
        );
    });
}

void MainWindow::on_frame_received(const QImage& img, double lat, double lon, double alt) {
    // 调试信息
    static int frame_count = 0;
    frame_count++;
    
    if (img.isNull()) {
        qWarning() << "收到空图像，帧计数:" << frame_count;
        statusBar()->showMessage("警告: 收到空图像", 2000);
        return;
    }
    
    qDebug() << "收到图像，帧" << frame_count << "大小:" << img.width() << "x" << img.height()
             << "格式:" << img.format();
    
    current_image_ = img;
    current_lat_ = lat;
    current_lon_ = lon;
    current_alt_ = alt;
    
    // 显示图像
    QPixmap pixmap = QPixmap::fromImage(img);
    if (pixmap.isNull()) {
        qWarning() << "QPixmap转换失败";
        statusBar()->showMessage("错误: 图像转换失败", 2000);
        return;
    }
    
    // 获取标签的实际大小（如果为0则使用最小尺寸）
    int w = video_label_->width();
    int h = video_label_->height();
    if (w <= 0) w = 640;
    if (h <= 0) h = 480;
    
    // 缩放并显示
    QPixmap scaled_pixmap = pixmap.scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    if (scaled_pixmap.isNull()) {
        qWarning() << "图像缩放失败";
        statusBar()->showMessage("错误: 图像缩放失败", 2000);
        return;
    }
    
    video_label_->setPixmap(scaled_pixmap);
    video_label_->setText("");  // 清除文本，显示图片
    
    // 更新状态栏
    statusBar()->showMessage(QString("已接收 %1 帧 | 图像: %2x%3")
                             .arg(frame_count)
                             .arg(img.width())
                             .arg(img.height()), 1000);
    
    // 更新 GPS 标签
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(6);
    oss << "纬度: " << lat << "°\n";
    oss << "经度: " << lon << "°\n";
    oss << "海拔: " << std::fixed << std::setprecision(1) << alt << " m";
    gps_label_->setText(QString::fromStdString(oss.str()));
    
    // 如果正在录制，保存帧
    if (is_recording_ && logger_) {
        logger_->save_frame(img, lat, lon, alt);
    }
}

void MainWindow::on_stats_updated(int fps, int kbs) {
    std::ostringstream oss;
    oss << "FPS: " << fps << "\n";
    oss << "速率: " << kbs << " KB/s\n";
    oss << "丢包率: " << "计算中..." << "%";
    stats_label_->setText(QString::fromStdString(oss.str()));
}

void MainWindow::on_save_frame_clicked() {
    if (current_image_.isNull()) {
        QMessageBox::warning(this, "警告", "还没有接收到图像");
        return;
    }
    
    QString file = QFileDialog::getSaveFileName(this,
        "保存图像", QString(),
        "JPEG Image (*.jpg);;PNG Image (*.png)");
    
    if (!file.isEmpty()) {
        current_image_.save(file);
        statusBar()->showMessage("图像已保存: " + file);
    }
}

void MainWindow::on_record_toggled(bool checked) {
    is_recording_ = checked;
    
    if (checked) {
        record_btn_->setText("⏹ 停止录制");
        statusBar()->showMessage("开始录制...");
    } else {
        record_btn_->setText("⏺ 开始录制");
        statusBar()->showMessage("录制已停止");
    }
}

void MainWindow::on_settings_clicked() {
    QMessageBox::information(this, "设置",
        "设置功能开发中...\n\n"
        "支持的设置项：\n"
        "- UDP 绑定端口\n"
        "- 视频保存路径\n"
        "- 丢包检测\n"
        "- GPS 地图显示"
    );
}

void MainWindow::on_device_info_received(const DeviceInfo& info) {
    QString text = QString("分辨率: %1 x %2\n帧率: %3\n格式: %4\n设备: %5 (%6)\n当前FPS: %7\n总帧: %8")
                       .arg(info.width)
                       .arg(info.height)
                       .arg(info.framerate, 0, 'f', 1)
                       .arg(info.pixelFormat)
                       .arg(info.deviceName.isEmpty() ? "Unknown" : info.deviceName)
                       .arg(info.devicePath)
                       .arg(info.currentFps, 0, 'f', 1)
                       .arg(info.totalFrames);
    device_info_label_->setText(text);
    statusBar()->showMessage("已更新设备信息", 1000);
}
