package com.lukouguoji.gjj.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
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class GjjChuKuListViewModel : ViewModel() {
    /**
     * 航班号
     */
    var fid: Int? = 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.searchGjjChuKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

    fun search(
        limit: Int,
        page: Int,
        wbNo: String,
        agentCode: String,
        id: String,
        beginDate: String,
        endDate: String
    ) {

        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["wbNo"] = wbNo.emptyToNull()
        param["agentCode"] = agentCode.emptyToNull()
        param["id"] = id.emptyToNull()
        param["beginDate"] = beginDate.emptyToNull()
        param["endDate"] = endDate.emptyToNull()

        searchParam.value = param
    }

    /**
     * 根据运单号后4位查询运单号
     */
    val queryWbNoGjjChuKuParamLive = MutableSharedFlow<JSONObject>(replay = 1)
    fun queryWbNoGjjChuKu(wbNo: String) {
        viewModelScope.launch {
            flow {
                val response = UserNetwork.queryWbNoGjjChuKu(wbNo)
                //返回结果
                emit(response)
            }.flowOn(Dispatchers.Default)
                .catch { e ->
                    Log.e("GnjChuKuListViewModel", e.stackTraceToString())
                }
                .collect { response ->
                    queryWbNoGjjChuKuParamLive.emit(response)
                }
        }
    }


    /**
     * 确认出库
     */
    private val completedGjjChuKuLiveData = MutableLiveData<JSONObject>()
    val completedGjjChuKuObserver = Transformations.switchMap(completedGjjChuKuLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.completedGjjChuKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

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

        completedGjjChuKuLiveData.value = temObj
    }

    /**
     * 根据提货单号查询
     */
    private val queryPickByIdGjjChuKuLiveData = MutableLiveData<String>()
    val queryPickByIdGjjChuKuObserver = Transformations.switchMap(queryPickByIdGjjChuKuLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.queryPickByIdGjjChuKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

    fun queryPickByIdGjjChuKu(pkId: String) {
        queryPickByIdGjjChuKuLiveData.value = pkId
    }

    /**
     * 代理人
     */
    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.agentCodeGjc()
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }
}