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

    val data  by lazy { MutableLiveData<XyBean>() }

    val setting by lazy { MutableLiveData<RespBody<List<InfoSetting>>>()  }
    val lastData by lazy { MutableLiveData<RespBody<XyBean.Model>>()  }
    val recordList by lazy{MutableLiveData<RespBody<MutableList<XueyaRecordListBean>>>()}
    /**
     * 获取演示数据
     */
    private suspend fun getDemonstrate(type: String  ): XyBean? {
        var json= JSONObject()
        json.put("type",type)
        json.put("signType",5)//体征类型1，血压，2，心率，3，呼吸率，4，体温，5，血氧，6，血糖，7，睡眠,8,健康报告

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

    fun getRecordList(){
        startHttp {
            var json= JSONObject()
//            json.put("id",id)
            var value=apiInterface().getRecordList()
            value.getCodeStatus(recordList,nowData)
        }
    }
    fun getSoData(type: Int, time: String?, id: String, isDemonstrate: Boolean) {
        if (isDemonstrate){
            startHttp {
                withContext(Dispatchers.IO) {
                    var nowLine = async { getDemonstrate("$type") }
                    var showInfo = nowLine.await()
                    data.postValue(showInfo)
                }
            }
        }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 { getSoFindNow("$type", date, endDate, id) }
                    var showInfo = nowLine.await()
                    data.postValue(showInfo)
                }
            }
        }
    }
    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().getLastSo(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 getSoFindNow(type: String,time:String?,endDate:String?,id:String):XyBean? {
        var json= JSONObject()
        json.put("type",type)
        json.put("date",time)
        json.put("endDate",endDate)
        json.put("id",id)

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

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

}