package com.xyzshell.andinfo.libs

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorDirectChannel
import android.hardware.SensorManager
import android.os.Build
import androidx.annotation.RequiresApi

class SensorInfo(private val context: Context) {

    private val sensorManager = context.getSystemService(SensorManager::class.java)

    fun getAllSensors(): List<Sensor> {
        return sensorManager.getSensorList(Sensor.TYPE_ALL)
    }

    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,
            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,
            fifoReservedEventCount = sensor.fifoReservedEventCount,
            minDelay = sensor.minDelay,
            maxDelay = sensor.maxDelay,
            maximumRange = sensor.maximumRange,
            power = sensor.power,
            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
        )
    }

    fun isDynamicSensorDiscoverySupported(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sensorManager.isDynamicSensorDiscoverySupported
        } else {
            false
        }
    }

    data class SensorDetail(
        val name: String,
        val id: Int?,
        val stringType: String,
        val type: Int,
        val vendor: String,
        val version: Int,
        val isDynamicSensor: Boolean,
        val isWakeUpSensor: Boolean,
        val fifoMaxEventCount: Int,
        val fifoReservedEventCount: Int,
        val minDelay: Int,
        val maxDelay: Int,
        val maximumRange: Float,
        val power: Float,
        val reportingMode: Int,
        val resolution: Float,
        val isAdditionalInfoSupported: Boolean,
        val highestDirectReportRateLevel: Int
    )

    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 "特殊触发",
        )

        @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 "非常快速",
        )
    }
}
