package com.lukouguoji.mit.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.JSONObject
import com.lukouguoji.module_base.common.String.emptyToNull
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers
import kotlin.collections.set

class MitInPackListViewModel : ViewModel() {

    private val logName = "MitInPackListViewModel"

    /**
     * 航班号
     */
    var fid: Int? = null
    var fdate: String? = null
    var fno: String? = null
    var countryName: String? = null
    var fdep: String? = null
    var fdest: String? = null


    /**
     * 分页搜索
     */
    private val searchParam = MutableLiveData<JSONObject>()
    val searchParamLive = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchMitInPack(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,//航班编码
        dest: String,//目的港
        location: String,//舱位
        range: String,
        status: String,
        wbNo: String,
        groupName: String,
        agentCode: String,
        awbType: String,
        businessType: String,
        goods: String,
        spCode: 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["dest"] = dest.emptyToNull()
        param["fno"] = fno.emptyToNull()
        param["location"] = location.emptyToNull()
        param["range"] = range.emptyToNull()
        param["locStatus"] = status.emptyToNull()
        param["wbNo"] = wbNo.emptyToNull()
        param["groupName"] = groupName.emptyToNull()
        param["agentCode"] = agentCode.emptyToNull()
        param["awbType"] = awbType.emptyToNull()
        param["businessType"] = businessType.emptyToNull()
        param["goods"] = goods.emptyToNull()
        param["spCode"] = spCode.emptyToNull()

        searchParam.value = param
    }

    /**
     * 查询航班
     */
    private val queryFlightLiveData = MutableLiveData<JSONObject>()
    val queryFlightObserver = Transformations.switchMap(queryFlightLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.queryFlightMitInPack(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun queryFlight(fdate: String, fno: String) {
        val temObj = JSONObject()
        temObj["countryType"] = null
        temObj["fclose"] = null
        temObj["fdate"] = fdate.emptyToNull()
        temObj["fdep"] = null
        temObj["fdest"] = null
        temObj["fid"] = null
        temObj["fno"] = fno.emptyToNull()
        temObj["mclose"] = null


        queryFlightLiveData.value = temObj
    }

    /**
     * 删除行
     */
    private val deleteLiveData = MutableLiveData<JSONObject>()
    val deleteObserver = Transformations.switchMap(deleteLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.deleteMitInPack(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun delete(fid: Int, carId: String) {
        val tempObj = JSONObject()
        tempObj["id"] = fid
        tempObj["location"] = carId.emptyToNull()
        deleteLiveData.value = tempObj
    }

}