package com.colorful.keyboard.theme.myutil

import android.content.Context
import android.content.SharedPreferences
import com.colorful.keyboard.theme.firebase.Constants

class AppSharedPreferences(context: Context) {
    companion object {
        private const val KEY_CURRENTLY_THEME_URL = "key_colorful_currently_theme_url"
        private const val KEY_CURRENTLY_THEME_GIF_URL = "key_colorful_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 getShowAdIntervalTime(): Long {
        return getLong(Constants.KEY_SHOW_AD_INTERVAL_TIME, Constants.DEFAULT_SHOW_AD_INTERVAL_TIME)
    }

    fun setShowAdIntervalTime(long: Long) {
        saveLong(Constants.KEY_SHOW_AD_INTERVAL_TIME, long)
    }

    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 saveLong(key: String, value: Long) {
        val editor = sharedPreferences.edit()
        editor.putLong(key, value)
        editor.apply()
    }

    fun getLong(key: String, defaultValue: Long = Constants.DEFAULT_SHOW_AD_INTERVAL_TIME): Long {
        return sharedPreferences.getLong(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()
    }
}
