package com.honeydue.storage

import java.util.prefs.Preferences

/**
 * JVM implementation of TokenManager using Java Preferences API.
 */
actual class TokenManager {
    private val prefs: Preferences = Preferences.userRoot().node(PREFS_NODE)

    actual fun saveToken(token: String) {
        prefs.put(KEY_TOKEN, token)
        prefs.flush()
    }

    actual fun getToken(): String? {
        return prefs.get(KEY_TOKEN, null)
    }

    actual fun clearToken() {
        prefs.remove(KEY_TOKEN)
        prefs.flush()
    }

    companion object {
        private const val PREFS_NODE = "com.honeyDue.treytartt.com"
        private const val KEY_TOKEN = "auth_token"

        @Volatile
        private var instance: TokenManager? = null

        fun getInstance(): TokenManager {
            return instance ?: synchronized(this) {
                instance ?: TokenManager().also { instance = it }
            }
        }
    }
}
