package bob.clean.data.store.cache

import bob.clean.data.model.UserInfo
import bob.clean.data.store.Cache
import bob.clean.data.store.UserStore

interface UserCache : UserStore {
    fun isCachedForName(name: String): Boolean
}

class UserCacheImpl(private val cache: Cache) : UserCache {
    private val prefix_name = "name"

    override fun login(name: String, password: String): UserInfo? {
        val info = cache.get<UserInfo>(createKey(prefix_name, name))
        return if (password.equals(info?.password)) info else null
    }

    override fun logout() {
        // nothing need to do here
    }

    override fun isCachedForName(name: String) = cache.get<UserInfo>(createKey(prefix_name, name)) != null

    private fun createKey(prefix: String, value: String) = "user_cache_" + prefix + value
}
