/**
 * @file DataLogger.cpp
 * @brief SkyLink Qt 地面站 - 数据记录实现
 * 
 * @author flowzl
 * @date 2026-01-19
 */

#include "DataLogger.h"
#include <QDir>
#include <QDateTime>
#include <QDebug>

DataLogger::DataLogger(const QString& data_dir)
    : data_dir_(data_dir), frame_counter_(0) {
    ensure_directory();
}

DataLogger::~DataLogger() {
    end_session();
}

bool DataLogger::ensure_directory() {
    QDir dir(data_dir_);
    if (!dir.exists()) {
        return dir.mkpath(data_dir_);
    }
    return true;
}

bool DataLogger::create_log_file() {
    QString log_path = QString("%1/session_%2.csv").arg(data_dir_, current_session_);
    log_file_ = std::make_unique<QFile>(log_path);
    
    if (!log_file_->open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "无法创建日志文件:" << log_path;
        return false;
    }
    
    log_stream_ = std::make_unique<QTextStream>(log_file_.get());
    *log_stream_ << "FrameID,Timestamp,Latitude,Longitude,Altitude,ImageFile\n";
    
    return true;
}

QString DataLogger::start_session() {
    end_session();
    
    current_session_ = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
    frame_counter_ = 0;
    
    QString session_dir = QString("%1/%2").arg(data_dir_, current_session_);
    QDir dir;
    if (!dir.mkpath(session_dir)) {
        qWarning() << "无法创建会话目录:" << session_dir;
        return QString();
    }
    
    if (!create_log_file()) {
        return QString();
    }
    
    qInfo() << "会话已启动:" << current_session_;
    return current_session_;
}

void DataLogger::end_session() {
    if (log_stream_) {
        log_stream_->flush();
    }
    
    if (log_file_) {
        log_file_->close();
        log_file_.reset();
    }
    
    log_stream_.reset();
    current_session_.clear();
}

bool DataLogger::save_frame(const QImage& image, double lat, double lon, double alt) {
    if (image.isNull() || current_session_.isEmpty()) {
        return false;
    }
    
    // 生成文件名
    QString filename = QString("frame_%1.jpg").arg(frame_counter_, 6, 10, QChar('0'));
    QString filepath = QString("%1/%2/%3").arg(data_dir_, current_session_, filename);
    
    // 保存图像
    if (!image.save(filepath, "JPEG", 90)) {
        qWarning() << "无法保存图像:" << filepath;
        return false;
    }
    
    // 记录日志
    if (log_stream_) {
        double timestamp = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0;
        *log_stream_ << frame_counter_ << ","
                    << timestamp << ","
                    << lat << ","
                    << lon << ","
                    << alt << ","
                    << filename << "\n";
        log_stream_->flush();
    }
    
    frame_counter_++;
    return true;
}

void DataLogger::save_stats(int fps, int kbs, double packet_loss) {
    // 实现可选的统计信息保存
    if (log_stream_) {
        *log_stream_ << "# FPS: " << fps
                    << ", KB/s: " << kbs
                    << ", PacketLoss: " << packet_loss << "%\n";
        log_stream_->flush();
    }
}
