package com.keyboard.craft.util

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

class AppSharedPreferences(context: Context) {
    companion object {
        private const val KEY_CURRENTLY_THEME_URL = "key_craft_currently_theme_url"
        private const val KEY_CURRENTLY_THEME_GIF_URL = "key_craft_currently_theme_gif_url"
    }

    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)

    fun getCurrentlyThemeUrl(): String {
        return getString(KEY_CURRENTLY_THEME_URL, "")
    }

    fun setCurrentlyThemeUrl(string: String) {
        saveString(KEY_CURRENTLY_THEME_URL, string)
    }

    fun getCurrentlyThemeGifUrl(): String {
        return getString(KEY_CURRENTLY_THEME_GIF_URL, "")
    }

    fun setCurrentlyThemeGifUrl(string: String) {
        saveString(KEY_CURRENTLY_THEME_GIF_URL, string)
    }

    fun saveString(key: String, value: String) {
        val editor = sharedPreferences.edit()
        editor.putString(key, value)
        editor.apply()
    }

    fun getString(key: String, defaultValue: String = ""): String {
        return sharedPreferences.getString(key, defaultValue) ?: defaultValue
    }

    fun saveInt(key: String, value: Int) {
        val editor = sharedPreferences.edit()
        editor.putInt(key, value)
        editor.apply()
    }

    fun getInt(key: String, defaultValue: Int = 0): Int {
        return sharedPreferences.getInt(key, defaultValue)
    }


    // 删除特定键对应的值
    fun removeKey(key: String) {
        val editor = sharedPreferences.edit()
        editor.remove(key)
        editor.apply()
    }

    // 清空 SharedPreferences 中的所有数据
    fun clearAll() {
        val editor = sharedPreferences.edit()
        editor.clear()
        editor.apply()
    }
}
