package com.cheng.bole.manager

import android.os.Build
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.cheng.bole.bean.BidTypeBean
import com.cheng.bole.bean.AreaBean
import com.cheng.bole.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.MMKVUtils
import com.example.base.utils.Utils
import com.github.gzuliyujiang.oaid.DeviceID
import com.github.gzuliyujiang.oaid.DeviceIdentifier
import com.github.gzuliyujiang.oaid.IGetter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

object UserConfigManager {
    val _userConfigLiveData = MutableLiveData<com.cheng.bole.bean.UserConfigEntity?>()

    val userConfig: com.cheng.bole.bean.UserConfigEntity? get() = _userConfigLiveData.value

    val userInfoLiveData = MutableLiveData<com.cheng.bole.bean.UserEntity?>()

    val userInfo: com.cheng.bole.bean.UserEntity? get() = userInfoLiveData.value

    fun getUserConfig(clickFun: () -> Unit?) {
        if (!TextUtils.isEmpty(MMKVUtils.getString("userConfig"))) {
            val data = Gson().fromJson(MMKVUtils.getString("userConfig"), com.cheng.bole.bean.UserConfigEntity::class.java)
            _userConfigLiveData.postValue(data)

            getOaId()
            saveUserConfig(data)
            clickFun.invoke()
            return
        }
        getOaId {
            userConfig { clickFun.invoke() }
        }
    }

