package com.cheng.blzb.ui.fragment.login

import androidx.lifecycle.MutableLiveData
import com.example.base.common.RxCountDown
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import com.cheng.blzb.net.ApiFactory
import io.reactivex.rxjava3.disposables.Disposable
import okhttp3.RequestBody.Companion.toRequestBody

class LoginViewModel : BaseViewModel() {
    private var disposable: Disposable? = null

    val accountLiveData = MutableLiveData<List<com.cheng.blzb.bean.AccountEntity>>()
    val countTimeLiveEvent = MutableLiveData<Long>()
    var sendCodeLiveData = MutableLiveData<com.cheng.blzb.bean.SendCodeEntity>()
    var phoneLoginLiveData = MutableLiveData<com.cheng.blzb.bean.LoginEntity>()
    var wxLoginLiveData = MutableLiveData<com.cheng.blzb.bean.LoginEntity>()

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

    fun sendCode(phone: String) {
        showDialog()
        launchOnUiTryCatch({
            val map = HashMap<String, String>()
            map["phone"] = phone
            val response = ApiFactory.apiService.sendCode(map)
            if (response.status) {
                sendCodeLiveData.postValue(response.data)
                startTimer()
            } else {
                toast(response.message, true)
            }
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun loginByPhone(timestamp: String, phone:String, code:String) {
        showDialog()
        launchOnUiTryCatch({
            val jsonPhone = JsonObject()
            jsonPhone.addProperty("timestamp", timestamp)
            jsonPhone.addProperty("phone", phone)
            jsonPhone.addProperty("code", code)

            val jsonObject = JsonObject()
            jsonObject.addProperty("login_type", "phone")
            jsonObject.add("phone", jsonPhone)
            val response = ApiFactory.apiService.login(jsonObject.toString().toRequestBody())
            if (response.status) {
                phoneLoginLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun loginByWx(code: String) {
        showDialog()
        launchOnUiTryCatch({
            val jsonWx = JsonObject()
            jsonWx.addProperty("code", code)
            jsonWx.addProperty("code_type", "")

            val jsonObject = JsonObject()
            jsonObject.addProperty("login_type", "weixin")
            jsonObject.add("weixin", jsonWx)
            val response = ApiFactory.apiService.login(jsonObject.toString().toRequestBody())
            if (response.status) {
                wxLoginLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    private fun startTimer() {
        disposable?.dispose()
        disposable = RxCountDown.countdown(60).subscribe {
            countTimeLiveEvent.value = it
        }
    }

    override fun onCleared() {
        super.onCleared()
        disposable?.dispose()
    }

}