package com.dahe.gldriver.utils

import android.content.Context
import com.dahe.glex.bean.UserBean
import com.dahe.mylibrary.net.JsonUtils
import com.dahe.mylibrary.utils.BaseSPUtils

/**
 * @ClassName SPUtils2
 * @Author 用户
 * @Date 2023/12/29 08:52
 * @Description TODO
 */
class SPUtils private constructor() : BaseSPUtils() {
    companion object{
        val instance = Holder.holder
    }

    object Holder {
        val holder = SPUtils()
    }

    /**
     * 存用户信息
     *
     * @param context
     * @param json
     */
    fun setUserInfo(context: Context?, json: String?) {
        BaseSPUtils.put(context, BaseSPUtils.USER_INFO_KEY, json)
    }

    /**
     * 拿用户信息
     *
     * @param context
     */
    fun getUserInfo(context: Context?): UserBean? {
        return JsonUtils.getInstance()
            .fromJson(BaseSPUtils.get(context, BaseSPUtils.USER_INFO_KEY, "") as String,
                UserBean::class.java)
    }

    /**
     * 存服务器类型
     *
     * @param context
     * @param isTestService
     */
    fun setNetServiceType(context: Context?, isTestService: Boolean) {
        BaseSPUtils.put(context, BaseSPUtils.NET_SERVICE_TEST, isTestService)
    }

    /**
     * 拿服务器类型
     *
     * @param context
     */
    fun geNetServiceType(context: Context?): Boolean {
        return BaseSPUtils.get(context, BaseSPUtils.NET_SERVICE_TEST, false) as Boolean
    }

    /**
     * 存搜索数据
     *
     * @param context
     * @param json
     */
    fun setSearchCache(context: Context?, json: String?) {
        BaseSPUtils.put(context, BaseSPUtils.SEARRH_CACHE, json)
    }

    fun getSearchCache(context: Context?): String? {
        return JsonUtils.getInstance()
            .fromJson(BaseSPUtils.get(context, BaseSPUtils.SEARRH_CACHE, "") as String,
                String::class.java)
    }

    fun removeSearchCache(context: Context?) {
        BaseSPUtils.remove(context, BaseSPUtils.SEARRH_CACHE)
    }

    /**
     * 导航偏好设置
     */
    fun setNaviPreferenceCache(context: Context?, json: String?) {
        BaseSPUtils.put(context, BaseSPUtils.NAVI_PH_EDIT_CACHE, json)
    }

    fun getNaviPreferenceCache(context: Context?): String? {
        return JsonUtils.getInstance()
            .fromJson(BaseSPUtils.get(context, BaseSPUtils.NAVI_PH_EDIT_CACHE, "") as String,
                String::class.java)
    }

    fun removeNaviPreference(context: Context?) {
        BaseSPUtils.remove(context, BaseSPUtils.NAVI_PH_EDIT_CACHE)
    }

    //第一次登录
    fun getIsFirstOpen(context: Context?): Boolean {
        return BaseSPUtils.get(context, BaseSPUtils.FIRST_OPEN, true) as Boolean
    }

    fun setIsFirstOpen(context: Context?, json: Boolean) {
        BaseSPUtils.put(context, BaseSPUtils.FIRST_OPEN, json)
    }


    /**
     * 设置隐私协议是否同意
     *
     * @param value 是否同意
     */
    fun setAgreePrivacyAgreement(context: Context?, value: Boolean) {
        BaseSPUtils.put(context, BaseSPUtils.KEY_PRIVACY_AGREEMENT, value)
    }

    /**
     * 是否同意了隐私协议
     *
     * @return true 已经同意；false 还没有同意
     */
    fun hasAgreePrivacyAgreement(context: Context?): Boolean {
        return BaseSPUtils.get(context, BaseSPUtils.KEY_PRIVACY_AGREEMENT, false) as Boolean
    }
}