package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.network.model.*
import org.json.JSONObject

class ExceptionVm: BaseVm() {
    val bindOk by lazy{MutableLiveData<RespBody<PaingBean<BindDevHistoryBean>>>()}
    val exceptionXYData by lazy {MutableLiveData<RespBody<XYBloodPressureBean>>()   }
    val exceptionXLData by lazy {MutableLiveData<RespBody<XLHeartRateBean>>()   }
    val exceptionHXLData by lazy {MutableLiveData<RespBody<HXLBreathingRateBean>>()   }
    val exceptionTWData by lazy {MutableLiveData<RespBody<TWTemperatureBean>>()   }
    val exceptionXOData by lazy {MutableLiveData<RespBody<XYBloodOxygenBean>>()   }

    val exceptionBloodFatData by lazy { MutableLiveData<RespBody<BloodFatExcpBean>>() }

    /**
     * 类型，1，血压计，2，血糖仪
     */
    fun getDeviceHistory(deviceType:Int,date:String?,page:Int){
        startHttp {
            var json=JSONObject()
            json.put("pageNum",page)
            json.put("pageSize", 20)
            date?.let {
                json.put("date",date)
            }
            json.put("deviceType",deviceType)
            var response =apiInterface().getHistoryList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(bindOk,nowData)
        }
    }

    fun getWatchAbnormalDataXY(id:String,type:String,date: String,signType:String){
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date?.split("至")
            val start = split?.get(0)
            val end = split?.get(1)
            date = start!!
            endDate = end
        }
        startHttp {
            var json=JSONObject()
            json.put("id",id)
            json.put("type", type)
            if(type == "1"){  //日报
                json.put("date",date.trim())
                json.put("endDate", "")
            }else{
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }
            json.put("signType", signType)
            var response =apiInterface().getWatchAbnormalDataXY(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(exceptionXYData,nowData)
        }
    }

    fun getWatchAbnormalDataXL(id:String,type:String,date: String,signType:String){
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date?.split("至")
            val start = split?.get(0)
            val end = split?.get(1)
            date = start!!
            endDate = end
        }
        startHttp {
            var json=JSONObject()
            json.put("id",id)
            json.put("type", type)

            if(type == "1"){  //日报
                json.put("date", date.trim())
                json.put("endDate", "")
            }else{
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }

            json.put("signType", signType)
            var response =apiInterface().getWatchAbnormalDataXL(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(exceptionXLData,nowData)
        }
    }

    fun getWatchAbnormalDataHXL(id:String,type:String,date: String,signType:String){
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date?.split("至")
            val start = split?.get(0)
            val end = split?.get(1)
            date = start!!
            endDate = end
        }
        startHttp {
            var json=JSONObject()
            json.put("id",id)
            json.put("type", type)
            if(type == "1"){  //日报
                json.put("date",date.trim())
                json.put("endDate", "")
            }else{
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }
            json.put("signType", signType)
            var response =apiInterface().getWatchAbnormalDataHXL(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(exceptionHXLData,nowData)
        }
    }

    fun getWatchAbnormalDataTW(id:String,type:String,date: String,signType:String){
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date?.split("至")
            val start = split?.get(0)
            val end = split?.get(1)
            date = start!!
            endDate = end
        }
        startHttp {
            var json=JSONObject()
            json.put("id",id)
            json.put("type", type)
            if(type == "1"){  //日报
                json.put("date", date.trim())
                json.put("endDate", "")
            }else{
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }
            json.put("signType", signType)
            var response =apiInterface().getWatchAbnormalDataTW(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(exceptionTWData,nowData)
        }
    }

    fun getWatchAbnormalDataXO(id:String,type:String,date: String,signType:String){
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date?.split("至")
            val start = split?.get(0)
            val end = split?.get(1)
            date = start!!
            endDate = end
        }
        startHttp {
            var json=JSONObject()
            json.put("id",id)
            json.put("type", type)
            if(type == "1"){  //日报
                json.put("date", date.trim())
                json.put("endDate", "")
            }else{
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }
            json.put("signType", signType)
            var response =apiInterface().getWatchAbnormalDataXO(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(exceptionXOData,nowData)
        }
    }

    fun getWatchAbnormalDataBloodFat(id: String, type: String, date: String, signType: String) {
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date.split("至")
            val start = split[0]
            val end = split[1]
            date = start
            endDate = end
        }
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            json.put("type", type)
            if (type == "1") {  //日报
                json.put("date", date.trim())
                json.put("endDate", "")
            } else {
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }
            json.put("signType", signType)
            val response = apiInterface().getWatchAbnormalDataBloodFat(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            response.getCodeStatus(exceptionBloodFatData, nowData)
        }
    }

}