package com.cheng.blzb.ui.fragment.bid.detail

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.BidDetailEntity
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import okhttp3.RequestBody.Companion.toRequestBody

class BidDetailViewModel: BaseViewModel() {
    val detailLiveData = MutableLiveData<BidDetailEntity>()
    val favoriteLiveData = MutableLiveData<Any>()
    val cancelLiveData = MutableLiveData<Any>()

    fun getBidDetail(id: String, infoSource: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getBidDetail(id, "recommend", infoSource)
            if (response.status) {
                detailLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun favorite(id: String, infoSource: String) {
        showDialog()
        launchOnUiTryCatch({
            val jsonObject = JsonObject()
            jsonObject.addProperty("infoId", id)
            jsonObject.addProperty("infoSource",infoSource)
            val response = ApiFactory.apiService.favorite(jsonObject.toString().toRequestBody())
            if (response.status) {
                favoriteLiveData.postValue(Any())
            }
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun favoriteCancel(id: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.favoriteCancel(id)
            if (response.status) {
                cancelLiveData.postValue(Any())
            }
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun sendAuth() {
        launchOnUiTryCatch({
            val jsonObject = JsonObject()
            jsonObject.addProperty("scene", "info")
            jsonObject.addProperty("count", 1)
            ApiFactory.apiService.sendAuth(jsonObject.toString().toRequestBody())
        }, {
            L.d(it)
        })
    }
}