package com.xyzshell.andinfo.libs

import com.xyzshell.andinfo.libs.cpu.models.Cache
import com.xyzshell.andinfo.libs.cpu.models.Cluster
import com.xyzshell.andinfo.libs.cpu.models.Core
import com.xyzshell.andinfo.libs.cpu.models.Package
import com.xyzshell.andinfo.libs.cpu.models.Processor
import com.xyzshell.andinfo.libs.cpu.models.UarchInfo
import com.xyzshell.andinfo.libs.cpu.utils.CpuInfoUtils

class CpuInfo {
    val processors: List<Processor>
        get() = CpuInfoUtils.getProcessors()

    val cores: List<Core>
        get() = CpuInfoUtils.getCores()

    val clusters: List<Cluster>
        get() = CpuInfoUtils.getClusters()

    val packages: List<Package>
        get() = CpuInfoUtils.getPackages()

    val uarchs: List<UarchInfo>
        get() = CpuInfoUtils.getUarchs()

    val l1iCaches: List<Cache>
        get() = CpuInfoUtils.getL1iCaches()

    val l1dCaches: List<Cache>
        get() = CpuInfoUtils.getL1dCaches()

    val l2Caches: List<Cache>
        get() = CpuInfoUtils.getL2Caches()

    val l3Caches: List<Cache>
        get() = CpuInfoUtils.getL3Caches()

    val l4Caches: List<Cache>
        get() = CpuInfoUtils.getL4Caches()

    fun hardware(): String {
        return if (processors.isNotEmpty()) {
            processors[0].cpuPackage?.name ?: "未知"
        } else {
            "未知"
        }
    }

    fun text(): String {
        val stringBuilder = StringBuilder()
        stringBuilder.append("处理器数量: ${processors.size}\n")
        processors.forEachIndexed { index, processor ->
            stringBuilder.append("  处理器 ${index}: ${processor.cpuPackage?.name ?: "未知"}\n")
            stringBuilder.append("    核心: ${processor.core?.coreId ?: "未知"}\n")
            stringBuilder.append("    集群: ${processor.cluster?.clusterId ?: "未知"}\n")
        }
        stringBuilder.append("核心数量: ${cores.size}\n")
        cores.forEachIndexed { index, core ->
            stringBuilder.append("  核心 ${index}: ID=${core.coreId}, 频率=${core.frequency}Hz\n")
        }
        // 可以根据需要添加更多详细信息
        return stringBuilder.toString()
    }

    companion object {
        init {
            System.loadLibrary("andinfo")
        }
    }
}
