package com.cheng.blzb.ui.fragment.mine.account

import androidx.lifecycle.MutableLiveData
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.cheng.blzb.net.ApiFactory
import okhttp3.RequestBody

class BindAccountViewModel : BaseViewModel() {
    val userInfoLiveData = MutableLiveData<com.cheng.blzb.bean.UserEntity>()
    var bindPhoneLiveData = MutableLiveData<String>()
    var unbindPhoneLiveData = MutableLiveData<String>()

    fun userInfo() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.userInfo()
            if (response.status) {
                userInfoLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun sendCode(phone: String, callback: (com.cheng.blzb.bean.SendCodeEntity) -> Unit) {
        showDialog()
        launchOnUiTryCatch({
            val map = HashMap<String, String>()
            map["phone"] = phone
            val response = ApiFactory.apiService.sendCode(map)
            if (response.status) {
                callback.invoke(response.data)
            } else {
                toast(response.message, true)
            }
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    //绑定 手机，微信
    fun bind(requestBody: RequestBody, type: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.login(requestBody)
            if (response.status) {
                bindPhoneLiveData.postValue(type)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })

    }

    //解绑 手机，微信
    fun unbind(requestBody: RequestBody, type: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.login(requestBody)
            if (response.status) {
                unbindPhoneLiveData.postValue(type)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })

    }
}