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 GjjWareHouseListViewModel : ViewModel() {

    private val logName = "GjjWareHouseListViewModel"

    /**
     * 分页搜索
     */
    private val searchParam = MutableLiveData<JSONObject>()
    val searchParamLive = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchGjjCangKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun search(
        limit: Int,
        page: Int,
        carrier: String,
        beginDate: String,
        endDate: String,
        fdate: String,//航班日期
        fno: String,//航班编码
        fdep: String,//上一个港
        dest: String,//目的港
        businessType: String,//业务类型
        range: String,
        status: String,
        wbNo: String,
        groupName: String,
        agentCode: String,
        location: String
    ) {
        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["carrier"] = carrier.emptyToNull()
        param["beginDate"] = beginDate.emptyToNull()
        param["endDate"] = endDate.emptyToNull()
        param["fdate"] = fdate.emptyToNull()
        param["fdep"] = fdep.emptyToNull()
        param["dest"] = dest.emptyToNull()
        param["fno"] = fno.emptyToNull()
        param["businessType"] = businessType.emptyToNull()
        param["range"] = range.emptyToNull()
        param["locStatus"] = status.emptyToNull()
        param["wbNo"] = wbNo.emptyToNull()
        param["groupName"] = groupName.emptyToNull()
        param["agentCode"] = agentCode.emptyToNull()
        param["agentCode"] = agentCode.emptyToNull()
        param["location"] = location.emptyToNull()

        searchParam.value = param
    }

    /**
     * 数据汇总
     */
    val summaryObserver = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.totalGjjCangKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    /**
     * 代理人
     */
    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(logName, 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(logName, e.stackTraceToString())
            }

        }
    }
}