package com.example.cmake_project_test

import android.util.Log
import type.SensorData

/**
 * 数据映射器 - 负责通道映射和数据处理
 * 处理流程：原始数据 → 解析数据 → 通道映射 → 信号处理 → 指标计算
 */
class DataMapper {
    
    companion object {
        // 加载本地库
        init {
            System.loadLibrary("cmake_project_test")
        }
    }
    
    // 本地映射器句柄
    private var mapperHandle: Long = 0
    
    /**
     * 初始化数据映射器
     */
    fun initialize(): Boolean {
        try {
            mapperHandle = createDataMapper()
            if (mapperHandle != 0L) {
                Log.d("DataMapper", "数据映射器初始化成功，句柄: $mapperHandle")
                return true
            } else {
                Log.e("DataMapper", "数据映射器初始化失败")
                return false
            }
        } catch (e: Exception) {
            Log.e("DataMapper", "初始化数据映射器时发生错误: ${e.message}")
            return false
        }
    }
    
    /**
     * 映射单个传感器数据
     */
    fun mapSensorData(sensorData: SensorData): SensorData? {
        Log.d("DataMapper", "DEBUG: DataMapper.mapSensorData被调用")
        Log.d("DataMapper", "DEBUG: mapperHandle = $mapperHandle")
        Log.d("DataMapper", "DEBUG: 输入数据类型 = ${sensorData.getDataType()}")
        
        if (mapperHandle == 0L) {
            Log.w("DataMapper", "数据映射器未初始化")
            return null
        }
        
        return try {
            Log.d("DataMapper", "DEBUG: 准备调用JNI mapSensorData")
            val mappedData = mapSensorData(mapperHandle, sensorData)
            Log.d("DataMapper", "DEBUG: JNI mapSensorData调用完成")
            
            if (mappedData != null) {
                Log.d("DataMapper", "成功映射传感器数据: ${sensorData.getDataType()}")
                Log.d("DataMapper", "DEBUG: 映射后数据类型 = ${mappedData.getDataType()}")
                Log.d("DataMapper", "DEBUG: 映射后通道数量 = ${mappedData.getChannelData()?.size ?: 0}")
                mappedData
            } else {
                Log.w("DataMapper", "映射传感器数据失败，返回原始数据")
                sensorData
            }
        } catch (e: Exception) {
            Log.e("DataMapper", "映射传感器数据时发生错误: ${e.message}")
            Log.e("DataMapper", "DEBUG: 映射异常堆栈", e)
            sensorData // 出错时返回原始数据
        }
    }
    
    /**
     * 批量映射传感器数据列表
     */
    fun mapSensorDataList(sensorDataList: List<SensorData>): List<SensorData> {
        Log.d("DataMapper", "DEBUG: DataMapper.mapSensorDataList被调用")
        Log.d("DataMapper", "DEBUG: mapperHandle = $mapperHandle")
        Log.d("DataMapper", "DEBUG: 输入数据包数量 = ${sensorDataList.size}")
        
        if (mapperHandle == 0L) {
            Log.w("DataMapper", "数据映射器未初始化")
            return sensorDataList
        }
        
        return try {
            Log.d("DataMapper", "DEBUG: 准备调用JNI mapSensorDataList")
            val mappedList = mapSensorDataList(mapperHandle, sensorDataList)
            Log.d("DataMapper", "DEBUG: JNI mapSensorDataList调用完成")
            
            if (mappedList != null) {
                Log.d("DataMapper", "成功批量映射 ${sensorDataList.size} 个传感器数据")
                Log.d("DataMapper", "DEBUG: 映射后数据包数量 = ${mappedList.size}")
                mappedList
            } else {
                Log.w("DataMapper", "批量映射失败，返回原始数据")
                sensorDataList
            }
        } catch (e: Exception) {
            Log.e("DataMapper", "批量映射传感器数据时发生错误: ${e.message}")
            Log.e("DataMapper", "DEBUG: 批量映射异常堆栈", e)
            sensorDataList // 出错时返回原始数据
        }
    }
    
    /**
     * 清理资源
     */
    fun cleanup() {
        if (mapperHandle != 0L) {
            try {
                destroyDataMapper(mapperHandle)
                mapperHandle = 0L
                Log.d("DataMapper", "数据映射器资源已清理")
            } catch (e: Exception) {
                Log.e("DataMapper", "清理数据映射器资源时发生错误: ${e.message}")
            }
        }
    }
    
    /**
     * 检查映射器是否已初始化
     */
    fun isInitialized(): Boolean = mapperHandle != 0L
    
    // ============================================================================
    // JNI函数声明
    // ============================================================================
    
    /**
     * 创建数据映射器实例
     * @return 映射器句柄，0表示失败
     */
    private external fun createDataMapper(): Long
    
    /**
     * 销毁数据映射器实例
     * @param mapperHandle 映射器句柄
     */
    private external fun destroyDataMapper(mapperHandle: Long)
    
    /**
     * 映射单个传感器数据
     * @param mapperHandle 映射器句柄
     * @param sensorData 传感器数据
     * @return 映射后的传感器数据，null表示失败
     */
    private external fun mapSensorData(mapperHandle: Long, sensorData: SensorData): SensorData?
    
    /**
     * 批量映射传感器数据列表
     * @param mapperHandle 映射器句柄
     * @param sensorDataList 传感器数据列表
     * @return 映射后的传感器数据列表，null表示失败
     */
    private external fun mapSensorDataList(mapperHandle: Long, sensorDataList: List<SensorData>): List<SensorData>?
}
