package com.zj365.health.vm

import android.content.Context
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.CommonUtils
import com.zj365.health.R
import com.zj365.health.model.ChildInfo
import com.zj365.health.model.MyCompareBean
import com.xty.network.model.BodyInfo
import com.xty.network.model.CompareBean
import com.xty.network.model.PaingBean
import com.xty.network.model.RespBody
import org.json.JSONObject

class BodyManagerVm : BaseVm() {
    val bodyListLive = MutableLiveData<RespBody<PaingBean<BodyInfo>>>()
    val compareLiveData = MutableLiveData<RespBody<CompareBean>>()
    val deleteLive = MutableLiveData<RespBody<String>>()

    val addLive = MutableLiveData<RespBody<String>>()

    //数据单位
    var infoUnit = listOf("cm", "kg", "%", "%", "", "%", "", "", "", "kg", "")

    //对比数据
    var compareInfo = mutableListOf(
        MyCompareBean("身高"),
        MyCompareBean("体重"),
        MyCompareBean("身体水分"),
        MyCompareBean("体脂肪"),
        MyCompareBean("身体质量"),
        MyCompareBean("腰臀脂肪"),
        MyCompareBean("基础代谢"),
        MyCompareBean("脂肪控制", data = "-", compareData = "-"),
        MyCompareBean("肌肉控制", data = "-", compareData = "-"),
        MyCompareBean("目标体重"),
        MyCompareBean("评分", data = "-", compareData = "-")
    )


    //身高信息
    val infoList = mutableListOf(
        ChildInfo("身高"),
        ChildInfo("体重"),
        ChildInfo("身体水分"),
        ChildInfo("体脂肪"),
        ChildInfo("身体质量"),
        ChildInfo("腰臀脂肪"),
        ChildInfo("基础代谢"),
        ChildInfo("脂肪控制"),
        ChildInfo("肌肉控制"),
        ChildInfo("目标体重"),
        ChildInfo("评分"),
        ChildInfo("", values = "")
    )


    var orderSort = 1  //1,按上传时间倒序，2，按测量时间倒序
    fun bodyInfoList(page: Int, year: String, month: String, userId: String?) {
        startHttp {
            var json = JSONObject()
            json.put("userId", userId)
            json.put("year", year)
            json.put("month", month)
            json.put("orderSort", orderSort)
            json.put("pageNum", page)
            json.put("pageSize", 20)
            var body = apiInterface().bodyInfoList(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(bodyListLive, nowData)
        }
    }

    /**
     * 数据对比
     */
    fun findComparData(data: String, compareData: String, userId: String?) {
        startHttp {
            var json = JSONObject()
            json.put("id", userId)
            json.put("date", data)
            json.put("compareDate", compareData)
            var body = apiInterface().findComparData(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(compareLiveData, nowData)
        }
    }

    /**
     * 添加身体数据
     */
    fun addBodyInfo(
        id: Int?,
        time: String,
        height: String,
        weight: String,
        bmi: String,
        whf: String,
        mc: String,
        bm: String,
        bf: String,
        basicM: String,
        fatC: String,
        tweight: String,
        scoreS: String,
        context: Context,
        userId: String?
    ) {

        if (time.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_1)
            return
        }
        if (height.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_2)
            return
        }
        if (weight.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_3)
            return
        }
        if (bmi.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_4)
            return
        }
        if (whf.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_5)
            return
        }
        if (mc.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_6)
            return
        }
        if (bm.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_7)
            return
        }
        if (bf.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_8)
            return
        }
        if (basicM.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_9)
            return
        }
        if (fatC.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_10)
            return
        }

        if (tweight.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_11)
            return
        }

        if (scoreS.isEmpty()) {
            CommonToastUtils.showToast(R.string.hint_body_12)
            return
        }

        //  binding.mV1.setValues(it.time?:"")
        //            binding.mV4.setValues(it.bm?:"",it.isChange)
        //            binding.mV5.setValues(it.bf?:"",it.isChange)
        //            binding.mV6.setValues(it.bmi?:"",it.isChange)
        //            binding.mV7.setValues(it.whf?:"",it.isChange)
        //            binding.mV8.setValues(it.basicM?:"",it.isChange)
        //            binding.mV9.setValues(it.fatC?:"",it.isChange)
        //            binding.mV10.setValues(it.mc?:"",it.isChange)
        //            binding.mV11.setValues(it.tweight?:"",it.isChange)
        //            binding.mV12.setValues(it.scoreS?:"",it.isChange)


        startHttp {
            val json = JSONObject()
            if (id != null) {
                json.put("id", id)
            }
            json.put("time", time)
            json.put("height", height)
            json.put("weight", weight)
            json.put("bm", bmi)
            json.put("bf", whf)
            json.put("bmi", mc)
            json.put("whf", bm)
            json.put("basicM", bf)
            json.put("fatC", basicM)
            json.put("mc", fatC)
            json.put("tweight", tweight)
            json.put("scoreS", scoreS)
            json.put("userId", userId)
            val body = if (id != null) {
                apiInterface().editBodyInfo(retrofits.getRequestBody(json.toString()))
            } else {
                val upType =
                    if (TextUtils.equals(CommonUtils.getMyUserId(), userId) || userId.isNullOrEmpty()) {
                        "1"
                    } else {
                        "5"
                    }
                json.put("upType", upType)
                apiInterface().addBodyInfo(retrofits.getRequestBody(json.toString()))
            }
            body.getCodeStatus(addLive, nowData)
        }
    }

    /**
     * 数据删除
     */
    fun deleteBodyInfo(id: String) {
        startHttp {
            var body = apiInterface().deleteBodyInfo(id)
            body.getCodeStatus(deleteLive, nowData)
        }
    }
}