package com.tt.honeyDue.data

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

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

    actual fun save(key: String, value: String) {
        prefs.edit().putString(key, value).apply()
    }

    actual fun load(key: String): String? {
        return prefs.getString(key, null)
    }

    actual fun remove(key: String) {
        prefs.edit().remove(key).apply()
    }

    actual fun clear() {
        prefs.edit().clear().apply()
    }

    companion object {
        private const val PREFS_NAME = "honeydue_data_manager"

        @Volatile
        private var instance: PersistenceManager? = null

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