package partisan.weforge.xyz.pulse

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

private fun makeKeyEnabled(mimetype: String) = "enabled_$mimetype"
private fun makeKeyPriority(mimetype: String) = "priority_$mimetype"

data class ServiceEntry(
    val mimetype: String,
    val labelRes: Int,
    var enabled: Boolean
)

fun Context.isServiceEnabled(mimetype: String): Boolean {
    val prefs = PreferenceManager.getDefaultSharedPreferences(this)
    return prefs.getBoolean(makeKeyEnabled(mimetype), true)
}

fun Context.setServiceEnabled(mimetype: String, enabled: Boolean) {
    val prefs = PreferenceManager.getDefaultSharedPreferences(this)
    prefs.edit().putBoolean(makeKeyEnabled(mimetype), enabled).apply()
}

fun Context.getServicePriority(mimetype: String): Int {
    val prefs = PreferenceManager.getDefaultSharedPreferences(this)
    return prefs.getInt(makeKeyPriority(mimetype), Int.MAX_VALUE)
}

fun Context.setServicePriority(mimetype: String, priority: Int) {
    val prefs = PreferenceManager.getDefaultSharedPreferences(this)
    prefs.edit().putInt(makeKeyPriority(mimetype), priority).apply()
}