package com.zj365.mime.vm

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.util.CommonUtils
import com.xty.network.model.MainBean
import com.xty.network.model.MyIntegralBean
import com.xty.network.model.RespBody
import com.xty.network.model.ScoreBean
import org.json.JSONObject

class ScoreDetailVm : BaseVm() {
    val scoreLive by lazy { MutableLiveData<RespBody<ScoreBean>>() }
    val getUserIntegralInfoData by lazy { MutableLiveData<RespBody<MutableList<MyIntegralBean>>>() }
    val allData by lazy { MutableLiveData<RespBody<MainBean>>() }
    val addDataOb by lazy { MutableLiveData<RespBody<Any>>() }

    fun getScore(pageNum: Int, pageSize: Int = 20) {
        startHttp {
            val json = JSONObject()
            json.put("pageSize", pageSize)
            json.put("pageNum", pageNum)
            val body = apiInterface().getScore(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(scoreLive, nowData)
        }
    }

    fun getUserIntegralInfoRequest(userId: String) {
        startHttp {
            val body = apiInterface().getUserIntegralInfo(userId)
            body.getCodeStatus(getUserIntegralInfoData, nowData)
        }
    }

    /**
     * 获取所有信息
     */
    fun getAllInfo(id: String) {
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            val body = apiInterface().getMainInfo(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(allData, nowData)
        }
    }

    /**
     * 添加数据接口
     */
    fun addData(
        userId:String?,
        dataType: String?,
        time: String?,
        dataValue: String?,//可以传体重值
        tcStr: String,
        tgStr: String,
        hdlStr: String,
        ldlStr: String,
        height: String//身高
    ) {
        startHttp {
            val json = JSONObject()
            json.put("time", "$time:00")
            json.put("dataType", dataType)
            json.put("dataValue", dataValue)
            json.put("height", height)
            json.put("tc", tcStr)
            json.put("tg", tgStr)
            json.put("hdl", hdlStr)
            json.put("ldl", ldlStr)
            json.put("userId", userId)
            val upType =
                if (TextUtils.equals(CommonUtils.getMyUserId(), userId) || userId.isNullOrEmpty()) {
                    "1"
                } else {
                    "5"
                }
            json.put("upType", upType)
            val response = apiInterface().addHealthData(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(addDataOb, nowData)
        }
    }
}