package com.gh.gamecenter.core.utils

import android.content.Context
import android.content.SharedPreferences
import com.gh.gamecenter.core.HaloApp
import com.google.gson.reflect.TypeToken

object SPUtils {

    private val mSp by lazy { HaloApp.getInstance().getSharedPreferences("Halo", Context.MODE_PRIVATE) }

    @JvmStatic
    fun setString(key: String, value: String) {
        mSp.edit().putString(key, value).apply()
    }

    @JvmStatic
    fun getString(key: String): String {
        return mSp.getString(key, "") ?: ""
    }

    @JvmStatic
    fun getString(key: String, defaultValue: String): String? {
        return mSp.getString(key, defaultValue)
    }

    @JvmStatic
    fun setInt(key: String, value: Int) {
        mSp.edit().putInt(key, value).apply()
    }

    @JvmStatic
    fun getInt(key: String): Int {
        return mSp.getInt(key, 0)
    }

    @JvmStatic
    fun getInt(key: String, defaultValue: Int): Int {
        return mSp.getInt(key, defaultValue)
    }

    @JvmStatic
    fun getLong(key: String): Long {
        return mSp.getLong(key, 0)
    }

    @JvmStatic
    fun getLong(key: String, defaultValue: Long): Long {
        return mSp.getLong(key, defaultValue)
    }

    @JvmStatic
    fun setLong(key: String, value: Long) {
        return mSp.edit().putLong(key, value).apply()
    }

    @JvmStatic
    fun setBoolean(key: String, value: Boolean) {
        mSp.edit().putBoolean(key, value).apply()
    }

    @JvmStatic
    fun getBoolean(key: String): Boolean {
        return mSp.getBoolean(key, false)
    }

    @JvmStatic
    fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return mSp.getBoolean(key, defaultValue)
    }

    @JvmStatic
    fun getBooleanWithContext(context: Context, key: String, defaultValue: Boolean): Boolean {
        return context.getSharedPreferences("Halo", Context.MODE_PRIVATE).getBoolean(key, defaultValue)
    }

    /**
     * 使用传入的 SP 来存储 KV
     */
    @JvmStatic
    fun setString(sharedPreferences: SharedPreferences, key: String, value: String? = null) {
        try {
            val commitStatus = sharedPreferences.edit().putString(key, value).commit()
            if (!commitStatus) {
                sharedPreferences.edit().putString(key, value).apply()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            sharedPreferences.edit().putString(key, value).apply()
        }
    }

    /**
     * 使用传入的 SP 来存储 KV
     */
    @JvmStatic
    fun setLong(sharedPreferences: SharedPreferences, key: String, value: Long) {
        try {
            val commitStatus = sharedPreferences.edit().putLong(key, value).commit()
            if (!commitStatus) {
                sharedPreferences.edit().putLong(key, value).apply()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            sharedPreferences.edit().putLong(key, value).apply()
        }
    }

    /**
     * 使用传入的 SP 来读取 KV
     */
    @JvmStatic
    fun getString(sharedPreferences: SharedPreferences, key: String, value: String? = null): String {
        return sharedPreferences.getString(key, "") ?: ""
    }

    /**
     * 使用传入的 SP 来读取 KV
     */
    @JvmStatic
    fun getLong(sharedPreferences: SharedPreferences, key: String, value: Long): Long {
        return sharedPreferences.getLong(key, value)
    }

    @JvmStatic
    fun setStringSet(key: String, values: Set<String>) {
        mSp.edit().putStringSet(key, values).apply()
    }

    @JvmStatic
    fun getStringSet(key: String): Set<String> {
        return mSp.getStringSet(key, HashSet()) ?: HashSet()
    }

    @JvmStatic
    fun getStringSet(key: String, defaultValues: Set<String>?): Set<String>? {
        return mSp.getStringSet(key, defaultValues)
    }

    @JvmStatic
    fun setMap(key: String, map: Map<String, String>) {
        val mapJson = GsonUtils.toJson(map)
        setString(key, mapJson)
    }

    @JvmStatic
    fun getMap(key: String): MutableMap<String, String> {
        val mapJson = getString(key)
        val type = object : TypeToken<MutableMap<String, String>>() {}.type
        return GsonUtils.gson.fromJson(mapJson, type) ?: mutableMapOf()
    }

    @JvmStatic
    fun remove(key: String) {
        val editor = mSp.edit()
        editor.remove(key)
        editor.apply()
    }

    /**
     * 使用传入的 SP 来移除 KV
     */
    @JvmStatic
    fun remove(sharedPreferences: SharedPreferences, key: String) {
        val editor = sharedPreferences.edit()
        editor.remove(key)
        editor.apply()
    }
}