#ifndef _SIGNAL_PROCESSOR_H_
#define _SIGNAL_PROCESSOR_H_
#include "data_praser.h"
#include <variant>
#include <map>
#include <vector>
#include <string>
// 滤波类型
enum class filtertype
{
    lowpass,highpass,notchpass,bandpass,bandstop
};
// 特征数据结构
struct ECGChannelFeatures {
    std::vector<int> r_peaks;     // R波位置
    float sdnn = 0.0f;            // RR间期标准差
    float rmssd = 0.0f;           // RR间期差值的均方根
};

struct FeatureSet {
    std::vector<ECGChannelFeatures> ecg_features;
    // 其他设备的特征结构...
};

// 生理指标数据结构
struct ECGIndicators {
    float heart_rate = 0.0f;      // 心率 (bpm)
    float sdnn = 0.0f;            // 心率变异性指标
    float rmssd = 0.0f;           // 心率变异性指标
    float st_elevation = 0.0f;    // ST段抬高 (mV)
    float st_depression = 0.0f;   // ST段压低 (mV)
};

struct PPGIndicators {
    float spo2 = 0.0f;            // 血氧饱和度
    float pi = 0.0f;              // 灌注指数
    float pulse_rate = 0.0f;      // 脉率
};

struct EEGIndicators {
    float alpha_power = 0.0f;     // Alpha波功率
    float beta_power = 0.0f;      // Beta波功率
    float attention = 0.0f;       // 注意力指数
};

struct IndicatorSet {
    float sqi = 0.0f;             // 信号质量指数 (0.0-1.0)
    std::vector<ECGIndicators> ecg_indicators;
    std::vector<PPGIndicators> ppg_indicators;
    std::vector<EEGIndicators> eeg_indicators;
};

// 滤波器配置
struct FilterConfig {
    std::string type;
    double cutoff_frequency = 0.0;
    double Q_factor = 0.0;
    double low_cutoff = 0.0;
    double high_cutoff = 0.0;
};

class SignalProcessor {
public:
    // 预处理信号
    SensorData preprocess_signals(const SensorData& raw_data);  // 使用DataType枚举
    SensorData preprocess_generic(const SensorData& data);
    // 新增：简化版通道级滤波处理（测试版本）
    std::vector<SensorData> process_channel_based_filtering_simple(
        const std::vector<SensorData>& data_packets);
    
    // 特征提取
    FeatureSet extract_signal_features(const SensorData& processed_data, 
                                      const std::vector<std::string>& features = {});
    
    // 计算生理指标
    IndicatorSet compute_physiological_indicators(const FeatureSet& features, 
                                                 const std::vector<std::string>& indicators = {});
    
    // 获取处理流水线配置
    std::string getProcessingPipeline(DataType device_type);  // 使用DataType枚举
    
    // 设置滤波器参数
    void setFilterParams(const std::string& filter_type, const FilterConfig& params);
    

    // 修改为public访问权限
    public:
        SensorData preprocess_ecg_12lead(const SensorData& data);
        SensorData preprocess_eeg(const SensorData& data);
        SensorData preprocess_ecg_2lead(const SensorData& data);
        SensorData preprocess_ppg(const SensorData& data);
        SensorData preprocess_respiration(const SensorData& data);
        SensorData preprocess_snore(const SensorData& data);
        SensorData preprocess_stethoscope(const SensorData& data);

    // 设备特定的特征提取
    FeatureSet extract_eeg_features(const SensorData& data);
    FeatureSet extract_ecg_features(const SensorData& data);
    FeatureSet extract_ppg_features(const SensorData& data);
    FeatureSet extract_respiration_features(const SensorData& data);
    FeatureSet extract_snore_features(const SensorData& data);
    FeatureSet extract_stethoscope_features(const SensorData& data);
    
    // 滤波器配置
    std::map<std::string, FilterConfig> filter_configs_;
    
    // 滤波器状态
    struct FilterState {
        std::vector<double> x_history;
        std::vector<double> y_history;
    };
    std::map<std::string, FilterState> filter_states_;

                // 在SignalProcessor类中添加
public:
// 带通滤波器实现
    std::vector<float> bandpass_filter(const std::vector<float>& input, 
                                                  double sample_rate,
                                                  double low_cutoff,
                                                  double high_cutoff);
    std::vector<float> Lowpass_filter(const std::vector<float>& input,
                                                    double sample_rate,
                                                    double low_cutoff) ;

    std::vector<float> filter(const std::vector<float>& input,
                                            double sample_rate,    
                                            double low_cutoff,  
                                            double high_cutoff,
                                            filtertype type);
    std::vector<float> Highpass_filter(const std::vector<float>& input,
                                        double sample_rate,
                                        double high_cutoff);
    std::vector<float> compensate_motion_artifact(const std::vector<float>& ppg,
                                             const std::vector<float>& motion);
    std::vector<float> compensate_eog_artifact(const std::vector<float>& eeg,
                                             const std::vector<float>& eog1,
                                             const std::vector<float>& eog2) ;
        // 添加自适应陷波滤波器声明
    std::vector<float> adaptive_notch_filter(const std::vector<float>& input, 
                                       double sample_rate,
                                       double target_freq,
                                       double bandwidth) ;
    std::vector<float> bandstop_filter(const std::vector<float>& input, 
                                                  double sample_rate,
                                                  double low_cutoff,
                                                  double high_cutoff);
    void normalize_amplitude(std::vector<float>& signal) ;
    
    // 添加通用预处理辅助函数
    std::vector<float> remove_dc_offset(const std::vector<float>& signal);
    std::vector<float> apply_gain(const std::vector<float>& signal, float gain);
    float calculate_correlation(const std::vector<float>& x, const std::vector<float>& y);
    float calculate_snr(const std::vector<float>& signal);
    float calculate_PPG_sqi(const std::vector<float>& red_channel, 
                                        const std::vector<float>& ir_channel);
    float calculate_ecg_sqi(const std::vector<float>& signal, double sample_rate);
    std::vector<float> remove_motion_artifacts(const std::vector<float>& signal, double sample_rate);
    
    // 新增：通道级滤波辅助方法
private:
    std::vector<std::vector<float>> apply_channel_filters(
        const std::vector<std::vector<float>>& channels, DataType data_type);
    std::vector<std::vector<float>> apply_eeg_filters(
        const std::vector<std::vector<float>>& channels);
    std::vector<std::vector<float>> apply_ecg_filters(
        const std::vector<std::vector<float>>& channels);
    std::vector<std::vector<float>> apply_ppg_filters(
        const std::vector<std::vector<float>>& channels);
    std::vector<std::vector<float>> apply_respiration_filters(
        const std::vector<std::vector<float>>& channels);
    std::vector<std::vector<float>> apply_snore_filters(
        const std::vector<std::vector<float>>& channels);
    std::vector<std::vector<float>> apply_stethoscope_filters(
        const std::vector<std::vector<float>>& channels);
    
    // 新增：单导联心电专用处理函数
    std::vector<std::vector<float>> apply_single_lead_ecg_filters(
        const std::vector<std::vector<float>>& channels);
    std::vector<float> enhance_signal_quality(
        const std::vector<float>& signal, double sample_rate);
    std::vector<float> apply_smoothing(
        const std::vector<float>& signal, int window_size);
};


#endif
 