package com.lukouguoji.hangban.viewModel

import android.util.Log
import androidx.lifecycle.*
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 HangBanListViewModel : ViewModel() {

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

        }
    }

    fun search(
        limit: Int,
        page: Int,
        beginDate: String,
        endDate: String,
        countryType: String,
        flight: String,
        flightType: String,
        fno: String,
        range: String,
        status: String
    ) {

        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["beginDate"] = beginDate.emptyToNull()
        param["endDate"] = endDate.emptyToNull()
        param["countryType"] = countryType.emptyToNull()
        param["flight"] = flight.emptyToNull()
        param["flightType"] = flightType.emptyToNull()
        param["fno"] = fno.emptyToNull()
        param["range"] = range.emptyToNull()
        param["status"] = status.emptyToNull()

        searchParam.value = param
    }


    /**
     * 航班类型
     */
    var flightTypeList = JSONArray()
    private val flightTypeLiveData = MutableLiveData<String>()
    fun flightType(time: String) {
        flightTypeLiveData.value = time
    }

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

        }
    }

    /**
     * 地区类型
     */
    var countryTypeList = JSONArray()
    private val countryTypeLiveData = MutableLiveData<String>()
    fun countryType(time: String) {
        countryTypeLiveData.value = time
    }

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

        }
    }

    /**
     * 进出港
     */
    val inOrOutList = JSONArray()
    fun initInOrOutList(){
        val inOrOutObj1 = JSONObject()
        inOrOutObj1["code"] = ""
        inOrOutObj1["name"] = "请选择进出港"
        inOrOutList.add(inOrOutObj1)

        val inOrOutObj2 = JSONObject()
        inOrOutObj2["code"] = "0"
        inOrOutObj2["name"] = "出港"
        inOrOutList.add(inOrOutObj2)

        val inOrOutObj3 = JSONObject()
        inOrOutObj3["code"] = "1"
        inOrOutObj3["name"] = "进港"
        inOrOutList.add(inOrOutObj3)
    }
}