package com.zj365.health.vm

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

class XlVm : BaseVm() {

   // val avgLive by lazy { MutableLiveData<MutableMap<Int, XlAvg>>() }
    val dataLive by lazy { MutableLiveData<MutableMap<Int, XlBean>>() }

    val setting by lazy { MutableLiveData<RespBody<List<InfoSetting>>>()  }

    val lastData by lazy {MutableLiveData<RespBody<XlBean.Model>>()   }

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

        var body = apiInterface().findDemonstrateWatchHr(retrofits.getRequestBody(json.toString()))
        return body.getLoginStatus(nowData)
    }
    fun getXlData(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(dataLive.value, showInfo, type)
                    if (dataMap != null) {
                        dataLive.postValue(dataMap)
                    }
                }
            }
        }else {
            var endDate: String? = ""
            var date = time
            if (type == 2) {
                val split = date?.split("至")
                if (split == null || split.size < 2) {
                    return
                }
                val start = split?.get(0)
                val end = split?.get(1)
                date = start
                endDate = end
            }
            startHttp {
                withContext(Dispatchers.IO) {
                    var nowLine = async { getFindNow("$type", date, endDate, id) }
                    var showInfo = nowLine.await()

                    var dataMap = setMapValue(dataLive.value, showInfo, type)
                    if (dataMap != null) {
                        dataLive.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().getLastXl(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 getFindNow(type: String,time:String?,endTime:String?,id:String): XlBean? {

        var json= JSONObject()
        json.put("type",type)
        json.put("date",time)
        json.put("endDate",endTime)
        json.put("id",id)

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

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