package com.lukouguoji.gnj.viewModel

import android.util.Log
import androidx.lifecycle.*
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.gnj.model.GnjChuKuListModel
import com.lukouguoji.module_base.common.String.emptyToNull
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

class GnjChuKuListViewModel : ViewModel() {
    /**
     * 航班号
     */
    var fid: Int? = null

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

    /**
     * 选中重量
     */
    val checkCountWeight: LiveData<Double>
        get() = _checkCountWeight

    private val _checkCount = MutableLiveData<Int>()
    private val _checkCountWeight = MutableLiveData<Double>()

    init {
        _checkCount.value = 0
    }

    fun plusOne(model: GnjChuKuListModel) {
        val count = _checkCount.value ?: 0
        _checkCount.value = count + 1
        _checkCountWeight.value = _checkCountWeight.value?.plus(model.weight)
    }

    fun subOne(model: GnjChuKuListModel) {
        val count = _checkCount.value ?: 0
        _checkCount.value = count - 1
        _checkCountWeight.value = _checkCountWeight.value?.minus(model.weight)
    }

    fun setCheckCount(count: Int, weight: Double) {
        _checkCount.value = count
        _checkCountWeight.value = weight
    }


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

        }
    }

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

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

        searchParam.value = param
    }

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


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

        }
    }

    fun completedGnjChuKu(ids: List<Int>) {
        val temObj = JSONObject()
        temObj["fid"] = 0 //随意填，不起作用
        temObj["ids"] = ids

        completedGnjChuKuLiveData.value = temObj
    }

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

        }
    }

    fun queryPickByIdGnjChuKu(pkId: String) {
        queryPickByIdGnjChuKuLiveData.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.agentCodeGnj()
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }
}