package com.lukouguoji.gnj.viewModel

import android.util.Log
import androidx.lifecycle.*
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.gnj.model.GnjRuKuRef
import com.lukouguoji.module_base.common.String.emptyToNull
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers


class GnjRuKuViewModel : ViewModel() {

    /**
     * 选中
     */
    val checkCount: LiveData<Int>
        get() = _checkCount

    private val _checkCount = MutableLiveData<Int>()

    init {
        _checkCount.value = 0
    }

    fun plusOne() {
        val count = _checkCount.value ?: 0
        _checkCount.value = count + 1
    }

    fun subOne() {
        val count = _checkCount.value ?: 0
        _checkCount.value = count - 1
    }

    fun setCheckCount(count: Int) {
        _checkCount.value = count
    }

    /**
     * 入库查询 返回数据
     */
    var wbListArrFromNet = JSONArray()
    var idsArray = JSONArray()
    val gncTransferRefList = arrayListOf<GnjRuKuRef>()

    /**
     * 入库查询
     */
    private val searchGnjRuKuLiveData = MutableLiveData<JSONObject>()
    val searchGnjRuKuObserver = Transformations.switchMap(searchGnjRuKuLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchGnjRuKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GnjRuKuViewModel", e.stackTraceToString())
            }

        }
    }

    fun searchGnjRuKu(
        limit: Int,
        page: Int,
        carNo: String
    ) {
        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["text"] = carNo.emptyToNull()

        searchGnjRuKuLiveData.value = param
    }

    /**
     * 装车确认
     */
    private val loadCarGnjRuKuLiveData = MutableLiveData<JSONObject>()
    val loadCarGnjRuKuObserver = Transformations.switchMap(loadCarGnjRuKuLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.loadCarGnjRuKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GnjRuKuViewModel", e.stackTraceToString())
            }

        }
    }

    fun loadCarGnjRuKu(carNo: String, paramArr: JSONArray) {
        val tempObj = JSONObject()
        tempObj["carNo"] = carNo
        tempObj["wbNo"] = paramArr
        loadCarGnjRuKuLiveData.value = tempObj
    }

    /**
     * 卸车确认
     */
    private val unloadCarGnjRuKuLiveData = MutableLiveData<JSONObject>()
    val unloadCarGnjRuKuObserver = Transformations.switchMap(unloadCarGnjRuKuLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.unloadCarGnjRuKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GnjRuKuViewModel", e.stackTraceToString())
            }

        }
    }

    fun unloadCarGnjRuKu(carNo: String, paramArr: JSONArray) {
        val tempObj = JSONObject()
        tempObj["carNo"] = carNo
        tempObj["wbNo"] = paramArr
        unloadCarGnjRuKuLiveData.value = tempObj
    }
}