package com.convenient.android.common.extension

import android.content.Context
import com.convenient.android.common.utils.string.SharedPreferencesSave

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/8/2
 * description: SP 存储数据相关扩展类
 */


fun <T : Any> Context.spSave(key: String, value : T){
    SharedPreferencesSave.instance.also {
        when(value){
            is String-> it.saveStringValue(context = this, key = key, value = value)
            is Int -> it.saveIntValue(context = this, key = key, value = value)
            is Float -> it.saveFloatValue(context = this, key = key, value = value)
            is Boolean -> it.saveBooleanValue(context = this, key = key, value = value)
            is Long -> it.saveLongValue(context = this, key = key, value = value)
            is Map<*, *> -> it.saveMap(context = this, key = key, map = value)
            is List<*> -> it.saveList(context = this, key = key, list = value)
            else->{
               it.saveObject(context = this, key = key, ob = value)
            }
        }
    }
}


fun Context.removeKey(key: String){
    SharedPreferencesSave.instance.removeKey(context = this, key = key)
}


fun Context.spGetString(key: String, defaultValue: String = "") : String{
    return SharedPreferencesSave.instance.getStringValue(context = this, key = key, defaultValue = defaultValue)
}

fun Context.spGetInt(key: String, defaultValue: Int = 0) : Int{
    return SharedPreferencesSave.instance.getIntValue(context = this, key = key, defaultValue = defaultValue)
}

fun Context.spGetBoolean(key: String, defaultValue: Boolean) : Boolean{
    return SharedPreferencesSave.instance.getBooleanValue(context = this, key = key, defaultValue = defaultValue)
}

fun Context.spGetFloat(key: String, defaultValue: Float = 0F) : Float{
    return SharedPreferencesSave.instance.getFloatValue(context = this, key = key,)
}

fun Context.spGetLong(key: String, defaultValue: Long = 0L) : Long{
    return SharedPreferencesSave.instance.getLongValue(context = this, key = key, defaultValue = defaultValue)
}

fun <T> Context.spGetObj(key: String) : T?{
    return SharedPreferencesSave.instance.getObject(context = this, key = key)
}

inline fun <reified K,reified V> Context.spGetMap(key: String) : Map<K,V>{
    return SharedPreferencesSave.instance.getMap<K,V>(context = this, key = key)
}

inline fun <reified E> Context.spGetList(key: String) : List<E>{
    return SharedPreferencesSave.instance.getList<E>(context = this, key = key)
}


