#ifndef __APP_CALIBRATION_H
#define __APP_CALIBRATION_H

#include <stdint.h>

#include "mx_log.h"
#include "resi_math_lib.h"
#include "mozen_protocol.h"
#include "cali_process.h"

#include "main.h"
//#include "bsp_config.h"

/****************************移植相关 ↓********************************/

//按项目的传感器行列填写
#define MT_AX_NUM                                   (AX_NUM)   
#define MT_AY_NUM                                   (AY_NUM) 

#ifdef CALI_POINT_NUM
#undef CALI_POINT_NUM
#endif
#define CALI_POINT_NUM                              (3)   //标定点位个数

#ifdef MAX_PRESSURE_POINTS
#undef MAX_PRESSURE_POINTS
#endif 
#define MAX_PRESSURE_POINTS                         CALI_POINT_NUM  

//未进行标定前，算法函数填入的默认参数
#define DEFAULT_MIN_TRIGGER_RES_VALUE               (1000)
#define DEFAULT_MAX_TRIGGER_RES_VALUE               (10000)
#define DEFAULT_DIV_TRIGGER_RES_VALUE               (1300)  // 根据具体项目的硬件的上拉电阻值填写
#define DEFAULT_MAX_DISPLAY_VALUE                   (255) // (255U)

/****************************移植相关 ↑ ********************************/

#define MAX_DISPLAY_ADC_VALUE                       (4096)   //最大显示值

#ifndef APP_CALI_USE_PROJECT_RUNTIME_FLAGS
#define APP_CALI_USE_PROJECT_RUNTIME_FLAGS          1U //是否使用项目运行时标志 (0:不使用 1:使用)
#endif

#ifndef APP_CALI_ENABLE_LEGACY_API
#define APP_CALI_ENABLE_LEGACY_API                  0U //是否启用旧版API (0:不启用 1:启用)
#endif

#define APP_MATH_PARAM_FRAME_SIZE                   8U //算法数学参数帧大小
#define APP_MAP_MATRIX_WORDS                        ((uint32_t)MT_AX_NUM * (uint32_t)MT_AY_NUM * (uint32_t)CALI_POINT_NUM) //矩阵字数
#define APP_MAP_MATRIX_BYTES                        (APP_MAP_MATRIX_WORDS * sizeof(uint16_t)) //矩阵字节数 (矩阵字数 * 2)
#define APP_SENSOR_FRAME_PAYLOAD_MAX_BYTES          (1U + ((uint32_t)MT_AX_NUM * (uint32_t)MT_AY_NUM * sizeof(uint16_t))) //传感器数据帧最大字节数
#define APP_CALI_STATUS_UNCALIBRATED                0x0000U // 未标定状态
#define APP_CALI_STATUS_CALIBRATED                  0xA55AU // 已标定状态

// 设备信息SN
#define DEFAULT_PN          "JJ009"
#define DEFAULT_SN          "FA0060G1A7100001"
#define DEFAULT_SW_VER      "V1.0.1"
#define DEFAULT_HW_VER      "V1.0_20250627"

#define APP_DEVICE_PROTOCOL_VER     "V1.0" // Protocol Version

//校准数据结构
typedef struct _app_math_cali_t
{
    uint16_t check_cail; // 校验值
    uint16_t sensor_data_type; // 传感器数据类型
    uint16_t min_trigger_res_value; // 最小触发电阻值
    uint16_t max_trigger_res_value; // 最大触发电阻值
    uint16_t div_trigger_res_value; // 触发电阻值除数
    uint16_t max_display_value; // 最大显示值
    uint16_t num_points; // 标定点个数
    uint16_t pressure_points[MAX_PRESSURE_POINTS]; // 标定点压力值
} app_math_cali_t;

typedef struct _app_creep_params
{
    uint8_t creep_strength;
	uint8_t creep_level;
}app_creep_params;


typedef struct _app_device_info 
{
    uint8_t pn[33];           // 型号存储
    char sn[17];           // 序列号存储
    uint8_t sw_ver[sizeof(DEFAULT_SW_VER)+1]; // 软件版本存储
    uint8_t hw_ver[sizeof(DEFAULT_HW_VER)+1]; // 硬件版本存储
    uint8_t sensor_size[10];   // 传感器尺寸
    uint8_t protocol_ver[10];  // 协议版本存储
} app_dev_info;

typedef struct {
    uint16_t max_cal;
    uint16_t min_cal;
    uint16_t ref_value;
    uint16_t adc_max;
    uint16_t max_display;
} app_legacy_math_t;

typedef struct {
    uint8_t creep_strength;
    uint8_t creep_level;
} app_legacy_creep_t;

// 设备状态 old
typedef struct 
{
    uint8_t led_sw;
    uint8_t sensor_tx_sw;
    uint8_t tx_stopping;
    uint8_t output_pick;
} app_device_status_t;

/* Compatibility typedefs after merge */
typedef app_math_cali_t app_math_params_t;
typedef app_math_cali_t pressure_params_t;

extern volatile bool  g_check_cali  ;
extern volatile bool  g_reset_takeEffect ;
extern volatile bool g_is_active_reporting;
extern pressure_params_t* pressure_params;
extern volatile bool g_is_creep_enable ;

/* Algorithm (math) parameter interfaces */
uint8_t app_math_init(uint16_t *min_trigger_res_value,
                      uint16_t *max_trigger_res_value,
                      uint16_t *div_trigger_res_value,
                      uint16_t *max_display_value,uint16_t *sensor_data_type);
