package com.obdwidgets.model

/**
 * Enum für verfügbare OBD2 Parameter
 * Erweiterbar für verschiedene Automarken und spezielle PIDs
 */
enum class OBDParameter(
    val pid: String,
    val displayName: String,
    val unit: String,
    val minValue: Float = 0f,
    val maxValue: Float = 100f,
    val formula: (ByteArray) -> Float
) {
    SPEED(
        pid = "010D",
        displayName = "Speed",
        unit = "km/h",
        minValue = 0f,
        maxValue = 200f,
        formula = { bytes -> bytes[0].toFloat() }
    ),
    RPM(
        pid = "010C",
        displayName = "RPM",
        unit = "RPM",
        minValue = 0f,
        maxValue = 8000f,
        formula = { bytes -> ((bytes[0] * 256) + bytes[1]) / 4f }
    ),
    VOLTAGE(
        pid = "0142",
        displayName = "Voltage",
        unit = "V",
        minValue = 10f,
        maxValue = 16f,
        formula = { bytes -> ((bytes[0] * 256) + bytes[1]) / 1000f }
    ),
    COOLANT_TEMP(
        pid = "0105",
        displayName = "Coolant Temp",
        unit = "°C",
        minValue = -40f,
        maxValue = 215f,
        formula = { bytes -> bytes[0].toFloat() - 40f }
    ),
    INTAKE_TEMP(
        pid = "010F",
        displayName = "Intake Temp",
        unit = "°C",
        minValue = -40f,
        maxValue = 215f,
        formula = { bytes -> bytes[0].toFloat() - 40f }
    ),
    MAF(
        pid = "0110",
        displayName = "MAF",
        unit = "g/s",
        minValue = 0f,
        maxValue = 655f,
        formula = { bytes -> ((bytes[0] * 256) + bytes[1]) / 100f }
    ),
    THROTTLE(
        pid = "0111",
        displayName = "Throttle",
        unit = "%",
        minValue = 0f,
        maxValue = 100f,
        formula = { bytes -> (bytes[0] * 100f) / 255f }
    ),
    FUEL_LEVEL(
        pid = "012F",
        displayName = "Fuel Level",
        unit = "%",
        minValue = 0f,
        maxValue = 100f,
        formula = { bytes -> (bytes[0] * 100f) / 255f }
    );
    
    companion object {
        fun fromPid(pid: String): OBDParameter? {
            return values().find { it.pid == pid }
        }
        
        fun fromName(name: String): OBDParameter? {
            return values().find { it.displayName.equals(name, ignoreCase = true) || it.name.equals(name, ignoreCase = true) }
        }
    }
}
