package com.pixelized.biblib.repository.credential

import android.content.SharedPreferences
import androidx.core.content.edit


class CredentialRepository constructor(
    private val preferences: SharedPreferences,
) : ICredentialRepository {

    override var login
        get() = preferences.login
        set(value) = value.let { preferences.login = it }

    override var password: String?
        get() = preferences.password
        set(value) = value.let { preferences.password = it }

    override var rememberCredential: Boolean
        get() = preferences.rememberCredential
        set(value) = value.let { preferences.rememberCredential = it }

    override var bearer: String?
        get() = preferences.bearer
        set(value) = value.let { preferences.bearer = it }

    private var SharedPreferences.login: String?
        get() = getString(REMEMBER_USER, null)
        set(value) = edit { putString(REMEMBER_USER, value) }

    private var SharedPreferences.password: String?
        get() = getString(REMEMBER_PASSWORD, null)
        set(value) = edit { putString(REMEMBER_PASSWORD, value) }

    private var SharedPreferences.rememberCredential: Boolean
        get() = getBoolean(REMEMBER_CREDENTIAL, false)
        set(value) = edit { putBoolean(REMEMBER_CREDENTIAL, value) }

    private var SharedPreferences.bearer: String?
        get() = getString(BEARER_TOKEN, null)
        set(value) = edit { putString(BEARER_TOKEN, value) }

    companion object {
        private const val REMEMBER_CREDENTIAL = "REMEMBER_CREDENTIAL"
        private const val REMEMBER_USER = "REMEMBER_USER"
        private const val REMEMBER_PASSWORD = "REMEMBER_PASSWORD"
        private const val BEARER_TOKEN = "BEARER_TOKEN"
    }
}