package com.lukouguoji.gnj.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 GnjQueryListViewModel : ViewModel() {

    private val searchParam = MutableLiveData<JSONObject>()

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

    fun search(
        limit: Int,
        page: Int,
        carrier: String,//承运人
        agentCode: String,//代理人
        awbType: String,//运单类型
        beginDate: String,
        endDate: String,
        fdate: String,//航班日期
        fno: String,//航班编码
        dest: String,//目的港
        location: String,//舱位
        range: String,
        businessType: String,//业务类型
        locStatus: String,//是否仓舱单
        wbNo: String,
        spCode: String, //特码
        goods: String //商品名
    ) {

        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["carrier"] = carrier.emptyToNull()
        param["agentCode"] = agentCode.emptyToNull()
        param["awbType"] = awbType.emptyToNull()
        param["beginDate"] = beginDate.emptyToNull()
        param["endDate"] = endDate.emptyToNull()
        param["fdate"] = fdate.emptyToNull()
        param["dest"] = dest.emptyToNull()
        param["fno"] = fno.emptyToNull()
        param["location"] = location.emptyToNull()
        param["range"] = range.emptyToNull()
        param["businessType"] = businessType.emptyToNull()
        param["locStatus"] = locStatus.emptyToNull()
        param["wbNo"] = wbNo.emptyToNull()
        param["spCode"] = spCode.emptyToNull()
        param["goods"] = goods.emptyToNull()

        searchParam.value = param
    }

    /**
     * 代理人
     */
    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.agentCodeGnj()
                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(time: String) {
        awbLiveData.value = time
    }

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

        }
    }
}