#ifndef __HEADER_EQ_HANDLER__
#define __HEADER_EQ_HANDLER__

#pragma once

#include "xmath/xmath.h"
#include <stdint.h>
#include <stdbool.h>

#define NUM_EQ_MODES 12
#define NUM_EQ_CHANS 2
#define MAX_EQ_BANDS 8
#define EQ_DISABLED_MODE 12  // 禁用EQ的模式编号（超出有效范围）
#define EQ_PRESET_MODE_MIN 0  // 预设模式最小编号
#define EQ_PRESET_MODE_MAX 9  // 预设模式最大编号（preset1-preset10）
#define EQ_USER_MODE_MIN 10   // 用户模式最小编号
#define EQ_USER_MODE_MAX 11   // 用户模式最大编号（user 1-2）
#define EQ_MODE_MAX  (NUM_EQ_MODES)    // 模式最大编号


// Filter type definition
typedef enum {
    FILTER_TYPE_BYPASS = 0x00,
    FILTER_TYPE_ALLPASS = 0x01,
    FILTER_TYPE_PEAKING = 0x02,
    FILTER_TYPE_LOWPASS = 0x03,
    FILTER_TYPE_HIGHPASS = 0x04,
    FILTER_TYPE_BANDPASS = 0x05,
    FILTER_TYPE_BANDSTOP = 0x06,
    FILTER_TYPE_NOTCH = 0x07,
    FILTER_TYPE_CONST_Q = 0x08,
    FILTER_TYPE_LOWSHELF = 0x09,
    FILTER_TYPE_HIGHSHELF = 0x0A
} filter_type_t;


// Filter parameter structure
typedef struct {
    float fc;                // Center frequency (Hz)
    float q;                 // Q value
    float bw;                // Bandwidth (Hz)
    float gain;              // Gain (dB)
    uint8_t index;           // Filter index
    filter_type_t type;      // Filter type
    uint8_t padding[2];      // 添加填充字节确保结构体对齐到4字节边界
}  filter_params_t;

// Filter biquad structure for runtime calculation
typedef struct {
    int32_t coef[5][8];      // 5×8×4 = 160字节
} filter_biquad_s32_coef_t;

// 注意：以下结构体定义暂时注释，因为它们可能在其他文件中定义
// 如果需要使用，请确保包含相应的头文件

// 优化后的Flash存储结构 - 只存储非默认滤波器
typedef struct {
    uint8_t active_bands_count;  // 实际使用的滤波器数量
    filter_params_t active_bands[MAX_EQ_BANDS];  // 只存储非bypass的滤波器
} eq_flash_data_t;

// 单个EQ参数变化记录结构（优化版）
typedef struct {
    uint32_t sample_rate;        // 采样率
    uint8_t mode;               // 模式
    uint8_t channel;            // 通道
    uint8_t band_index;         // 滤波器索引
    filter_params_t params;     // 单个滤波器参数（不包含系数）
    uint32_t timestamp;         // 时间戳
    uint8_t is_dirty;           // 脏标志
} __attribute__((packed)) eq_single_param_t;

// 单参数存储数据库
typedef struct {
    eq_single_param_t params[MAX_EQ_BANDS * 8 * 2]; // 最多存储8个模式×2通道的所有参数
    uint16_t param_count;       // 当前参数数量
    uint16_t max_params;        // 最大参数数量
    uint32_t last_save_time;    // 最后保存时间
} eq_single_param_database_t;

// 设备信息结构体
typedef struct {
    char mode_names[NUM_EQ_MODES][16];  // 模式名称数组，每个16字节
    int32_t mode_gains[NUM_EQ_MODES];   // 模式增益数组，每个4字节
} __attribute__((packed)) eq_mode_info_t;

// Define EQ data structure for each sample rate
typedef struct {
    unsigned sample_rate;  // Sample rate
    int32_t total_bshift;
    int32_t post_gain_db;
    filter_params_t bands[MAX_EQ_BANDS];  // Filter parameters
    filter_biquad_s32_t filter;  // Filter data (运行时计算)
} __attribute__((packed)) eq_mode_data_t;


uint32_t init_eq_data(unsigned sample_freq);

void clear_eq_status(unsigned sample_freq, unsigned ch_no);

int32_t handler_eq_filter( unsigned sample_freq,  uint32_t ch, int32_t new_sample);

void init_eq_hid_params(void);

// EQ参数处理函数 - 用于串口接收的EQ参数转换和存储
int process_eq_params_from_uart(uint8_t eq_mode, uint8_t eq_index, uint32_t sample_rate, float coefficients[5]);

// 模式信息处理函数
#ifdef __XC__
// 独立的增益和名称设置/获取函数
void set_mode_gain(uint8_t mode, int32_t gain);
void set_mode_name(uint8_t mode, const char* unsafe name);
void get_mode_gain(uint8_t mode, int32_t* unsafe gain);
void get_mode_name(uint8_t mode, char* unsafe name);
uint8_t process_uart_set_eq_mode(uint8_t mode);
uint8_t process_uart_get_eq_mode(uint8_t * unsafe response, uint8_t response_size);
uint8_t process_uart_set_mode_gain_and_name(uint8_t * unsafe data);
uint8_t process_uart_set_eq_params(uint8_t * unsafe data);
uint8_t process_uart_get_eq_params(uint8_t * unsafe data, uint8_t * unsafe response, uint8_t response_size);
uint8_t process_uart_reset_eq_params(uint8_t * unsafe data);
uint8_t process_uart_get_eq_mode_count(uint8_t * unsafe response, uint8_t response_size);
uint8_t process_uart_set_and_save_eq_mode(uint8_t * unsafe data);

#else
// 独立的增益和名称设置/获取函数
void set_mode_gain(uint8_t mode, int32_t gain);
void set_mode_name(uint8_t mode, const char* name);
void get_mode_gain(uint8_t mode, int32_t* gain);
void get_mode_name(uint8_t mode, char* name);
uint8_t process_uart_set_eq_mode(uint8_t mode);
uint8_t process_uart_get_eq_mode(uint8_t *response, uint8_t response_size);
uint8_t process_uart_set_mode_gain_and_name(uint8_t *data);
uint8_t process_uart_set_eq_params(uint8_t *data);
uint8_t process_uart_get_eq_params(uint8_t *data, uint8_t *response, uint8_t response_size);
uint8_t process_uart_reset_eq_params(uint8_t *data);
uint8_t process_uart_get_eq_mode_count(uint8_t *response, uint8_t response_size);
uint8_t process_uart_set_and_save_eq_mode(uint8_t *data);


#endif
void init_mode_info(void);

uint8_t reset_eq_params(uint8_t mode);

// 动态系数计算函数声明
int calculate_current_mode_coefficients(uint32_t sample_rate, uint8_t mode);

// 串口EQ命令处理函数声明
#endif //__HEADER_EQ_HANDLER__
