package com.cheng.blzb.ui.fragment.guide

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.AreaEntity
import com.cheng.blzb.bean.BidTypeEntity
import com.cheng.blzb.bean.GuideTotalBidEntity
import com.cheng.blzb.bean.HotWordEntity
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel

class GuideViewModel :BaseViewModel(){
    val hotWordLiveData = MutableLiveData<List<HotWordEntity>>()
    val cityLiveData = MutableLiveData<List<AreaEntity>>()
    val bidTypeLiveData = MutableLiveData<List<BidTypeEntity>>()
    val userCityLiveData = MutableLiveData<AreaEntity>()
    val totalLiveData = MutableLiveData<GuideTotalBidEntity>()

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

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

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

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

    fun getTotalBidInfo(keywords: String, cityIds: String, kindNum: String) {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getTotalBidInfo(keywords, cityIds, kindNum)
            if (response.status) {
                totalLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }
}