package com.tt.honeyDue.storage

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

/**
 * Android implementation of theme storage using SharedPreferences.
 */
actual class ThemeStorageManager(context: Context) {
    private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    actual fun saveThemeId(themeId: String) {
        prefs.edit().putString(KEY_THEME_ID, themeId).apply()
    }

    actual fun getThemeId(): String? {
        return prefs.getString(KEY_THEME_ID, null)
    }

    actual fun clearThemeId() {
        prefs.edit().remove(KEY_THEME_ID).apply()
    }

    actual fun saveUseDynamicColor(enabled: Boolean) {
        prefs.edit().putBoolean(KEY_USE_DYNAMIC_COLOR, enabled).apply()
    }

    actual fun getUseDynamicColor(): Boolean {
        return prefs.getBoolean(KEY_USE_DYNAMIC_COLOR, false)
    }

    companion object {
        private const val PREFS_NAME = "honeydue_theme_prefs"
        private const val KEY_THEME_ID = "theme_id"
        private const val KEY_USE_DYNAMIC_COLOR = "use_dynamic_color"

        @Volatile
        private var instance: ThemeStorageManager? = null

        fun getInstance(context: Context): ThemeStorageManager {
            return instance ?: synchronized(this) {
                instance ?: ThemeStorageManager(context.applicationContext).also { instance = it }
            }
        }
    }
}
