package com.erwan.revwatch.model

data class GaugeLimits(
    val warningLow: Double? = null,
    val dangerLow: Double? = null,
    val warningHigh: Double? = null,
    val dangerHigh: Double? = null
) {
    enum class Zone { NORMAL, WARNING, DANGER }

    fun zoneFor(value: Double): Zone {
        if (dangerLow != null && value <= dangerLow) return Zone.DANGER
        if (dangerHigh != null && value >= dangerHigh) return Zone.DANGER
        if (warningLow != null && value <= warningLow) return Zone.WARNING
        if (warningHigh != null && value >= warningHigh) return Zone.WARNING
        return Zone.NORMAL
    }

    /** Convert thresholds from raw sensor units to display units */
    fun convert(unitOption: UnitOption): GaugeLimits = GaugeLimits(
        warningLow = warningLow?.let { unitOption.convert(it) },
        dangerLow = dangerLow?.let { unitOption.convert(it) },
        warningHigh = warningHigh?.let { unitOption.convert(it) },
        dangerHigh = dangerHigh?.let { unitOption.convert(it) }
    )

    companion object {
        fun forWidget(widget: WidgetType): GaugeLimits = when (widget) {
            WidgetType.OIL_TEMP -> GaugeLimits(
                warningLow = 60.0,
                warningHigh = 120.0,
                dangerHigh = 140.0
            )
            WidgetType.BOOST -> GaugeLimits(
                warningHigh = 130.0,
                dangerHigh = 150.0
            )
            WidgetType.RPM -> GaugeLimits(
                warningHigh = 5500.0,
                dangerHigh = 6500.0
            )
            WidgetType.SPEED -> GaugeLimits()
            WidgetType.COOLANT -> GaugeLimits(
                warningLow = 60.0,
                warningHigh = 105.0,
                dangerHigh = 115.0
            )
            WidgetType.VOLTAGE -> GaugeLimits(
                dangerLow = 11.0,
                warningLow = 11.8,
                warningHigh = 14.8,
                dangerHigh = 15.2
            )
            WidgetType.FUEL_LEVEL -> GaugeLimits(
                warningLow = 15.0,
                dangerLow = 7.0
            )
            WidgetType.FUEL_RATE -> GaugeLimits(
                warningHigh = 12.0,
                dangerHigh = 16.0
            )
            WidgetType.FUEL_L100 -> GaugeLimits(
                warningHigh = 12.0,
                dangerHigh = 20.0
            )
        }
    }
}