    fun userConfig(clickFun: () -> Unit?) {
        GlobalScope.launch {
            try {
                val oaid = MMKVUtils.getString("oaid") ?: ""
                val response = ApiFactory.apiService.getUserConfig(oaid, Build.VERSION.SDK_INT, "", DeviceIdentifier.getAndroidID(Utils.getApp()), getGTCid())
                if (response.status) {
                    _userConfigLiveData.postValue(response.data)
                    MMKVUtils.put("userConfig", Gson().toJson(response.data))

                    saveUserConfig(response.data)

                    withContext(Dispatchers.Main) {
                        clickFun.invoke()
                    }
                } else {
                    withContext(Dispatchers.Main) {
                        toast(response.message, true)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun saveUserConfig(data: com.cheng.bole.bean.UserConfigEntity) {
        try {
            LoginManager.saveToken(data.token)
            saveIsTemp(data.temp)
            if (data.config != null){
                saveGuidePayEnable(data.config!!.guidePayEnable!!)
                saveGuideEnable(data.config!!.guideEnable!!)
                saveGuideHint(data.config!!.guideHint)
                saveNoLoginPay(data.config!!.noLoginPayEnable!!)
                savePayAgreementEnable(data.config!!.payAgreementEnable!!)
                saveLoginType(data.config!!.loginType!!)
                saveShareEntity(data.config!!.wxShareEntity)
                saveAdSwitch(data.config!!.adSwitch)
                saveServicePhoneList(data.config!!.servicePhoneList)
            }
        } catch (e : Exception) {
            e.printStackTrace()
        }
    }

    fun saveLastUserinfo(userinfo: com.cheng.bole.bean.UserEntity?) {
        MMKVUtils.put("last_userinfo", Gson().toJson(userinfo))
    }

    fun getLastUserinfo(): com.cheng.bole.bean.UserEntity? {
        val str = MMKVUtils.getString("last_userinfo")
        if (!TextUtils.isEmpty(str)) {
            return Gson().fromJson(str, com.cheng.bole.bean.UserEntity::class.java)
        }
        return null
    }

    /**
     * 是否同意隐私政策
     */
    fun saveAgree() {
        MMKVUtils.put("isAgree", true)
    }

    fun isAgree(): Boolean {
        return MMKVUtils.getBoolean("isAgree", false)
    }

    private fun saveGuidePayEnable(temp: Boolean) {
        MMKVUtils.put("guide_pay", temp)//引导页是否可以支付
    }

    fun getGuidePayEnable(): Boolean {
        return MMKVUtils.getBoolean("guide_pay", true)
    }

    private fun saveGuideEnable(temp: Boolean) {
        MMKVUtils.put("guide", temp)//是否开启引导页
    }

    fun getGuideEnable(): Boolean {
        return MMKVUtils.getBoolean("guide", true)
    }

    private fun saveGuideHint(hint: String?) {
        MMKVUtils.put("guide_hint", hint)
    }

    fun getGuideHint(): String {
        return MMKVUtils.getString("guide_hint") ?: ""
    }

    fun saveIsTemp(temp: Boolean) {
        MMKVUtils.put("x-role", temp)//true临时用户 ,false登录用户
    }

    fun getIsTemp(): Boolean {
        return MMKVUtils.getBoolean("x-role", true)
    }

    fun isFirstUseApp(): Boolean {
        return MMKVUtils.getBoolean("isFirstUse", true)
    }

    fun saveFirstUseApp(isFirst: Boolean) {
        MMKVUtils.put("isFirstUse", isFirst)
    }

    private fun saveNoLoginPay(noLoginPay: Boolean) {
        MMKVUtils.put("nologin_pay", noLoginPay)
    }

    fun getNoLoginPay(): Boolean {
        return MMKVUtils.getBoolean("nologin_pay", true)
    }

    private fun savePayAgreementEnable(payAgreement: Boolean) {
        MMKVUtils.put("pay_agreement", payAgreement)
    }

    fun isPayAgreementEnable(): Boolean {
        return MMKVUtils.getBoolean("pay_agreement", true)
    }

    private fun saveLoginType(list: List<String>) {
        MMKVUtils.put("login_type", Gson().toJson(list))
    }

    fun getLoginType(): List<String> {
        val s = MMKVUtils.getString("login_type")
        if (!TextUtils.isEmpty(s)) {
            return Gson().fromJson(s, object : TypeToken<List<String>>() {}.type)
        }
        return emptyList()
    }

    private fun saveShareEntity(entity: com.cheng.bole.bean.WxShareEntity?) {
        MMKVUtils.put("weixin_share", Gson().toJson(entity))
    }

    fun getShareEntity(): com.cheng.bole.bean.WxShareEntity? {
        val s = MMKVUtils.getString("weixin_share")
        if (!TextUtils.isEmpty(s)) {
            return Gson().fromJson(s, com.cheng.bole.bean.WxShareEntity::class.java)
        }
        return null
    }

    private fun saveAdSwitch(switch: Boolean) {
        MMKVUtils.put("ad_switch", switch)
    }

    fun isAdSwitch(): Boolean {
        return MMKVUtils.getBoolean("ad_switch")
    }

    private fun saveServicePhoneList(list: List<String>) {
        MMKVUtils.put("service_phone_list", Gson().toJson(list))
    }

    fun getServicePhoneList(): List<String> {
        val str = MMKVUtils.getString("service_phone_list")
        if (!TextUtils.isEmpty(str)) {
            return Gson().fromJson(str, object : TypeToken<List<String>>() {}.type)
        }
        return emptyList()
    }

    /**
     * 搜索排序
     */
    private fun saveSearchSort(sort: String) {
        MMKVUtils.put("search_sort", sort)
    }

    fun getSearchSort(): String {
        return MMKVUtils.getString("search_sort") ?: ""
    }

    /**
     * 地区列表
     */
    fun saveAreaList(list: List<AreaBean>) {
        if (list.isNotEmpty()) {
            MMKVUtils.put("area_list", Gson().toJson(list))
        }
    }

    fun getAreaList(): MutableList<AreaBean> {
        val str = MMKVUtils.getString("area_list")
        if (!TextUtils.isEmpty(str)) {
            return Gson().fromJson(str, object : TypeToken<MutableList<AreaBean>>() {}.type)
        }
        return mutableListOf()
    }

    /**
     * 城市列表
     */
    fun getCityList(): List<AreaBean> {
        val list = mutableListOf<AreaBean>()
        getAreaList().forEach {
            list.addAll(it.children)
        }
        return list
    }

    /**
     * 招标类型
     */
    fun saveBidTypes(list: List<BidTypeBean>) {
        MMKVUtils.put("bid_type", Gson().toJson(list))
    }

    fun getBidTypes(): List<BidTypeBean> {
        val str = MMKVUtils.getString("bid_type")
        if (!TextUtils.isEmpty(str)) {
            return Gson().fromJson(str, object : TypeToken<List<BidTypeBean>>() {}.type)
        }
        return emptyList()
    }

    /**
     * 保存个推cid
     */
    fun saveGTCid(cid: String) {
        MMKVUtils.put("gt_cid", cid)
    }

    /**
     * 获取个推cid
     */
    fun getGTCid(): String {
        return MMKVUtils.getString("gt_cid") ?: ""
    }

    private fun getOaId(callback: ((String) -> Unit)? = null) {
        val oaid = DeviceIdentifier.getOAID(Utils.getApp())
        if (TextUtils.isEmpty(oaid)) {
            DeviceID.getOAID(Utils.getApp(), object : IGetter {
                override fun onOAIDGetComplete(result: String) {
                    MMKVUtils.put("oaid", result)
                    callback?.invoke(result)
                }

                override fun onOAIDGetError(error: Exception) {
                    callback?.invoke("")
                }
            })
        } else {
            MMKVUtils.put("oaid", oaid)
            callback?.invoke(oaid)
        }
    }

    /**
     * 保存百度归因bd_vid
     */
    fun saveBDVID() {
        try {
            val str = com.cheng.bole.utils.appwalle.ChannelReader.get(Utils.getApp())
            if (!TextUtils.isEmpty(str)) {
                val jsonObj = JSONObject(str)
                jsonObj.getString("bd_vid").let {
                    MMKVUtils.put("bd_vid", it)
                }
            } else {
                MMKVUtils.put("bd_vid", "")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 获取百度归因bd_vid
     */
    fun getBDVID(): String {
        return MMKVUtils.getString("bd_vid") ?: ""
    }
}