package cl.homelogic.platform.common.settings

import com.russhwolf.settings.Settings

sealed class PreferenceKey<T>(
    val key: String,
) {
    abstract fun getValue(
        settings: Settings,
        defaultValue: T,
    ): T

    abstract fun setValue(
        settings: Settings,
        value: T,
    )

    class StringKey(
        key: String,
    ) : PreferenceKey<String>(key) {
        override fun getValue(
            settings: Settings,
            defaultValue: String,
        ): String = settings.getString(key, defaultValue)

        override fun setValue(
            settings: Settings,
            value: String,
        ) = settings.putString(key, value)
    }

    class IntKey(
        key: String,
    ) : PreferenceKey<Int>(key) {
        override fun getValue(
            settings: Settings,
            defaultValue: Int,
        ): Int = settings.getInt(key, defaultValue)

        override fun setValue(
            settings: Settings,
            value: Int,
        ) = settings.putInt(key, value)
    }

    class LongKey(
        key: String,
    ) : PreferenceKey<Long>(key) {
        override fun getValue(
            settings: Settings,
            defaultValue: Long,
        ): Long = settings.getLong(key, defaultValue)

        override fun setValue(
            settings: Settings,
            value: Long,
        ) = settings.putLong(key, value)
    }

    class FloatKey(
        key: String,
    ) : PreferenceKey<Float>(key) {
        override fun getValue(
            settings: Settings,
            defaultValue: Float,
        ): Float = settings.getFloat(key, defaultValue)

        override fun setValue(
            settings: Settings,
            value: Float,
        ) = settings.putFloat(key, value)
    }

    class DoubleKey(
        key: String,
    ) : PreferenceKey<Double>(key) {
        override fun getValue(
            settings: Settings,
            defaultValue: Double,
        ): Double = settings.getDouble(key, defaultValue)

        override fun setValue(
            settings: Settings,
            value: Double,
        ) = settings.putDouble(key, value)
    }

    class BooleanKey(
        key: String,
    ) : PreferenceKey<Boolean>(key) {
        override fun getValue(
            settings: Settings,
            defaultValue: Boolean,
        ): Boolean = settings.getBoolean(key, defaultValue)

        override fun setValue(
            settings: Settings,
            value: Boolean,
        ) = settings.putBoolean(key, value)
    }
}
