package com.swoosh.microblog.data

import android.content.Context
import android.content.SharedPreferences

class TagsPreferences private constructor(
    private val prefs: SharedPreferences,
    private val accountIdProvider: () -> String
) {

    constructor(context: Context) : this(
        prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE),
        accountIdProvider = { AccountManager(context).getActiveAccount()?.id ?: "" }
    )

    constructor(prefs: SharedPreferences, accountId: String) : this(
        prefs = prefs,
        accountIdProvider = { accountId }
    )

    private fun activeAccountId(): String = accountIdProvider()

    fun isTagsEnabled(): Boolean =
        prefs.getBoolean("tags_enabled_${activeAccountId()}", true)

    fun setTagsEnabled(enabled: Boolean) =
        prefs.edit().putBoolean("tags_enabled_${activeAccountId()}", enabled).apply()

    companion object {
        const val PREFS_NAME = "tags_prefs"
    }
}
