package com.cartrack.cartrackble

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

interface PreferenceManager {
    fun getString(key: String, defaultValue: String): String
    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean
    fun getInt(key: String, defaultValue: Int): Int

    fun set(key: String, value: String)
    fun set(key: String, value: Int)
    fun set(key: String, value: Boolean)

    fun remove(key: String)
}

class LocalPreferenceManager(private val preferences: SharedPreferences) : PreferenceManager {

    override fun set(key: String, value: String) {
        preferences.edit {
            putString(key, value)
            apply()
        }
    }

    override fun set(key: String, value: Int) {
        preferences.edit {
            putInt(key, value)
            apply()
        }
    }

    override fun set(key: String, value: Boolean) {
        preferences.edit {
            putBoolean(key, value)
            apply()
        }
    }

    override fun getString(key: String, defaultValue: String): String {
        return preferences.getString(key, defaultValue) ?: defaultValue
    }

    override fun getInt(key: String, defaultValue: Int): Int {
        return preferences.getInt(key, defaultValue)
    }

    override fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return preferences.getBoolean(key, defaultValue)
    }

    override fun remove(key: String) {
        preferences.edit().remove(key).apply()
    }
}