package com.gh.gamecenter.common.constant

import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.core.utils.SPUtils.setString


object Config {
    const val FIX_DOWNLOAD_KEY = "isFixDownload"
    const val FIX_PLUGIN_KEY = "isFixPlugin"
    const val FIX_ARTICLE_KEY = "isFixArticle"
    const val FIX_COMMUNITY_KEY = "isFixCommunity"

    const val DEFAULT_CHANNEL = "GH_TEST3"
    const val DEFAULT_CHANNEL_FOR_RELEASE = "GH_LOST" // 正式包的缺省渠道，避免因渠道丢失而回落到测试渠道
    private var mDefaultSharedPreferences: SharedPreferences? = null

    @JvmStatic
    fun getExceptionMsg(): String? {
        return getPreferences().getString("errMsg", null)
    }

    @JvmStatic
    fun setExceptionMsg(errMsg: String?) {
        setString(getPreferences(), "errMsg", errMsg) //先用apply(),保存不了再用commit() 9.0机型保存不了信息
    }

    @JvmStatic
    fun updateContainsErrorMsg(containsCrashMsg: Boolean) {
        setString(getPreferences(), "containsErrMsg", if (containsCrashMsg) "true" else "false")
    }

    @JvmStatic
    fun isContainsErrorMsg(): Boolean {
        return "true" == getPreferences().getString("containsErrMsg", null)
    }

    fun getPreferences(): SharedPreferences {
        if (mDefaultSharedPreferences == null) {
            mDefaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance())
        }
        return mDefaultSharedPreferences!!
    }

    fun isExistHideFunction(): Boolean {
        if (!getPreferences().getBoolean(FIX_DOWNLOAD_KEY, false)) return true
        if (!getPreferences().getBoolean(FIX_PLUGIN_KEY, false)) return true
        if (!getPreferences().getBoolean(FIX_COMMUNITY_KEY, false)) return true
        if (!getPreferences().getBoolean(FIX_ARTICLE_KEY, false)) return true
        return false
    }

    fun fixHideFunction() {
        getPreferences().edit().run {
            putBoolean(FIX_DOWNLOAD_KEY, true)
            putBoolean(FIX_ARTICLE_KEY, true)
            putBoolean(FIX_COMMUNITY_KEY, true)
            putBoolean(FIX_PLUGIN_KEY, true)
            apply()
        }
    }
}