/*
 * @Author: mypx
 * @Date: 2025-08-07 13:14:49
 * @LastEditTime: 2025-08-14 11:15:00
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: 
 */
#ifndef __ET_TREND_H__
#define __ET_TREND_H__
#include "etk_utils.h"

#define ET_TREND_WINDOW_MIN 3
#define ET_TREND_WINDOW_MAX 64

typedef enum
{
    TREND_INVALID = -1, // Invalid input parameters
    TREND_STABLE  = 0,  // Stable trend
    TREND_INCREASING,   // Increasing trend
    TREND_DECREASING,   // Decreasing trend
    TREND_NO_VALID_DATA // Insufficient valid data (all outliers)
} ET_TrendResult;

typedef enum
{
    OUTLIER_NONE = 0, // No outlier filtering
    OUTLIER_MAX,      // Filter only maximum values
    OUTLIER_MIN,      // Filter only minimum values
    OUTLIER_BOTH      // Filter both maximum and minimum values
} ET_OutlierMode;

typedef struct
{
    uint8_t        window_size;  // Window size for trend determination
    float          threshold;    // Trend determination threshold
    ET_OutlierMode outlier_mode; // Outlier processing mode
}et_trend_config_t;

typedef struct
{
    const float   *data;         // Input data array
    uint16_t       length;       // Total data length
    et_trend_config_t config; // Trend configuration parameters
} et_trend_t;

#ifdef __cplusplus
extern "C"
{
#endif

/**
 * @brief Detect trend in time series data
 * 
 * This function analyzes the trend of input data by comparing the average values
 * of the first and second halves of the data within a specified window.
 * It can optionally filter out outliers before performing the trend analysis.
 *
 * @param cfg Pointer to the trend detection configuration structure
 * @param out_avg_diff Pointer to store the average difference (can be NULL)
 * @return ET_TrendResult Trend detection result
 *         - TREND_INVALID: Invalid input parameters
 *         - TREND_STABLE: Stable trend (difference within threshold)
 *         - TREND_INCREASING: Increasing trend (difference > threshold)
 *         - TREND_DECREASING: Decreasing trend (difference < -threshold)
 *         - TREND_NO_VALID_DATA: Insufficient valid data after outlier filtering
 */
ET_TrendResult et_trend_detect(const et_trend_t *cfg, float *out_avg_diff);

int et_trend_init(et_trend_t *trend, et_trend_config_t *cfg);

#ifdef __cplusplus
}
#endif
#endif