package club.clubk.ktag.apps.sharedservices

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

/**
 * Enum representing the target team setting options.
 */
enum class TargetTeamMode(val value: Int) {
    RED(1),
    BLUE(2),
    ALL(3);

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

/**
 * Data class holding Mine configuration loaded from SharedPreferences.
 */
data class MineConfig(
    val minRssi: Int,
    val damage: Int,
    val targetTeam: TargetTeamMode
) {
    companion object {
        /**
         * Load Mine configuration from the app's default SharedPreferences.
         */
        @JvmStatic
        fun fromPreferences(context: Context): MineConfig {
            val prefs = PreferenceManager.getDefaultSharedPreferences(context)
            return MineConfig(
                minRssi = prefs.getString(
                    MinePreferenceKeys.MIN_RSSI,
                    MinePreferenceKeys.DEFAULT_MIN_RSSI
                )?.toIntOrNull() ?: MinePreferenceKeys.DEFAULT_MIN_RSSI.toInt(),
                damage = prefs.getString(
                    MinePreferenceKeys.DAMAGE,
                    MinePreferenceKeys.DEFAULT_DAMAGE
                )?.toIntOrNull() ?: MinePreferenceKeys.DEFAULT_DAMAGE.toInt(),
                targetTeam = TargetTeamMode.fromInt(
                    prefs.getString(
                        MinePreferenceKeys.TARGET_TEAM,
                        MinePreferenceKeys.DEFAULT_TARGET_TEAM
                    )?.toIntOrNull() ?: MinePreferenceKeys.DEFAULT_TARGET_TEAM.toInt()
                )
            )
        }
    }
}
