package com.zj365.health.vm

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.xty.base.vm.BaseVm
import com.xty.common.util.CommonUtils
import com.zj365.health.R
import com.xty.network.model.BindDeviceListBean
import com.xty.network.model.BodyFatUserBean
import com.xty.network.model.RespBody
import org.json.JSONObject

class BindDeviceListVm : BaseVm() {
    val list by lazy { MutableLiveData<RespBody<MutableList<BindDeviceListBean>>>() }
    val unbindLive by lazy { MutableLiveData<RespBody<String>>() }
    val saveSuccess by lazy { MutableLiveData<RespBody<Any>>() }
    val bindDev by lazy { MutableLiveData<RespBody<String>>() }
    val userInfo by lazy { MutableLiveData<RespBody<BodyFatUserBean>>() }
    val boundDevices  by lazy{MutableLiveData<RespBody<MutableList<BindDeviceListBean>>>()}

    /**
     * 设备类型1，血压计，2，血糖仪.3体脂秤
     */
    fun getBindList(type:Int){
        startHttp {
            var json=JSONObject()
            json.put("deviceType",type)
            var response =apiInterface().getBindDevList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(boundDevices,nowData)
        }
    }
    fun saveWeight(weight:Double,resistance:Double,sn:String,mac:String){
        startHttp {
            val json = JSONObject()
            json.put("userId", CommonUtils.getMyUserId())
            json.put("weight", weight)
            json.put("resistance", resistance)
            json.put("sn", sn)
            json.put("mac",mac)
            var response = apiInterface().savePbfRecord(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(saveSuccess, nowData)
        }
    }
    fun bindDev(sn: String, mac: String) {
//        if (sn.isNullOrEmpty()) {
//            ToastUtils.show("设备SN号获取失败，请重试")
//            return
//        }
        startHttp {
            var json = JSONObject()
            json.put("sn", sn)
            json.put("mac", mac)
            var response = apiInterface().bindDevSn(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(bindDev, nowData)
        }
    }

    /**
     * 设备类型1，血压计，2，血糖仪.3体脂秤
     */
    fun getBindList(type: Int, userId: String?) {
        startHttp {
            val json = JSONObject()
            json.put("deviceType", type)
            if (!TextUtils.isEmpty(userId)) {
                json.put("userId", userId)
            }
            val response = apiInterface().getBindDevList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(list, nowData)
        }
    }

    fun unBind(sn: String?, mac:String?,userId: String = "") {
        sn?.let {
            startHttp {
                var json = JSONObject()
                json.put("sn", it)
                json.put("userId", userId)
                json.put("mac",mac)
                var response = apiInterface().unBindDevSn(retrofits.getRequestBody(json.toString()))
                response.getCodeStatus(unbindLive, nowData)
            }
        }
    }

    fun saveUserInfo(birthday: String, birthdayType: Int, sex: Int, id: String, height: String) {

        startHttp {
            var json = JSONObject()
            json.put("birthday", birthday)
            json.put("birthdayType", birthdayType)
            json.put("sex", sex)
            json.put("id", id)
            json.put("height", height)
            var response = apiInterface().saveUserInfo(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(saveSuccess, nowData)
        }
    }

    fun getUserInfo(userId: String) {
        startHttp {
            var response = apiInterface().getUserInfo(userId)
            response.getCodeStatus(userInfo, nowData)
        }
    }
}