package com.zxdemo.utils
import android.content.Context
import android.content.SharedPreferences
import com.zxdemo.ZxApplication
import com.zxdemo.http.ZxHttp
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

class SpUtils private constructor(context: Context) {

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

    // 保存字符串
    fun putString(key: String, value: String) {
        with(sharedPreferences.edit()) {
            putString(key, value)
            apply()
        }
    }

    // 获取字符串
    fun getString(key: String, defaultValue: String = ""): String {
        return sharedPreferences.getString(key, defaultValue) ?: defaultValue
    }

    // 保存整数
    fun putInt(key: String, value: Int) {
        with(sharedPreferences.edit()) {
            putInt(key, value)
            apply()
        }
    }

    fun putLong(key: String, value: Long) {
        with(sharedPreferences.edit()) {
            putLong(key, value)
            apply()
        }
    }

    // 获取整数
    fun getInt(key: String, defaultValue: Int = 0): Int {
        return sharedPreferences.getInt(key, defaultValue)
    }

    fun getLong(key: String, defaultValue: Long = 0): Long {
        return sharedPreferences.getLong(key, defaultValue)
    }

    // 保存布尔值
    fun putBoolean(key: String, value: Boolean) {
        with(sharedPreferences.edit()) {
            putBoolean(key, value)
            apply()
        }
    }

    // 获取布尔值
    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return sharedPreferences.getBoolean(key, defaultValue)
    }

    // 保存浮点数
    fun putFloat(key: String, value: Float) {
        with(sharedPreferences.edit()) {
            putFloat(key, value)
            apply()
        }
    }

    // 获取浮点数
    fun getFloat(key: String, defaultValue: Float = 0f): Float {
        return sharedPreferences.getFloat(key, defaultValue)
    }

    // 删除键值对
    fun remove(key: String) {
        with(sharedPreferences.edit()) {
            remove(key)
            apply()
        }
    }

    // 清除所有数据
    fun clear() {
        with(sharedPreferences.edit()) {
            clear()
            apply()
        }
    }

    // 检查键是否存在
    fun contains(key: String): Boolean {
        return sharedPreferences.contains(key)
    }

    fun saveJsonObjectToSp(
        json: String,
    ) {
        if (json.isNullOrEmpty()) {
            return
        }
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        var saveDate = getString("config_date")
        if (saveDate != "") {
            val savedDate = dateFormat.parse(saveDate)
            val currentDate = Calendar.getInstance().time
            if (isSameDay(savedDate, currentDate)) {
                // 如果是同一天，清除保存的次数
                remove("notice_current")
            }
        }
        var jsonObject = JSONObject(json)
        val editor = sharedPreferences.edit()
        // 遍历JSONObject中的所有键值对
        jsonObject.keys().forEachRemaining { key ->
            val value = jsonObject.get(key)
            // 根据值的类型选择不同的存储方法
            when (value) {
                is String -> {
                    editor.putString(key, value)
                }

                is Int -> {
                    editor.putInt(key, value)
                }

                is Long -> editor.putLong(key, value)
                else -> {
                    editor.putString(key, value.toString())
                }
            }
            if (key == "ut") {
                val json = JSONObject()
                json.put("ut", jsonObject.get("ut"))
                ZxHttp.getHttpReportInterface("user_type", "", json)
            }
        }
        editor.apply()

    }

    private fun isSameDay(date1: Date, date2: Date): Boolean {
        val cal1 = Calendar.getInstance()
        cal1.time = date1
        val cal2 = Calendar.getInstance()
        cal2.time = date2

        return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
    }

    companion object {
        @Volatile
        private var instance: SpUtils? = null

        // 单例模式获取实例
        fun getInstance(): SpUtils {
            return instance ?: synchronized(this) {
                instance ?: SpUtils(ZxApplication.context).also {
                    instance = it
                }
            }
        }
    }
}