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

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

    val calibrationHistoryLive by lazy{MutableLiveData<RespBody<MutableList<BloodHistoryBean>>>()}
    val recordList by lazy{MutableLiveData<RespBody<MutableList<XueyaRecordListBean>>>()}
    fun getRecordList(){
        startHttp {
            var json= JSONObject()
//            json.put("id",id)
            var value=apiInterface().getRecordList()
            value.getCodeStatus(recordList,nowData)
        }
    }
    /**
     * 校准记录
     */
    fun calibrationHistory(id:String){
        var json=JSONObject()
        json.put("id",id)
        startHttp {
            var body= apiInterface().calibrationHistory(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(calibrationHistoryLive,nowData)
        }
    }
    private suspend fun getDemonstrate(type: String  ): DbpBean? {
        var json= JSONObject()
        json.put("type",type)
        json.put("signType",1)//体征类型1，血压，2，心率，3，呼吸率，4，体温，5，血氧，6，血糖，7，睡眠,8,健康报告

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

    fun getXyaData(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(dbpValue.value, showInfo, type)
                    if (dataMap != null) {
                        dbpValue.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 { getDbpFindNow("$type", date, endDate, id) }
                    var showInfo = nowLine.await()

                    var dataMap = setMapValue(dbpValue.value, showInfo, type)
                    if (dataMap != null) {
                        dbpValue.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().getLastDbp(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 getDbpFindNow(type: String,time:String?,endDate:String?,id:String): DbpBean? {
        var json=JSONObject()
        json.put("type",type)
        json.put("date",time)
        json.put("endDate",endDate)
        json.put("id",id)

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

    //获取平均值
    private suspend fun getDbpAvgFindNow(type: String,time:String?,id:String): DbpBean? {
        var json=JSONObject()
        json.put("type",type)
        json.put("date",time)
        json.put("id",id)
        var body = apiInterface().getDbpAvgFindNow(retrofits.getRequestBody(json.toString()))
        return body.getLoginStatus(nowData)
    }

}