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.SleepBean
import com.xty.network.model.SleepStatistcBean
import org.json.JSONObject

class SleepVm : BaseVm() {
    val data by lazy { MutableLiveData<List<SleepBean>>() }
    val setting by lazy { MutableLiveData<RespBody<List<InfoSetting>>>()  }
    val sleepLive by lazy { MutableLiveData<RespBody<SleepBean>>()  }
    val sleeStatisticpLive by lazy { MutableLiveData<RespBody<SleepStatistcBean>>()  }

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

    /**
     * 最后一条数据
     */
    fun getSleepLastTata(id: String?, date: String, isDemonstrate: Boolean){
        if (isDemonstrate){
            startHttp {
                var json = JSONObject()
                json.put("type",1)
                json.put("signType",7)//体征类型1，血压，2，心率，3，呼吸率，4，体温，5，血氧，6，血糖，7，睡眠,8,健康报告
                var sleep = apiInterface().findDemonstrateNowSleep(retrofits.getRequestBody(json.toString()))
                sleep.getCodeStatus(sleepLive, nowData)
            }
        }else {
            startHttp {
                var json = JSONObject()
                id?.let {
                    json.put("id", id)
                }
                json.put("date", date)
                var sleep = apiInterface().getTodaySleep(retrofits.getRequestBody(json.toString()))
                sleep.getCodeStatus(sleepLive, nowData)
            }
        }
    }

    /**
     * 周月年接口
     */
    fun getSleepStatistic(
        id: String?,
        type: Int,
        date: String,
        endDate: String,
        isDemonstrate: Boolean
    ){
        if (isDemonstrate){
            startHttp {
                var json = JSONObject()
                json.put("type", type)
                json.put("signType", 7)//体征类型1，血压，2，心率，3，呼吸率，4，体温，5，血氧，6，血糖，7，睡眠,8,健康报告
                var sleep =
                    apiInterface().findDemonstrateSleep(retrofits.getRequestBody(json.toString()))
                sleep.getCodeStatus(sleeStatisticpLive, nowData)
            }
        }else {
            startHttp {
                var json= JSONObject()
                if (id == null){
                    json.put("id","")
                } else {
                    json.put("id",id)
                }
                json.put("date",date)
                json.put("type",type)
                if (type == 2){
                    json.put("endDate",endDate)
                }
                var sleep =apiInterface().getStatisticSleepList(retrofits.getRequestBody(json.toString()))
                sleep.getCodeStatus(sleeStatisticpLive,nowData)
            }
        }
    }


}