package com.swoosh.microblog.ui.theme

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

/**
 * Persists the user's theme preference to SharedPreferences.
 * Uses regular SharedPreferences (not encrypted) since theme preference is not sensitive.
 */
class ThemePreferences(context: Context) {

    private val prefs: SharedPreferences = context.getSharedPreferences(
        PREFS_NAME,
        Context.MODE_PRIVATE
    )

    var themeMode: ThemeMode
        get() {
            val name = prefs.getString(KEY_THEME_MODE, ThemeMode.SYSTEM.name)
            return try {
                ThemeMode.valueOf(name ?: ThemeMode.SYSTEM.name)
            } catch (e: IllegalArgumentException) {
                ThemeMode.SYSTEM
            }
        }
        set(value) {
            prefs.edit().putString(KEY_THEME_MODE, value.name).apply()
        }

    companion object {
        const val PREFS_NAME = "swoosh_theme_prefs"
        const val KEY_THEME_MODE = "theme_mode"
    }
}
