#ifndef __ET_WAVEFORM_ANALYSIS_H__
#define __ET_WAVEFORM_ANALYSIS_H__

#include "et_zero_cross.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

// Structure for storing waveform analysis results
typedef struct
{
    et_zero_cross_t zc;
    float           first_peak;        // Peak amplitude of first half-cycle
    float           second_peak;       // Peak amplitude of second half-cycle
    float           first_mean;        // Mean value of first half-cycle
    float           second_mean;       // Mean value of second half-cycle
    float           sim_ratio;         // Similarity ratio between consecutive half-cycles
    uint32_t        first_peak_idx;    // Index of first half-cycle peak
    uint32_t        second_peak_idx;   // Index of second half-cycle peak
    uint32_t        zc_count;          // Number of zero crossings in the analyzed segment
    uint32_t        first_sample_cnt;  // Number of samples in first half-cycle
    uint32_t        second_sample_cnt; // Number of samples in second half-cycle
} et_waveform_analysis_t;

#ifdef __cplusplus
extern "C"
{
#endif

/**
 * @brief Analyze waveform characteristics (et_zc_point_t input)
 * @param analysis Pointer to et_waveform_analysis_t structure for storing results
 * @param point    Pointer to input et_zc_point_t array
 * @param length   Number of samples
 * @param offset   DC offset to subtract
 * @return int     0 on success, -1 on failure
 */
int et_waveform_analyze_point(et_waveform_analysis_t *analysis, const et_zc_point_t *point, uint32_t length,
                              uint16_t offset);

/**
 * @brief Create and initialize a waveform analysis structure
 * @param analysis Pointer to et_waveform_analysis_t structure
 * @return int      0 on success, -1 on failure
 */
int et_waveform_analysis_create(et_waveform_analysis_t *analysis, uint32_t max_pos);

void et_waveform_analysis_destroy(et_waveform_analysis_t *analysis);

/**
 * @brief Calculate the ratio between first and second half-cycle mean values
 * @param analysis Pointer to et_waveform_analysis_t structure with analysis results
 * @return float    Ratio between first and second half-cycle mean values
 */
float et_waveform_mean_ratio(const et_waveform_analysis_t *analysis);

float et_waveform_peak_diff(const et_waveform_analysis_t *analysis);

float et_waveform_peak_ratio(const et_waveform_analysis_t *analysis);
/**
 * @brief Calculate the difference between first and second half-cycle mean values
 * @param analysis Pointer to et_waveform_analysis_t structure with analysis results
 * @return float    Difference between first and second half-cycle mean values
 */
float et_waveform_mean_diff(const et_waveform_analysis_t *analysis);

/**
 * @brief Detect if mean values are balanced within a threshold
 * @param analysis Pointer to et_waveform_analysis_t structure with analysis results
 * @param threshold Balance threshold (0.0-1.0)
 * @return int     1 if mean values are balanced, 0 otherwise
 */
int et_waveform_mean_balanced(const et_waveform_analysis_t *analysis, float threshold);

/**
 * @brief Calculate the ratio of sample counts between first and second half-cycles
 * @param analysis Pointer to et_waveform_analysis_t structure with analysis results
 * @return float    Ratio of first to second half-cycle sample counts
 */
float et_waveform_sample_count_ratio(const et_waveform_analysis_t *analysis);

/**
 * @brief Analyze consecutive half-cycles defined by three zero crossings
 * @param analysis Pointer to et_waveform_analysis_t structure for storing results
 * @param point    Pointer to input et_zc_point_t array
 * @param length   Number of samples
 * @param offset   DC offset to subtract
 * @return int     0 on success, -1 on failure
 */
int et_waveform_analyze_consecutive_half_cycles(et_waveform_analysis_t *analysis, const et_zc_point_t *point,
                                                uint32_t length, uint16_t offset);

/**
 * @brief Calculate the similarity ratio between consecutive half-cycles
 * @param analysis Pointer to et_waveform_analysis_t structure with analysis results
 * @return float    Similarity ratio (1.0 = identical, 0.0 = completely different)
 */
float et_waveform_sim_ratio(const et_waveform_analysis_t *analysis);

#ifdef __cplusplus
}
#endif

#endif // ET_WAVEFORM_ANALYSIS_H__
