package com.lukouguoji.gjj.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.module_base.common.String.emptyToNull
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers


class GjjCangDanInfoViewModel : ViewModel() {
    private val logName = "GjjCangDanInfoViewModel"

    var waybillFormNetWork = JSONObject()

    /**
     * 进港 舱单详情
     */
    inner class WaybillParam(val wbId: Int, val wbNo: String?)

    private val wbIdData = MutableLiveData<WaybillParam>()
    val waybillLiveData = Transformations.switchMap(wbIdData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = JSONObject()
                if (param.wbId != -9999) {
                    response = UserNetwork.queryFestByIdGjjCangDan(param.wbId)
                } else if (param.wbNo != null) {
//                    response = UserNetwork.queryFestByWbNoGjjCangDan(param.wbNo)//tw2022年7月9日19:18:22   这个接口已经从后台删除了
                } else {
                }
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

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


    /**
     * 代理人
     */
    var agentCodeList = JSONArray()
    private val agentCodeLiveData = MutableLiveData<String>()
    fun agentCode(time: String) {
        agentCodeLiveData.value = time
    }

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

        }
    }

    /**
     * 业务类型
     */
    var businessList = JSONArray()
    private val businessLiveData = MutableLiveData<String>()
    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())
            }

        }
    }

    /**
     * 特码
     */
    var specialCodeList = JSONArray()
    private val specialCodeLiveData = MutableLiveData<String>()
    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())
            }

        }
    }

    /**
     * 运单类型
     */
    var awbList = JSONArray()
    private val awbLiveData = MutableLiveData<String>()
    fun awb(type: String) {
        awbLiveData.value = type
    }

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

        }
    }


    /**
     * 进港 新增舱单
     */
    private val saveFestGjjCangDanLiveData = MutableLiveData<JSONObject>()
    val saveFestGjjCangDanObserver = Transformations.switchMap(saveFestGjjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.saveFestGjjCangDan(param)
                emit(response)

            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun saveFestGjjCangDan(
        fid: Int,
        prefix: String?,
        no: String?,
        agentCode: String?,
        spCode: String,
        location: String,
        totalPc: Int,
        pc: Int,
        weight: Double,
        goods: String?,
        origin: String?,
        dest: String?,
        businessType: String?,
        remark: String?,
        awbType: String?,
        cashWeight: Double,
        consignee: String?,
        cneeId: String?,
        cneeTel: String?
    ) {
        val temObj = JSONObject()
        temObj["fid"] = fid
        temObj["prefix"] = prefix.emptyToNull()
        temObj["no"] = no.emptyToNull()
        temObj["agentCode"] = agentCode.emptyToNull()
        temObj["spCode"] = spCode.emptyToNull()
        temObj["location"] = location.emptyToNull()
        temObj["totalPc"] = totalPc
        temObj["pc"] = pc
        temObj["weight"] = weight
        temObj["goods"] = goods.emptyToNull()
        temObj["origin"] = origin.emptyToNull()
        temObj["dest"] = dest.emptyToNull()
        temObj["businessType"] = businessType.emptyToNull()
        temObj["remark"] = remark.emptyToNull()
        temObj["awbType"] = awbType.emptyToNull()
        temObj["cashWeight"] = cashWeight
        temObj["consignee"] = consignee.emptyToNull()
        temObj["cneeId"] = cneeId.emptyToNull()
        temObj["cneeTel"] = cneeTel.emptyToNull()

        saveFestGjjCangDanLiveData.value = temObj
    }

    /**
     * 进港 修改舱单
     */
    private val updateFestGjjCangDanLiveData = MutableLiveData<JSONObject>()
    val updateFestGjjCangDanObserver = Transformations.switchMap(updateFestGjjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.updateFestGjjCangDan(param)
                emit(response)

            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun updateFestGjjCangDan(
        mfId: Int,
        fid: Int,
        prefix: String,
        no: String,
        agentCode: String,
        spCode: String,
        location: String,
        totalPc: Int,
        pc: Int,
        weight: Double,
        goods: String,
        origin: String,
        dest: String,
        businessType: String,
        remark: String,
        awbType: String,
        cashWeight: Double,
        consignee: String,
        cneeId: String,
        cneeTel: String
    ) {

        val temObj = JSONObject()
        temObj["mfId"] = mfId
        temObj["fid"] = fid
        temObj["prefix"] = prefix.emptyToNull()
        temObj["no"] = no.emptyToNull()
        temObj["agentCode"] = agentCode.emptyToNull()
        temObj["spCode"] = spCode.emptyToNull()
        temObj["location"] = location.emptyToNull()
        temObj["totalPc"] = totalPc
        temObj["pc"] = pc
        temObj["weight"] = weight
        temObj["goods"] = goods.emptyToNull()
        temObj["origin"] = origin.emptyToNull()
        temObj["dest"] = dest.emptyToNull()
        temObj["businessType"] = businessType.emptyToNull()
        temObj["remark"] = remark.emptyToNull()
        temObj["awbType"] = awbType.emptyToNull()
        temObj["cashWeight"] = cashWeight
        temObj["consignee"] = consignee.emptyToNull()
        temObj["cneeId"] = cneeId.emptyToNull()
        temObj["cneeTel"] = cneeTel.emptyToNull()

        updateFestGjjCangDanLiveData.value = temObj
    }
}

