package com.halo.assistant.fragment.user

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody

class BbsCertificationViewModel(application: Application) : AndroidViewModel(application) {

    private val api = RetrofitManager.getInstance().api

    var unBindLiveData = MutableLiveData<Boolean>()

    init {
        getMobileAuth()
    }

    fun getCertType(): String {
        return when {
            isRealName() -> TYPE_REAL_NAME_CERT
            isBindPhone() -> TYPE_BIND_PHONE
            else -> TYPE_BBS_CERT
        }
    }

    // 是否实名
    private fun isRealName() = UserManager.getInstance().userInfoEntity?.idCard != null

    // 是否绑定手机
    private fun isBindPhone() = UserManager.getInstance().userInfoEntity?.loginMobile?.isNotBlank() ?: false

    fun getMobileAuth() = UserManager.getInstance().userInfoEntity?.mobileAuth

    @SuppressLint("CheckResult")
    fun unBindPhone() {
        api.unBindPhone()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    unBindLiveData.postValue(true)
                }
            })
    }

    companion object {
        const val TYPE_REAL_NAME_CERT = "real_name_cert" // 已完成实名认证
        const val TYPE_BIND_PHONE = "bind_phone" // 已绑定手机
        const val TYPE_BBS_CERT = "bbs_cert" // 已完成社区交流身份认证
    }
}