package ru.myitschool.work.utils

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

class AuthPreferences(context: Context) {
    private val sharedPreferences: SharedPreferences = context.getSharedPreferences(
        PREFS_NAME,
        Context.MODE_PRIVATE
    )

    fun saveLoginState(isLoggedIn: Boolean) {
        sharedPreferences.edit().apply {
            putBoolean(KEY_IS_LOGGED_IN, isLoggedIn)
            apply()
        }
    }


    fun isLoggedIn(): Boolean {
        return sharedPreferences.getBoolean(KEY_IS_LOGGED_IN, false)
    }

    fun clearLoginState() {
        sharedPreferences.edit().apply {
            remove(KEY_IS_LOGGED_IN)
            apply()
        }
    }

    fun saveLogin(login: String) {
        sharedPreferences.edit().apply {
            putString("user_login", login)
            apply()
        }
    }

    fun getLogin(): String? {
        return sharedPreferences.getString("user_login", null)
    }

    fun savePassword(login: String) {
        sharedPreferences.edit().apply {
            putString("user_password", login)
            apply()
        }
    }

    fun getPassword(): String? {
        return sharedPreferences.getString("user_password", null)
    }

    companion object {
        private const val PREFS_NAME = "AuthPreferences"
        private const val KEY_IS_LOGGED_IN = "is_logged_in"
    }
}