package com.example.cmake_project_test

import type.SensorData

/**
 * 设备类型帮助类
 * 负责设备类型名称映射和通道数据详情构建
 */
object DeviceTypeHelper {
    
    /**
     * 根据数据类型获取设备名称
     */
    fun getDeviceName(dataType: SensorData.DataType): String {
        return when (dataType) {
            SensorData.DataType.EEG -> "脑电设备"
            SensorData.DataType.ECG_2LEAD -> "胸腹设备"
            SensorData.DataType.PPG -> "血氧设备"
            SensorData.DataType.ECG_12LEAD -> "12导联心电"
            SensorData.DataType.STETHOSCOPE -> "数字听诊"
            SensorData.DataType.SNORE -> "鼾声设备"
            SensorData.DataType.RESPIRATION -> "呼吸/姿态"
            SensorData.DataType.MIT_BIH -> "MIT-BIH"
            else -> "未知设备"
        }
    }
    
    /**
     * 构建通道数据详情字符串
     */
    fun buildChannelDetails(
        data: List<SensorData>, 
        maxPackets: Int = Constants.MAX_DETAIL_PACKETS, 
        maxChannels: Int = Constants.MAX_DISPLAY_CHANNELS, 
        maxSamples: Int = Constants.MAX_DISPLAY_SAMPLES
    ): String {
        if (data.isEmpty()) {
            return "无通道数据"
        }

        val details = mutableListOf<String>()

        data.take(maxPackets).forEachIndexed { packetIndex, sensorData ->
            if (sensorData.channelData.isNullOrEmpty()) {
                details.add("数据包 ${packetIndex + 1}: 无通道数据")
                return@forEachIndexed
            }

            details.add("数据包 ${packetIndex + 1} (${getDeviceName(sensorData.dataType ?: SensorData.DataType.EEG)}):")

            sensorData.channelData.take(maxChannels).forEachIndexed { channelIndex, channel ->
                if (channel.isNullOrEmpty()) {
                    details.add("  通道 ${channelIndex + 1}: 空数据")
                    return@forEachIndexed
                }
                
                // 只显示前几个采样点
                val sampleCount = minOf(maxSamples, channel.size)
                val channelDataStr = channel.take(sampleCount).joinToString(", ") { "%.1f".format(it) }

                details.add("  通道 ${channelIndex + 1}: ${sampleCount}/${channel.size} 采样点")
                details.add("    示例: $channelDataStr${if (channel.size > sampleCount) "..." else ""}")
            }

            if (sensorData.channelData.size > maxChannels) {
                details.add("    ... 还有 ${sensorData.channelData.size - maxChannels} 个通道")
            }

            // 添加分隔线
            details.add("")
        }

        if (data.size > maxPackets) {
            details.add("... 还有 ${data.size - maxPackets} 个数据包")
        }

        return details.joinToString("\n")
    }
}
