package org.wntr.mdeditor
import android.content.Context

class CredentialManager(applicationContext: Context) {
    val sharedPrefs= applicationContext.getSharedPreferences("prefs", Context.MODE_PRIVATE)
    var instance = "nowhere"
        get()  =
            if (field.equals("nowhere")) loadCredentialsFromSharedPrefs().instance
            else field

    var username = "nobody"
        get() =
            if (field.equals("nobody")) loadCredentialsFromSharedPrefs().username
            else field

    fun saveCredentialsToSharedPrefs(creds: Credentials) {
        val (instance, username) = creds

        sharedPrefs.edit().apply {
                putString("instance", instance)
                putString("username", username)
                apply()
            }
    }

    fun loadCredentialsFromSharedPrefs() : Credentials {
        val instance = sharedPrefs.getString("instance", "nowhere")!!
        val username = sharedPrefs.getString("username", "nobody")!!
        return (Credentials(instance!!, username!!, "nothing"))
    }

}