uint8_t app_math_get_temp_params(app_math_cali_t *out_params);
uint8_t app_math_set_temp_params(const app_math_cali_t *params);

uint8_t app_math_write_temp_params(const uint8_t *data, uint16_t len);
uint8_t app_math_save_params(const uint8_t *data, uint16_t len);
uint8_t app_math_clear_params(void);
uint8_t app_math_read_temp_params(uint8_t *response_buf, uint16_t *response_len);
uint8_t app_math_read_solidified_params(uint8_t *response_buf, uint16_t *response_len);

/* Pressure parameter interfaces */
pressure_params_t *app_pressure_init(void);
uint8_t app_pressure_write_temp_params(const uint8_t *data, uint16_t len);
uint8_t app_pressure_read_temp_params(uint8_t *response_buf, uint16_t *response_len);
uint8_t app_pressure_save_params(const uint8_t *data, uint16_t len);
uint8_t app_pressure_read_solidified_params(uint8_t *response_buf, uint16_t *response_len);
uint8_t app_pressure_clear_params(void);

/* N-point map interfaces */
uint8_t app_map_init(void);
uint8_t app_map_write_temp_params(const uint8_t *data, uint16_t len);
uint8_t app_map_save_params(const uint8_t *data, uint16_t len);
uint8_t app_map_clear_params(void);
uint8_t app_map_read_temp_params(uint8_t *response_buf, uint16_t *response_len);
uint8_t *app_map_data_ptr(void);
uint32_t app_map_data_size(void);

/* Creep parameter interfaces */
uint8_t app_creep_write_temp_params(app_creep_params *creep_params);
uint8_t app_creep_save_params(app_creep_params *creep_params);
uint8_t app_creep_read_params(app_creep_params *creep_params);
uint8_t app_creep_clear_params(void);

/* Device information interfaces */
uint8_t app_device_get_info(app_dev_info *dev_info);
uint8_t app_device_set_info(const app_dev_info *dev_info);

/* Legacy device status interfaces */
app_device_status_t* app_device_status_get(void);
void app_device_status_set(const app_device_status_t *status);
void app_device_status_init(void);

/* Runtime state hooks for protocol layer */
void app_calibration_invalidate_runtime(void);
void app_calibration_request_reset_effect(void);
uint8_t app_calibration_check(void);
uint8_t app_calibration_get_cail_status(void);
uint8_t app_calibration_set(uint8_t calibrated);

/* Forward declaration for flash operations */
struct flash_port_ops_t;

/**
 * @brief Initialize the calibration module and underlying flash port.
 * @param flash_ops Pointer to flash operations mapping.
 */
void app_calibration_init(const struct flash_port_ops_t *flash_ops);

void app_calibration_reset_init(void);

/**
 * @brief Process raw 16-bit sensor data against calibration map
 * @param in_raw_data Input raw sensor data matrix (MT_AX_NUM x MT_AY_NUM)
 * @param out_cali_data Output buffer to store processed data
 * @return 1 on success, 0 on invalid parameters
 */
void process_calibration_frame(const uint16_t in_raw_data[MT_AX_NUM][MT_AY_NUM],
                                  uint16_t out_cali_data[MT_AX_NUM][MT_AY_NUM]);

uint8_t send_sensor_frame_8bit(const uint8_t (*sensor_data)[MT_AY_NUM]);
uint8_t send_sensor_frame_16bit(const uint16_t (*sensor_data)[MT_AY_NUM]);

uint8_t app_calibration_build_sensor8bit_payload(const uint8_t (*sensor_data)[MT_AY_NUM],                                    
                                             uint8_t *payload,
                                             uint16_t payload_size,
                                             uint16_t *payload_len);
uint8_t app_calibration_build_sensor16bit_payload(const uint16_t (*sensor_data)[MT_AY_NUM],                    
                                             uint8_t *payload,
                                             uint16_t payload_size,
                                             uint16_t *payload_len);

/* Legacy compatibility interfaces */
#if APP_CALI_ENABLE_LEGACY_API
uint8_t array_resi_params_write(const uint8_t *data, uint16_t len);
uint8_t array_resi_params_save(const uint8_t *data, uint16_t len);
uint8_t clear_array_init_info(void);
uint8_t return_array_resi_init_info(uint8_t *response_buf, uint16_t *response_len);
uint8_t return_flash_array_resi_init_info(uint8_t *response_buf, uint16_t *response_len);

pressure_params_t *init_pressure_params(void);
uint8_t write_temp_pressure_params(const uint8_t *data, uint16_t len);
uint8_t read_temp_pressure_params(uint8_t *response_buf, uint16_t *response_len);
uint8_t write_solidified_pressure_params(const uint8_t *data, uint16_t len);
uint8_t read_solidified_pressure_params(uint8_t *response_buf, uint16_t *response_len);
uint8_t clear_pressure_params_from_flash(void);

void write_cali_params_to_ram(const uint8_t *data, uint16_t len);
uint8_t save_cali_params_to_flash(const uint8_t *data, uint16_t len);
uint8_t clear_cali_params_from_flash(void);
uint8_t read_cali_params_from_ram(uint8_t *response_buf, uint16_t *response_len);
#endif

#endif /* __APP_CALIBRATION_H */
