package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.network.model.InfoSetting
import com.xty.network.model.RespBody
import com.xty.network.model.TempBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.json.JSONObject

class TempVm : BaseVm() {

    val data by lazy { MutableLiveData<MutableMap<Int, TempBean>>() }
    val setting by lazy { MutableLiveData<RespBody<List<InfoSetting>>>() }
    val lastData by lazy { MutableLiveData<RespBody<TempBean.Model>>() }

    private suspend fun getDemonstrate(type: String  ): TempBean? {
        var json= JSONObject()
        json.put("type",type)
        json.put("signType",4)//体征类型1，血压，2，心率，3，呼吸率，4，体温，5，血氧，6，血糖，7，睡眠,8,健康报告

        var body = apiInterface().findDemonstrateWatchTemp(retrofits.getRequestBody(json.toString()))
        return body.getLoginStatus(nowData)
    }

    fun getTempData(type: Int, time: String?, id: String, isDemonstrate: Boolean) {

        if (isDemonstrate){
            startHttp {
                withContext(Dispatchers.IO) {
                    var nowLine = async { getDemonstrate("$type" ) }
                    var showInfo = nowLine.await()

                    var dataMap = setMapValue(data.value, showInfo, type)
                    if (dataMap != null) {
                        data.postValue(dataMap)
                    }
                }
            }

        }else {
            var endDate: String? = ""
            var date = time
            if (type == 2) {
                val split = date?.split("至")
                val start = split?.get(0)
                val end = split?.get(1)
                date = start
                endDate = end
            }
            startHttp {
                withContext(Dispatchers.IO) {
                    var nowLine = async { getTempFindNow("$type", date, endDate, id) }
                    var showInfo = nowLine.await()

                    var dataMap = setMapValue(data.value, showInfo, type)
                    if (dataMap != null) {
                        data.postValue(dataMap)
                    }
                }
            }
        }
    }



    fun getSetting(type: Int) {
        startHttp {
            var value = apiInterface().getWatchSetting(type)
            value.getCodeStatus(setting, nowData)
        }
    }

    fun getLastData(id:String) {
        startHttp {
            var json= JSONObject()
            json.put("id",id)
            var value = apiInterface().getLastTemp(retrofits.getRequestBody(json.toString()))
            value.getCodeStatus(lastData, nowData)
        }
    }

    fun <T> setMapValue(map: MutableMap<Int, T>?, data: T?, type: Int): MutableMap<Int, T>? {
        if (data == null) {
            return null
        } else {  //当得到的值不是空的时候
            var mapNew = mutableMapOf<Int, T>()
            if (map == null) {
                mapNew[type] = data
                return mapNew
            } else {
                map[type] = data
                return map
            }
        }
    }


    //获取当前曲线的信息
    private suspend fun getTempFindNow(type: String,time:String?,endTime:String?,id:String): TempBean? {
        var json= JSONObject()
        json.put("type",type)
        json.put("date",time)
        json.put("endDate",endTime)
        json.put("id",id)

        var body = apiInterface().getTempFindNow(retrofits.getRequestBody(json.toString()))
        return body.getLoginStatus(nowData)
    }

    //获取平均值
    private suspend fun getTempAvgFindNow(type: String,time:String?): TempBean? {
        var body = apiInterface().getTempAvgFindNow("$type",time)
        return body.getLoginStatus(nowData)
    }


}