package com.lukouguoji.gjc.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.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.gjc.model.GjcStartReceiveWaybill
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers

class GjcStartReceiveViewModel : ViewModel() {
    var receiveWaybill = GjcStartReceiveWaybill()
    var waybillFormNetWork = JSONObject()

    var agentCodeList = JSONArray()
    var businessList = JSONArray()
    var specialCodeList = JSONArray()
    var dgrDetailList = JSONArray()

    private val wbIdData = MutableLiveData<WaybillParam>()
    private val businessLiveData = MutableLiveData<String>()
    private val specialCodeLiveData = MutableLiveData<String>()


    inner class WaybillParam(val wbId: Int, val wbNo: String?)

    val waybillLiveData = Transformations.switchMap(wbIdData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = JSONObject()
                if (param.wbId != -9999) {
                    response = UserNetwork.queryWbByIdGjcReceive(param.wbId)
                } else if (param.wbNo != null) {
                    response = UserNetwork.queryWbByNoGjcReceive(param.wbNo)
                } else {
                }
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun queryWaybillById(wbId: Int, wbNo: String?) {
        wbIdData.value = WaybillParam(wbId, wbNo)
    }


    fun business(type: String) {
        businessLiveData.value = type
    }

    val businessObserver = Transformations.switchMap(businessLiveData) { type ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.business(type)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun specialCode(time: String) {
        specialCodeLiveData.value = time
    }

    val specialCodeObserver = Transformations.switchMap(specialCodeLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.specialCode()
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }


    private val completedLiveData = MutableLiveData<JSONObject>()

    val completedObserver = Transformations.switchMap(completedLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.completedGjcReceive(param!!)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun completed(waybill: GjcStartReceiveWaybill) {
        var prefix: String = ""
        var no: String = ""

        val jsonObject = waybillFormNetWork
        jsonObject["maWbId"] = waybill.maWbId
        jsonObject["wbNo"] = waybill.wbNo

        jsonObject["agentCode"] = waybill.agentCode
        jsonObject["goods"] = waybill.goods
        jsonObject["spCode"] = waybill.spCode
        jsonObject["businessType"] = waybill.businessType
        jsonObject["awbType"] = waybill.awbType

        jsonObject["pc"] = waybill.pc
        jsonObject["weight"] = waybill.weight
        jsonObject["volume"] = waybill.volume
        jsonObject["arrivePc"] = waybill.arrivePc
        jsonObject["arriveWeight"] = waybill.arriveWeight
        jsonObject["arriveVolume"] = waybill.arriveVolume

        jsonObject["activeId"] = waybill.activeId
        jsonObject["remark"] = waybill.remark

        jsonObject["dep"] = waybill.dep
        jsonObject["dest"] = waybill.dest
        jsonObject["dest1"] = waybill.dest1
        jsonObject["by1"] = waybill.by1
        jsonObject["checkIn"] = waybill.checkIn
        completedLiveData.value = jsonObject
    }


    /**
     *  集装器编号，查询集装器自重
     */
    private val queryUldGjcReceiveLiveData = MutableLiveData<String>()
    val queryUldGjcReceiveObserver = Transformations.switchMap(queryUldGjcReceiveLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.queryUldGjcReceive(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun queryUldGjcReceive(id: String) {
        queryUldGjcReceiveLiveData.value = id
    }

    /**
     *  集装器编号，校验ULD重量
     */
    private val updateUldGjcReceiveLiveData = MutableLiveData<JSONObject>()
    val updateUldGjcReceiveObserver = Transformations.switchMap(updateUldGjcReceiveLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.updateUldGjcReceive(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun updateUldGjcReceive(uld: String, uldWeight: Double) {
        val param = JSONObject()
        param["uld"] = uld
        param["uldWeight"] = uldWeight
        updateUldGjcReceiveLiveData.value = param
    }

    /**
     *  托盘编号，查询托盘自重
     */
    private val queryFlatcarGjcReceiveLiveData = MutableLiveData<String>()
    val queryFlatcarGjcReceiveObserver = Transformations.switchMap(queryFlatcarGjcReceiveLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.queryFlatcarGjcReceive(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun queryFlatcarGjcReceive(id: String) {
        queryFlatcarGjcReceiveLiveData.value = id
    }

    /**
     *  托盘编号，校验平板车重量
     */
    private val updateFlatcarGjcReceiveLiveData = MutableLiveData<JSONObject>()
    val updateFlatcarGjcReceiveObserver = Transformations.switchMap(updateFlatcarGjcReceiveLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.updateFlatcarGjcReceive(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

    fun updateFlatcarGjcReceive(carId: String, carWeight: Double) {
        val param = JSONObject()
        param["carId"] = carId
        param["carWeight"] = carWeight
        updateFlatcarGjcReceiveLiveData.value = param
    }

}