package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.xty.base.vm.BaseVm
import com.xty.common.Const
import com.xty.common.LogUtils
import com.xty.common.util.CommonToastUtils
import com.zj365.health.model.BloodBean
import com.xty.network.model.*
import org.json.JSONObject

class BloodCalibrationVm : BaseVm() {

    val calibrationLive by lazy { MutableLiveData<RespBody<CalibrationBean>>() }
    val calibrationHistoryLive by lazy { MutableLiveData<RespBody<MutableList<BloodHistoryBean>>>() }
    val calibrationHistoryLiveNew by lazy { MutableLiveData<RespBody<PaingBean<BloodHistoryNewBean>>>() }
    val recordStudyLive by lazy { MutableLiveData<RespBody<String>>() }
    val deleteDbpCalibrationData by lazy { MutableLiveData<RespBody<Any>>() }

    fun bloodCalibration(list: MutableList<BloodBean>) {
        LogUtils.d("BloodBeans:$list")
        var jsonArray = JsonParser.parseString(Gson().toJson(list)).asJsonArray;
        LogUtils.d("jsonArray:$jsonArray")
        if (jsonArray.size() < 1) {//至少要有一组数据
            CommonToastUtils.showToast("数据不完整，请检查后再提交")
            return
        }
        startHttp {
            var body = apiInterface().calibration(
                retrofits.getRequestBody(
                    jsonArray.toString()
                )
            )
            body.getCodeStatus(calibrationLive, nowData)
        }
    }

    /**
     * 血压校准记录新
     * */
    fun calibrationHistoryNew(id: String, page: Int) {
        val json = JSONObject()
        json.put("id", id)
        json.put("pageNum", page)
        json.put("pageSize", Const.PAGE_SIZE)
        startHttp {
            val body = apiInterface().calibrationHistoryNew(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            body.getCodeStatus(calibrationHistoryLiveNew, nowData)
        }
    }

    /**
     * 校准记录
     */
    fun calibrationHistory(id: Int) {
        val json = JSONObject()
        json.put("id", id)
        startHttp {
            val body = apiInterface().calibrationHistory(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            body.getCodeStatus(calibrationHistoryLive, nowData)
        }
    }

    /**
     * 校准记录
     */
    fun createStudyRecord(higr: Int, low: Int) {
        val json = JSONObject()
        json.put("gbp", higr)
        json.put("dbp", low)
        startHttp {
            val body = apiInterface().createStudyRecord(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            body.getCodeStatus(recordStudyLive, nowData)
        }
    }

    /**
     * 批量删除血压校准记录
     */
    fun deleteDbpCalibrationRequest(list: MutableList<BloodHistoryNewBean>) {
        startHttp {
            var ids = ""
            list.forEachIndexed { _, bloodSugarNewbean ->
                ids += "${bloodSugarNewbean.id},"
            }
            val deleteDbpCalibration = apiInterface().deleteDbpCalibration(ids)
            deleteDbpCalibration.getCodeStatus(deleteDbpCalibrationData, nowData)
        }
    }
}