#ifndef _INDICATOR_CAL_H
#define _INDICATOR_CAL_H

#include "cpp/data_praser.h"
#include <jni.h>
#include <android/log.h>
#include <vector>
#include <map>
#include <string>
#include <memory>

// 指标计算器类
class MetricsCalculator
{
public:
    // 构造函数和析构函数
    MetricsCalculator();
    ~MetricsCalculator();
    
    // ECG相关指标
    float calculate_heart_rate_ecg(const SensorData& ecg_signal, float sample_rate);
    float calculate_t_wave_amplitude(const std::vector<float>& ecg_signal);
    float calculate_qrs_width(const std::vector<float>& ecg_signal, float sample_rate);
    float calculate_st_offset(const std::vector<float>& ecg_signal, float sample_rate);
    
    // PPG相关指标
    float calculate_heart_rate_ppg(const std::vector<float>& ppg_signal, float sample_rate);
    float calculate_spo2(const SensorData& ppg_data);
    float calculate_perfusion_index(const SensorData& ppg_data);
    float calculate_pulse_width(const std::vector<float>& ppg_signal);
    float calculate_amplitude_ratio(const SensorData& ppg_data);
    
    // HRV相关指标
    float calculate_sdnn(const std::vector<float>& rr_intervals);
    float calculate_rmssd(const std::vector<float>& rr_intervals);
    float calculate_pnn50(const std::vector<float>& rr_intervals);
    float calculate_triangular_index(const std::vector<float>& rr_intervals);
    
    // 信号处理
    std::vector<float> detect_r_peaks(const std::vector<float>& ecg_signal, float sample_rate);
    std::vector<float> detect_pulse_peaks(const std::vector<float>& ppg_signal, float sample_rate);
    float calculate_signal_quality(const std::vector<float>& signal);
    float estimate_heart_rate_from_frequency(const std::vector<float>& signal, float sample_rate);
    
    // 综合指标计算
    std::map<std::string, float> calculate_all_ecg_metrics(const SensorData& ecg_data, float sample_rate);
    std::map<std::string, float> calculate_all_ppg_metrics(const SensorData& ppg_data, float sample_rate);
    std::map<std::string, float> calculate_all_hrv_metrics(const std::vector<float>& rr_intervals);
    
    // 完整数据处理流程
    std::map<std::string, float> process_complete_pipeline(const SensorData& raw_data, float sample_rate);
};   

// JNI函数声明
extern "C" {
    // 创建指标计算器实例
    JNIEXPORT jlong JNICALL Java_com_example_cmake_1project_1test_IndicatorCalculator_createIndicatorCalculator(JNIEnv* env, jobject thiz);
    
    // 销毁指标计算器实例
    JNIEXPORT void JNICALL Java_com_example_cmake_1project_1test_IndicatorCalculator_destroyIndicatorCalculator(JNIEnv* env, jobject thiz, jlong calculatorHandle);
    
    // 完整数据处理流程
    JNIEXPORT jobject JNICALL Java_com_example_cmake_1project_1test_IndicatorCalculator_processCompletePipeline(JNIEnv* env, jobject thiz, jlong calculatorHandle, jobject sensorData, jfloat sampleRate);
    
    // 单独指标计算
    JNIEXPORT jobject JNICALL Java_com_example_cmake_1project_1test_IndicatorCalculator_calculateECGMetrics(JNIEnv* env, jobject thiz, jlong calculatorHandle, jobject sensorData, jfloat sampleRate);
    JNIEXPORT jobject JNICALL Java_com_example_cmake_1project_1test_IndicatorCalculator_calculatePPGMetrics(JNIEnv* env, jobject thiz, jlong calculatorHandle, jobject sensorData, jfloat sampleRate);
    JNIEXPORT jobject JNICALL Java_com_example_cmake_1project_1test_IndicatorCalculator_calculateHRVMetrics(JNIEnv* env, jobject thiz, jlong calculatorHandle, jobject rrIntervals);
}

// 辅助函数声明
jobject convertMetricsMapToJava(JNIEnv* env, const std::map<std::string, float>& metrics);
std::vector<float> convertJavaFloatListToVector(JNIEnv* env, jobject floatList);

#endif