package club.clubk.ktag.apps.sharedservices

import android.content.Context
import androidx.preference.PreferenceManager

/**
 * Enum representing the auto-heal setting options.
 */
enum class AutoHealMode(val value: Int) {
    NONE(0),
    RED(1),
    BLUE(2),
    ALL(3);

    companion object {
        fun fromInt(value: Int): AutoHealMode = entries.find { it.value == value } ?: NONE
    }
}

/**
 * Data class holding Medic configuration loaded from SharedPreferences.
 */
data class MedicConfig(
    val minRssi: Int,
    val autoHeal: AutoHealMode
) {
    companion object {
        /**
         * Load Medic configuration from the app's default SharedPreferences.
         */
        @JvmStatic
        fun fromPreferences(context: Context): MedicConfig {
            val prefs = PreferenceManager.getDefaultSharedPreferences(context)
            return MedicConfig(
                minRssi = prefs.getString(
                    MedicPreferenceKeys.MIN_RSSI,
                    MedicPreferenceKeys.DEFAULT_MIN_RSSI
                )?.toIntOrNull() ?: MedicPreferenceKeys.DEFAULT_MIN_RSSI.toInt(),
                autoHeal = AutoHealMode.fromInt(
                    prefs.getString(
                        MedicPreferenceKeys.AUTO_HEAL,
                        MedicPreferenceKeys.DEFAULT_AUTO_HEAL
                    )?.toIntOrNull() ?: MedicPreferenceKeys.DEFAULT_AUTO_HEAL.toInt()
                )
            )
        }
    }
}
