package bums.lunatic.launcher.helpers

import android.content.SharedPreferences

enum class PrefString {
    weatherApiKey,
    locationApi,
    telegramBotApi,
    telegramMyId,
    telegramSendTarget,
    carName;
    fun set(value : String) = PrefHelper.putString(this.name, value)
    fun get(def : String? = null) : String = PrefHelper.getString(this.name, def as? String ?: "") ?: ""
}

enum class PrefLong {
    locationTimePeriod,
    locationDistance,
    shortTimePeriod,
    longTimePeriod,
    midTimePeriod,
    maxQueryCount;
    fun set(value : Long) = PrefHelper.putLong(this.name, value)
    fun get(def : Long? = null) : Long = PrefHelper.getLong(this.name, def as? Long ?: 0L) ?: 0L
}

enum class PrefBoolean {
    location,
    rootPermisssion,
    isConnectedCar,
    useQuickLaunch,
    openWithKayboard,
    showAppResultCount,
    weatherDress,
    weatherState,
    showCallHistory,
    showSMSHistory,
    showNotificationHistory,
    showNewsHistory,
    showNowPlaying,
    ;
    fun set(value : Boolean) = PrefHelper.putBoolean(this.name, value)
    fun get(def : Boolean? = null) : Boolean = PrefHelper.getBoolean(this.name, def as? Boolean ?: false) ?: false
}
enum class PrefKey {
    TYPE_STRING,
    locationApi,
    telegramBotApi,
    telegramSendTarget,
    carName,
    TYPE_LONG,
    shortTimePeriod,
    longTimePeriod,
    midTimePeriod,
    maxQueryCount,
    TYPE_BOOL,
    location,
    isConnectedCar,
    useQuickLaunch,
    openWithKayboard,
    showAppResultCount,
    TYPE_END;

    fun set(value : Any): Unit {
        when(this.ordinal) {
            in (TYPE_STRING.ordinal..TYPE_LONG.ordinal) -> {
                (value as? String)?.let { PrefHelper.putString(this.name, it) }
            }
            in (TYPE_LONG.ordinal..TYPE_BOOL.ordinal) -> {
                (value as? Long)?.let { PrefHelper.putLong(this.name, it) }
            }
            in (TYPE_BOOL.ordinal..TYPE_END.ordinal) -> {
                (value as? Boolean)?.let { PrefHelper.putBoolean(this.name, it) }
            }
            else -> {}
        }
    }

    fun get(def : Any? = null) : Any? {
        when(this.ordinal) {
            in (TYPE_STRING.ordinal..TYPE_LONG.ordinal) -> {
                return PrefHelper.getString(this.name, def as? String ?: "")
            }
            in (TYPE_LONG.ordinal..TYPE_BOOL.ordinal) -> {
                return PrefHelper.getLong(this.name, def as? Long ?: 0L)
            }
            in (TYPE_BOOL.ordinal..TYPE_END.ordinal) -> {
                return PrefHelper.getBoolean(this.name, def as? Boolean ?: false)
            }
            else -> {}
        }
        return null
    }
}

object PrefHelper {
    val D_PREFIX = "rasel.lunar.launcher.helpers"
    val BOOL_PRE = D_PREFIX.plus(".BOOL.")
    val STRING_PRE = D_PREFIX.plus(".STRING.")
    val LONG_PRE = D_PREFIX.plus(".LONG.")
    fun inject(SharedPreferences : SharedPreferences) {
        this.sharedPreferences = SharedPreferences
    }
    var sharedPreferences : SharedPreferences? = null
    fun getBoolean(key : String, def :Boolean) = this.sharedPreferences?.getBoolean(BOOL_PRE.plus(key),def) ?: def
    fun putBoolean(key : String, value :Boolean) = this.sharedPreferences?.edit()?.putBoolean(BOOL_PRE.plus(key),value)?.apply()
    fun getLong(key : String, def :Long) = this.sharedPreferences?.getLong(LONG_PRE.plus(key),def) ?: def
    fun putLong(key : String, value :Long) = this.sharedPreferences?.edit()?.putLong(LONG_PRE.plus(key),value)?.apply()
    fun getString(key : String, def :String) = this.sharedPreferences?.getString(STRING_PRE.plus(key),def) ?: def
    fun putString(key : String, value :String) = this.sharedPreferences?.edit()?.putString(STRING_PRE.plus(key),value)?.apply()


    fun location(boolean: Boolean)  = PrefKey.location.set(boolean)
    fun isLocationOn() = (PrefKey.location.get() as? Boolean) ?: false

    var locationApi : String
        get()  = PrefKey.locationApi.get() as? String ?: ""
        set(value) = PrefKey.locationApi.set(value)

    var telegramBotApi : String
        get() = PrefKey.telegramBotApi.get() as? String ?: ""
        set(value) = PrefKey.telegramBotApi.set(value)

    var telegramSendTarget : String
        get() = PrefKey.telegramSendTarget.get() as? String ?: ""
        set(value) = PrefKey.telegramSendTarget.set(value)

    var carName : String
        get() = PrefKey.carName.get() as? String ?: ""
        set(value) = PrefKey.carName.set(value)


    var shortTimePeriod : Long
        get() = PrefKey.shortTimePeriod.get(20L) as? Long ?: 20L
        set(value) = PrefKey.shortTimePeriod.set(value)

    var longTimePeriod : Long
        get() = PrefKey.longTimePeriod.get(60L) as? Long ?: 60L
        set(value) = PrefKey.longTimePeriod.set(value)

    var midTimePeriod : Long
        get() = PrefKey.midTimePeriod.get(30L) as? Long ?: 30L
        set(value) = PrefKey.midTimePeriod.set(value)


    var isConnectedCar : Boolean
        get() = PrefKey.isConnectedCar.get() as? Boolean ?: false
        set(value) = PrefKey.isConnectedCar.set(value)

    var useQuickLaunch : Boolean
        get() = PrefKey.useQuickLaunch.get() as? Boolean ?: false
        set(value) = PrefKey.useQuickLaunch.set(value)

    var openWithKayboard : Boolean
        get() = PrefKey.openWithKayboard.get() as? Boolean ?: false
        set(value) = PrefKey.openWithKayboard.set(value)

    var showAppResultCount : Boolean
        get() = PrefKey.showAppResultCount.get() as? Boolean ?: false
        set(value) = PrefKey.showAppResultCount.set(value)



}
typealias BLOCK = ()->Unit
inline fun Boolean.letTrue(block: BLOCK) {
    if (this) {
        block.invoke()
    } else {
//        elseblock.invoke()
    }
}