package com.lukouguoji.gjj.viewModel

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

    private val logName = "GjjCangDanListViewModel"

    /**
     * 航班号
     */
    var fid: Int? = null
    var prefix: String? = null

    /**
     * 选中
     */
    val checkCount: LiveData<Int>
        get() = _checkCount

    private val _checkCount = MutableLiveData<Int>()

    init {
        _checkCount.value = 0
    }

    fun plusOne() {
        val count = _checkCount.value ?: 0
        _checkCount.value = count + 1
    }

    fun subOne() {
        val count = _checkCount.value ?: 0
        _checkCount.value = count - 1
    }

    fun setCheckCount(count: Int) {
        _checkCount.value = count
    }


    /**
     * 分页搜索
     */
    private val searchParam = MutableLiveData<JSONObject>()
    val searchParamLive = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchGjjCangDan(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 intFlightGjjCangDanLiveData = MutableLiveData<JSONObject>()
    val intFlightGjjCangDanObserver = Transformations.switchMap(intFlightGjjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.queryFlightMitInPack(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun intFlightGjjCangDan(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


        intFlightGjjCangDanLiveData.value = temObj
    }

    /**
     * 删除行
     */
    inner class CangDanDel(val id: Int, val position: Int)

    private val deleteFestGjjCangDanLiveData = MutableLiveData<CangDanDel>()
    val deleteFestGjjCangDanObserver = Transformations.switchMap(deleteFestGjjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.deleteFestGjjCangDan(param.id)
                response["positionDel"] = param.position
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun deleteFestGjjCangDan(id: Int, position: Int) {
        deleteFestGjjCangDanLiveData.value = CangDanDel(id, position)
    }

    /**
     * 货物发放
     */
    private val provideGjjCangDanLiveData = MutableLiveData<JSONObject>()
    val provideGjjCangDanObserver = Transformations.switchMap(provideGjjCangDanLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.provideGjjCangDan(param)
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun provideGjjCangDan(fid: Int, ids: List<Int>) {
        val temObj = JSONObject()
        temObj["fid"] = fid
        temObj["ids"] = ids

        provideGjjCangDanLiveData.value = temObj
    }
}