package com.xyzshell.andinfo.libs

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorDirectChannel
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Build
import androidx.annotation.RequiresApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow

// 传感器信息工具类
class SensorInfo(private val context: Context) {

    // 传感器管理器
    private val sensorManager = context.getSystemService(SensorManager::class.java)

    /**
     * 获取所有传感器列表
     * @return 传感器列表
     */
    fun getAllSensors(): List<Sensor> {
        return sensorManager.getSensorList(Sensor.TYPE_ALL)
    }

    /**
     * 获取单个传感器的详细信息
     * @param sensor 传感器对象
     * @return 传感器详细信息数据类
     */
    fun getSensorDetail(sensor: Sensor): SensorDetail {
        return SensorDetail(
            name = sensor.name, // 传感器名称
            id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) sensor.id.takeIf { it > 0 } else null, // 传感器ID（Android N及以上）
            stringType = sensor.stringType, // 传感器类型字符串
            type = sensor.type, // 传感器类型
            vendor = sensor.vendor, // 供应商
            version = sensor.version, // 版本号
            isDynamicSensor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) sensor.isDynamicSensor else false, // 是否为动态传感器
            isWakeUpSensor = sensor.isWakeUpSensor, // 是否为唤醒传感器
            fifoMaxEventCount = sensor.fifoMaxEventCount, // FIFO最大事件数
            fifoReservedEventCount = sensor.fifoReservedEventCount, // FIFO保留事件数
            minDelay = sensor.minDelay, // 最小延迟（微秒）
            maxDelay = sensor.maxDelay, // 最大延迟（微秒）
            maximumRange = sensor.maximumRange, // 最大量程
            power = sensor.power, // 功耗（mA）
            reportingMode = sensor.reportingMode, // 报告模式
            resolution = sensor.resolution, // 分辨率
            isAdditionalInfoSupported = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) sensor.isAdditionalInfoSupported else false, // 是否支持附加信息
            highestDirectReportRateLevel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) sensor.highestDirectReportRateLevel else SensorDirectChannel.RATE_STOP // 最高直报速率等级
        )
    }

    /**
     * 判断是否支持动态传感器发现
     * @return 是否支持
     */
    fun isDynamicSensorDiscoverySupported(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sensorManager.isDynamicSensorDiscoverySupported
        } else {
            false
        }
    }

    // ==================== 温度传感器 ====================

    /**
     * 获取所有温度传感器列表
     * @return 温度传感器数据列表
     */
    fun getTemperatureSensors(): List<TemperatureSensorData> {
        val sensors = mutableListOf<TemperatureSensorData>()

        // 环境温度传感器
        sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE)?.let {
            sensors.add(TemperatureSensorData(
                name = it.name,
                type = "环境温度",
                manufacturer = it.vendor,
                model = it.stringType,
                resolution = it.resolution,
                maximumRange = it.maximumRange,
                power = it.power,
                isWakeUpSensor = it.isWakeUpSensor
            ))
        }

        // 设备温度传感器（已弃用但某些设备仍支持）
        @Suppress("DEPRECATION")
        sensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE)?.let {
            sensors.add(TemperatureSensorData(
                name = it.name,
                type = "设备温度",
                manufacturer = it.vendor,
                model = it.stringType,
                resolution = it.resolution,
                maximumRange = it.maximumRange,
                power = it.power,
                isWakeUpSensor = it.isWakeUpSensor
            ))
        }

        return sensors
    }

    /**
     * 实时获取温度传感器数据流
     * @param sensorType 传感器类型
     * @return 温度数据流
     */
    fun getTemperatureFlow(sensorType: Int = Sensor.TYPE_AMBIENT_TEMPERATURE): Flow<Float> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(sensorType)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(event.values[0]) // 温度值（摄氏度）
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 加速度传感器 ====================

    /**
     * 获取加速度传感器信息
     * @return 加速度传感器数据，如果不存在返回null
     */
    fun getAccelerometerInfo(): AccelerometerData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) ?: return null
        return AccelerometerData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取加速度传感器数据流
     * @return 加速度数据流（包含x, y, z轴）
     */
    fun getAccelerometerFlow(): Flow<ThreeAxisData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(ThreeAxisData(
                    x = event.values[0], // x轴加速度 (m/s²)
                    y = event.values[1], // y轴加速度 (m/s²)
                    z = event.values[2]  // z轴加速度 (m/s²)
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 磁力传感器 ====================

    /**
     * 获取磁力传感器信息
     * @return 磁力传感器数据，如果不存在返回null
     */
    fun getMagnetometerInfo(): MagnetometerData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) ?: return null
        return MagnetometerData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取磁力传感器数据流
     * @return 磁力数据流（包含x, y, z轴和总磁场强度）
     */
    fun getMagnetometerFlow(): Flow<MagneticFieldData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                val x = event.values[0]
                val y = event.values[1]
                val z = event.values[2]
                val total = kotlin.math.sqrt(x * x + y * y + z * z) // 计算总磁场强度

                trySend(MagneticFieldData(
                    x = x,     // x轴磁场强度 (μT)
                    y = y,     // y轴磁场强度 (μT)
                    z = z,     // z轴磁场强度 (μT)
                    total = total // 总磁场强度 (μT)
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 方向传感器 ====================

    /**
     * 获取方向传感器信息
     * @return 方向传感器数据，如果不存在返回null
     */
    @Suppress("DEPRECATION")
    fun getOrientationInfo(): OrientationSensorData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) ?: return null
        return OrientationSensorData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取方向传感器数据流
     * @return 方向数据流（包含方位角、俯仰角、横滚角和方向）
     */
    @Suppress("DEPRECATION")
    fun getOrientationFlow(): Flow<OrientationData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                val azimuth = event.values[0]
                trySend(OrientationData(
                    azimuth = azimuth,        // 方位角 (0-360°)
                    pitch = event.values[1],  // 俯仰角 (-180 to 180°)
                    roll = event.values[2],   // 横滚角 (-90 to 90°)
                    direction = getDirection(azimuth) // 方向文字描述
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    /**
     * 根据方位角获取方向描述
     */
    private fun getDirection(azimuth: Float): String {
        return when {
            azimuth >= 337.5f || azimuth < 22.5f -> "北"
            azimuth < 67.5f -> "东北"
            azimuth < 112.5f -> "东"
            azimuth < 157.5f -> "东南"
            azimuth < 202.5f -> "南"
            azimuth < 247.5f -> "西南"
            azimuth < 292.5f -> "西"
            azimuth < 337.5f -> "西北"
            else -> "未知"
        }
    }

    // ==================== 陀螺仪传感器 ====================

    /**
     * 获取陀螺仪传感器信息
     * @return 陀螺仪传感器数据，如果不存在返回null
     */
    fun getGyroscopeInfo(): GyroscopeData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) ?: return null
        return GyroscopeData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取陀螺仪传感器数据流
     * @return 陀螺仪数据流（包含x, y, z轴角速度）
     */
    fun getGyroscopeFlow(): Flow<ThreeAxisData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(ThreeAxisData(
                    x = event.values[0], // x轴角速度 (rad/s)
                    y = event.values[1], // y轴角速度 (rad/s)
                    z = event.values[2]  // z轴角速度 (rad/s)
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 环境光传感器 ====================

    /**
     * 获取环境光传感器信息
     * @return 环境光传感器数据，如果不存在返回null
     */
    fun getLightSensorInfo(): LightSensorData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) ?: return null
        return LightSensorData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取环境光传感器数据流
     * @return 环境光数据流（包含当前值、最小值、平均值、最大值）
     */
    fun getLightSensorFlow(): Flow<LightData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        var minValue = Float.MAX_VALUE
        var maxValue = Float.MIN_VALUE
        var sum = 0f
        var count = 0

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                val currentValue = event.values[0]

                // 更新统计数据
                minValue = minOf(minValue, currentValue)
                maxValue = maxOf(maxValue, currentValue)
                sum += currentValue
                count++

                trySend(LightData(
                    current = currentValue,     // 当前光照强度 (lx)
                    min = minValue,             // 最小值 (lx)
                    avg = sum / count,          // 平均值 (lx)
                    max = maxValue              // 最大值 (lx)
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 重力传感器 ====================

    /**
     * 获取重力传感器信息
     * @return 重力传感器数据，如果不存在返回null
     */
    fun getGravitySensorInfo(): GravitySensorData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) ?: return null
        return GravitySensorData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取重力传感器数据流
     * @return 重力数据流（包含x, y, z轴）
     */
    fun getGravitySensorFlow(): Flow<ThreeAxisData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(ThreeAxisData(
                    x = event.values[0], // x轴重力加速度 (m/s²)
                    y = event.values[1], // y轴重力加速度 (m/s²)
                    z = event.values[2]  // z轴重力加速度 (m/s²)
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 线性加速度传感器 ====================

    /**
     * 获取线性加速度传感器信息
     * @return 线性加速度传感器数据，如果不存在返回null
     */
    fun getLinearAccelerationInfo(): LinearAccelerationData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION) ?: return null
        return LinearAccelerationData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取线性加速度传感器数据流
     * @return 线性加速度数据流（包含x, y, z轴）
     */
    fun getLinearAccelerationFlow(): Flow<ThreeAxisData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(ThreeAxisData(
                    x = event.values[0], // x轴线性加速度 (m/s²)
                    y = event.values[1], // y轴线性加速度 (m/s²)
                    z = event.values[2]  // z轴线性加速度 (m/s²)
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 旋转矢量传感器 ====================

    /**
     * 获取旋转矢量传感器信息
     * @return 旋转矢量传感器数据，如果不存在返回null
     */
    fun getRotationVectorInfo(): RotationVectorSensorData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) ?: return null
        return RotationVectorSensorData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取旋转矢量传感器数据流
     * @return 旋转矢量数据流（包含x, y, z的sin值和cos值）
     */
    fun getRotationVectorFlow(): Flow<RotationVectorData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(RotationVectorData(
                    xSin = event.values[0], // x轴旋转矢量分量 x*sin(θ/2)
                    ySin = event.values[1], // y轴旋转矢量分量 y*sin(θ/2)
                    zSin = event.values[2], // z轴旋转矢量分量 z*sin(θ/2)
                    cos = if (event.values.size > 3) event.values[3] else {
                        // 如果没有提供cos值，可以计算：cos(θ/2)
                        val sinSum = event.values[0] * event.values[0] +
                                   event.values[1] * event.values[1] +
                                   event.values[2] * event.values[2]
                        kotlin.math.sqrt(1 - sinSum)
                    }
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 游戏旋转矢量传感器 ====================

    /**
     * 获取游戏旋转矢量传感器信息
     * @return 游戏旋转矢量传感器数据，如果不存在返回null
     */
    fun getGameRotationVectorInfo(): GameRotationVectorSensorData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR) ?: return null
        return GameRotationVectorSensorData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取游戏旋转矢量传感器数据流
     * @return 游戏旋转矢量数据流（包含x, y, z的sin值和cos值）
     */
    fun getGameRotationVectorFlow(): Flow<RotationVectorData> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(RotationVectorData(
                    xSin = event.values[0], // x轴旋转矢量分量 x*sin(θ/2)
                    ySin = event.values[1], // y轴旋转矢量分量 y*sin(θ/2)
                    zSin = event.values[2], // z轴旋转矢量分量 z*sin(θ/2)
                    cos = if (event.values.size > 3) event.values[3] else {
                        val sinSum = event.values[0] * event.values[0] +
                                   event.values[1] * event.values[1] +
                                   event.values[2] * event.values[2]
                        kotlin.math.sqrt(1 - sinSum)
                    }
                ))
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 步数传感器 ====================

    /**
     * 获取步数传感器信息
     * @return 步数传感器数据，如果不存在返回null
     */
    fun getStepCounterInfo(): StepCounterSensorData? {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) ?: return null
        return StepCounterSensorData(
            manufacturer = sensor.vendor,
            model = sensor.stringType,
            resolution = sensor.resolution,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            isWakeUpSensor = sensor.isWakeUpSensor
        )
    }

    /**
     * 实时获取步数传感器数据流
     * @return 步数数据流
     */
    fun getStepCounterFlow(): Flow<Long> = callbackFlow {
        val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)

        if (sensor == null) {
            close()
            return@callbackFlow
        }

        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                trySend(event.values[0].toLong()) // 步数（自上次重启以来）
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }

        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }

    // ==================== 数据类定义 ====================

    /**
     * 传感器详细信息数据类
     */
    data class SensorDetail(
        val name: String, // 传感器名称
        val id: Int?, // 传感器ID
        val stringType: String, // 类型字符串
        val type: Int, // 类型
        val vendor: String, // 供应商
        val version: Int, // 版本
        val isDynamicSensor: Boolean, // 是否动态
        val isWakeUpSensor: Boolean, // 是否唤醒
        val fifoMaxEventCount: Int, // FIFO最大事件数
        val fifoReservedEventCount: Int, // FIFO保留事件数
        val minDelay: Int, // 最小延迟
        val maxDelay: Int, // 最大延迟
        val maximumRange: Float, // 最大量程
        val power: Float, // 功耗
        val reportingMode: Int, // 报告模式
        val resolution: Float, // 分辨率
        val isAdditionalInfoSupported: Boolean, // 是否支持附加信息
        val highestDirectReportRateLevel: Int // 最高直报速率等级
    )

    /**
     * 温度传感器数据类
     */
    data class TemperatureSensorData(
        val name: String,           // 传感器名称
        val type: String,           // 传感器类型（环境温度/设备温度）
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (°C)
        val maximumRange: Float,    // 最大量程 (°C)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 加速度传感器数据类
     */
    data class AccelerometerData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (m/s²)
        val maximumRange: Float,    // 最大量程 (m/s²)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 三轴数据类（用于加速度、陀螺仪、重力、线性加速度等）
     */
    data class ThreeAxisData(
        val x: Float, // x轴数值
        val y: Float, // y轴数值
        val z: Float  // z轴数值
    )

    /**
     * 磁力传感器数据类
     */
    data class MagnetometerData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (μT)
        val maximumRange: Float,    // 最大量程 (μT)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 磁场数据类
     */
    data class MagneticFieldData(
        val x: Float,     // x轴磁场强度 (μT)
        val y: Float,     // y轴磁场强度 (μT)
        val z: Float,     // z轴磁场强度 (μT)
        val total: Float  // 总磁场强度 (μT)
    )

    /**
     * 方向传感器数据类
     */
    data class OrientationSensorData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (°)
        val maximumRange: Float,    // 最大量程 (°)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 方向数据类
     */
    data class OrientationData(
        val azimuth: Float,   // 方位角 (0-360°)
        val pitch: Float,     // 俯仰角 (-180 to 180°)
        val roll: Float,      // 横滚角 (-90 to 90°)
        val direction: String // 方向描述（北、东北、东等）
    )

    /**
     * 陀螺仪传感器数据类
     */
    data class GyroscopeData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (rad/s)
        val maximumRange: Float,    // 最大量程 (rad/s)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 环境光传感器数据类
     */
    data class LightSensorData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (lx)
        val maximumRange: Float,    // 最大量程 (lx)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 环境光数据类
     */
    data class LightData(
        val current: Float, // 当前光照强度 (lx)
        val min: Float,     // 最小值 (lx)
        val avg: Float,     // 平均值 (lx)
        val max: Float      // 最大值 (lx)
    )

    /**
     * 重力传感器数据类
     */
    data class GravitySensorData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (m/s²)
        val maximumRange: Float,    // 最大量程 (m/s²)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 线性加速度传感器数据类
     */
    data class LinearAccelerationData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率 (m/s²)
        val maximumRange: Float,    // 最大量程 (m/s²)
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 旋转矢量传感器数据类
     */
    data class RotationVectorSensorData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率
        val maximumRange: Float,    // 最大量程
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 旋转矢量数据类
     */
    data class RotationVectorData(
        val xSin: Float, // x*sin(θ/2)
        val ySin: Float, // y*sin(θ/2)
        val zSin: Float, // z*sin(θ/2)
        val cos: Float   // cos(θ/2)
    )

    /**
     * 游戏旋转矢量传感器数据类
     */
    data class GameRotationVectorSensorData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率
        val maximumRange: Float,    // 最大量程
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    /**
     * 步数传感器数据类
     */
    data class StepCounterSensorData(
        val manufacturer: String,   // 制造商
        val model: String,          // 型号
        val resolution: Float,      // 分辨率
        val maximumRange: Float,    // 最大量程
        val power: Float,           // 功耗 (mA)
        val isWakeUpSensor: Boolean // 是否为唤醒传感器
    )

    companion object {
        // 传感器报告模式对应中文
        val sensorReportingModeToStringResId = mapOf(
            Sensor.REPORTING_MODE_CONTINUOUS to "连续",
            Sensor.REPORTING_MODE_ON_CHANGE to "变化时",
            Sensor.REPORTING_MODE_ONE_SHOT to "单次",
            Sensor.REPORTING_MODE_SPECIAL_TRIGGER to "特殊触发",
        )

        // 直通通道速率等级对应中文（Android O及以上）
        @RequiresApi(Build.VERSION_CODES.O)
        val sensorDirectReportModeRatesToStringResId = mapOf(
            SensorDirectChannel.RATE_STOP to "停止",
            SensorDirectChannel.RATE_NORMAL to "正常",
            SensorDirectChannel.RATE_FAST to "快速",
            SensorDirectChannel.RATE_VERY_FAST to "非常快速",
        )
    }
}
