package com.cheng.bole.ui.fragment.home

import androidx.collection.ArrayMap
import androidx.lifecycle.MutableLiveData
import com.cheng.bole.bean.BidItemBean
import com.cheng.bole.bean.UserAuthBean
import com.cheng.bole.net.ApiFactory
import com.cheng.bole.net.model.toListResult
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.ListViewModel

class HomeViewModel : ListViewModel<BidItemBean>() {
    val totalLiveData = MutableLiveData<String>()
    val authLiveData = MutableLiveData<UserAuthBean>()
    val recommendLiveData = MutableLiveData<List<String>>()

    override suspend fun requestApi(params: ArrayMap<String, String>): Result<List<BidItemBean>> {
        val response = ApiFactory.apiService.getRecommendList(params)
        if (response.status) {
            totalLiveData.postValue(response.data.total)
        }
        return response.toListResult()
    }

    fun getHotKeywordList(query: String) {
        launchOnUiTryCatch({
            val params = mutableMapOf<String, String>()
            params["query"] = query
            params["size"] = "4"
            val response = ApiFactory.apiService.getHotKeywords(params)
            if (response.status) {
                recommendLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }

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

}