package com.lukouguoji.gnc.viewModel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers

class GoutFuBangListViewModel : ViewModel() {

    private val searchParam = MutableLiveData<JSONObject>()

    val searchParamLive = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchFuBang(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

    fun search(limit: Int, page: Int, text: String,ext :String) {
        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["text"] = text
        param["ext"] = ext
        searchParam.value = param
    }
    private val chukuParam = MutableLiveData<JSONObject>()

    val chukuParamLive = Transformations.switchMap(chukuParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.gncChuku(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

    fun chuku(useId: Int,carId: String) {
        val param = JSONObject()
        param["useId"] = useId
        param["carId"] = carId

        chukuParam.value = param
    }
    private val loadParam = MutableLiveData<JSONObject>()
    val loadParamLive = Transformations.switchMap(loadParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.gncload(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

    fun load(useId: Int,carId: String) {
        val param = JSONObject()
        param["useId"] = useId
        param["carId"] = carId

        loadParam.value = param
    }

}