package com.lukouguoji.gnc.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.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.http.user.UserNetwork
import com.lukouguoji.gnc.model.GncFuBang
import kotlinx.coroutines.Dispatchers

class GoutFuBangViewModel : ViewModel() {
    /**
     * 接口返回复磅信息
     */
    var fuBangInfoFormNetWork = JSONObject()
    val fuBangInfo = GncFuBang()
    private  var weightName=""
    fun setWeightName(name :String)
    {
        weightName=name
    }
    private val weightDataLiveData = MutableLiveData<String>()
    /**
     * 板车详情
     */
    private val carData = MutableLiveData<CarParam>()
    val carDataObserver = Transformations.switchMap(carData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = JSONObject()
                if (param.carNo != null) {
                    response = UserNetwork.queryWeightingByNo(param.carNo)
                } else if (param.userId != null) {
                    response = UserNetwork.queryWeightingById(param.userId)
                } else {
                }
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutFuBangViewModel", e.stackTraceToString())
            }

        }
    }
    fun weightData(value: String) {
        weightDataLiveData.postValue(value)
    }

    var   weightThread=  Thread {
        while (true) {
            try {
                if(!weightName.equals(""))
                {
                    var res=  UserNetwork.getWeightData(weightName)
                    var repsonse=  res.execute()
                    var xml:StringBuffer= StringBuffer(repsonse.body().toString())
                    var str=">"
                    var len=str.length
                    var start=xml.indexOf(str)
                    //  xml = xml.replace(0,1,"")
                    xml = xml.replace(0,start+len,"")
                    xml = xml.replace(0,start+len+1,"")
                    // xml=   xml.replace(0,len,"")
                    //  Log.i("WeightThread0",xml.toString())
                    var end =xml.indexOf("</decimal>")
                    xml=   xml.replace(end,xml.length,"")
                    //   Log.i("WeightThread",xml.toString())
                    // val str = "kotlin very good"
                    // str.first{ it == 'o' }
                    weightData(xml.toString())
                }
            }catch ( e:Exception){
                Log.e("weightThread1", e.stackTraceToString())
            }
            try {
                Thread.sleep(1000)
            }catch ( e:Exception){
                Log.e("weightThread2", e.stackTraceToString())
            }


        }
    } as Thread
    val weightDataObserver = Transformations.switchMap(weightDataLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                emit(weightDataLiveData.value.toString())
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }
    fun queryWaybillById(carNo: String?, userId: String?) {
        carData.value = CarParam(carNo, userId)
    }

    /**
     * 货物类型 下拉框
     */
    var cargoList = JSONArray()
    private val cargoLiveData = MutableLiveData<String>()
    val cargoObserver = Transformations.switchMap(cargoLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.cargoSelect()
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutFuBangViewModel", e.stackTraceToString())
            }

        }
    }
    fun cargoSelect(type: String) {
        cargoLiveData.value = type
    }

    /**
     * 板型 下拉框
     */
    var boardList = JSONArray()
    private val boardLiveData = MutableLiveData<String>()
    val boardObserver = Transformations.switchMap(boardLiveData) { _ ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.cargoSelect()
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutFuBangViewModel", e.stackTraceToString())
            }

        }
    }
    fun boardSelect(type: String) {
        boardLiveData.value = type
    }

    /**
     * 完成复磅
     */
    private val completedLiveData = MutableLiveData<JSONObject>()
    val completedObserver = Transformations.switchMap(completedLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.completedFuBang(param!!)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }
    fun completed(gncFuBang: GncFuBang) {
        val jsonObject = fuBangInfoFormNetWork
        jsonObject["carId"] = gncFuBang.carId
        jsonObject["netWeight"] = gncFuBang.netWeight
        jsonObject["volume"] = gncFuBang.volume
        jsonObject["flight"] = gncFuBang.flight
        jsonObject["fdest"] = gncFuBang.fdest
        jsonObject["carWeight"] = gncFuBang.carWeight
        jsonObject["location"] = gncFuBang.location
        jsonObject["pc"] = gncFuBang.pc
        jsonObject["mailWeight"] = gncFuBang.mailWeight
        jsonObject["priority"] = gncFuBang.priority
        jsonObject["cargoType"] = gncFuBang.cargoType
        jsonObject["uld"] = gncFuBang.uld
        jsonObject["uldWeight"] = gncFuBang.uldWeight
        jsonObject["fdate"] = gncFuBang.fdate
        completedLiveData.value = jsonObject
    }
}

class CarParam(val carNo: String?, val userId: String?)