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 UricAcidVm : BaseVm() {

    val dataLive by lazy { MutableLiveData<MutableMap<Int, UricAcidInfoBean>>() }

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

    val uricAcideInfo by lazy {MutableLiveData<RespBody<UricAcidHeardInfoBean>>()   }

    val oneDayData by lazy { MutableLiveData<RespBody<UricAcidInfoBean>>() }

    val userInfo by lazy { MutableLiveData<RespBody<UricAcidUserBean>>() }

      fun getUserInfo(id:String){
          startHttp(false) {
              val json= JSONObject()
              json.put("id",id)

              val body = apiInterface().getUserOtherInfo(retrofits.getRequestBody(json.toString()))
              body.getCodeStatus(userInfo,nowData)
          }
    }

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

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

    /**
     * 获取尿酸信息
     */
    fun getUricAcidInfo(type: Int, time: String?, id: String, isDemonstrate: Boolean) {
        if (isDemonstrate){
            startHttp(false) {
                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[0].trim()
                val end = split[1].trim()
                date = start
                endDate = end
            }
            startHttp(false) {
                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 getCurrentUricAcidValue(date:String?,id: String,isDemonstrate: Boolean){
        if (isDemonstrate){
            return
        }
        startHttp(false){
            val json= JSONObject()
            json.put("type","1")
            json.put("date",date)
            json.put("endDate","")
            json.put("id",id)

            val body = apiInterface().getUricAcidFindNow(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(oneDayData,nowData)
        }
    }

    /**
     * 获取尿酸界面头部值
     */
    fun getUricAcidHead(id:String){
        startHttp(false){
            var json=JSONObject()
            json.put("id",id)
            var value=  apiInterface().getUricAcidNowInfo(retrofits.getRequestBody(json.toString()))
            value.getCodeStatus(uricAcideInfo,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): UricAcidInfoBean? {

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

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


}