#ifndef DATA_STORAGE_H
#define DATA_STORAGE_H

#include "main.h"
#include "app_config.h"
#include "fatfs.h"
#include "ff.h"
#include "data_packet.h"
#include "correction.h"
#include "flash_counter.h"
#include "gps_driver.h"
#include <stdint.h>

// 数据存储配置（数值由 app_config.h 中 CFG_* 统一管理）
#define DATA_STORAGE_BUFFER_SIZE    CFG_STORAGE_BUFFER_SIZE
#define DATA_STORAGE_FILE_MAX_SIZE  CFG_STORAGE_FILE_MAX_SIZE
#define DATA_STORAGE_BASE_PATH      "0:/DATA"       // 数据存储基础路径
#define DATA_STORAGE_NOFX_DIR       "0:/DATA/NOFX"  // 无GPS定位时的目录
#define DATA_STORAGE_MAX_PATH_LEN   128             // 最大路径长度

// 缓冲区状态
typedef enum {
    BUFFER_IDLE = 0,        // 缓冲区空闲
    BUFFER_WRITING,         // 正在写入数据
    BUFFER_READY_TO_FLUSH,  // 准备刷新到文件
    BUFFER_FLUSHING         // 正在刷新到文件
} BufferState_t;

// 数据存储状态
typedef enum {
    DATA_STORAGE_IDLE = 0,
    DATA_STORAGE_RECORDING,
    DATA_STORAGE_ERROR,
    DATA_STORAGE_FULL
} DataStorageState_t;

// 数据存储统计信息
typedef struct {
    uint32_t total_samples;
    uint32_t current_file_size;
    uint32_t file_count;
    uint32_t error_count;
    DataStorageState_t state;
    char current_filename[256];
} DataStorageStats_t;

// 双缓冲区结构
typedef struct {
    uint8_t data[DATA_STORAGE_BUFFER_SIZE];
    uint16_t index;
    BufferState_t state;
} DataBuffer_t;

// 数据存储句柄
typedef struct {
    FIL file;
    DataBuffer_t buffers[2];        // 双缓冲区
    uint8_t active_buffer;          // 当前活动缓冲区索引 (0 或 1)
    uint8_t flush_buffer;           // 待刷新缓冲区索引
    DataStorageStats_t stats;
    uint8_t initialized;
    uint8_t flush_in_progress;      // 刷新进行中标志
    char current_session_path[DATA_STORAGE_MAX_PATH_LEN];  // 当前会话文件夹路径
    uint32_t file_counter;          // NOFX模式下的文件序号（从Flash读取后+1000）
} DataStorageHandle_t;

// 函数声明
HAL_StatusTypeDef DataStorage_Init(DataStorageHandle_t *handle);
HAL_StatusTypeDef DataStorage_StartRecording(DataStorageHandle_t *handle);
HAL_StatusTypeDef DataStorage_StopRecording(DataStorageHandle_t *handle);
HAL_StatusTypeDef DataStorage_WriteData(DataStorageHandle_t *handle, const DataPacket_t *packet);
HAL_StatusTypeDef DataStorage_WriteCorrectedData(DataStorageHandle_t *handle, const CorrectedDataPacket_t *result);
HAL_StatusTypeDef DataStorage_Flush(DataStorageHandle_t *handle);
void DataStorage_GetStats(DataStorageHandle_t *handle, DataStorageStats_t *stats);
HAL_StatusTypeDef DataStorage_CreateNewFile(DataStorageHandle_t *handle);

// 文件夹管理函数
HAL_StatusTypeDef DataStorage_CreateSessionFolder(DataStorageHandle_t *handle);

// 双缓冲区管理函数
HAL_StatusTypeDef DataStorage_SwitchBuffer(DataStorageHandle_t *handle);
HAL_StatusTypeDef DataStorage_FlushBuffer(DataStorageHandle_t *handle, uint8_t buffer_index);
void DataStorage_ProcessBackgroundTasks(DataStorageHandle_t *handle);

// 缓冲区可用性检查函数
uint8_t DataStorage_IsBufferAvailable(DataStorageHandle_t *handle, uint32_t required_size);

// 通用字节流写入（支持任意包类型，方案Y混流存储）
HAL_StatusTypeDef DataStorage_WriteRawBytes(DataStorageHandle_t *handle, const uint8_t *data, uint16_t size);

#endif // DATA_STORAGE_H
