package com.zj365.mime.vm

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.xty.base.vm.BaseVm
import com.xty.common.util.CommonToastUtils
import com.zj365.mime.R
import com.xty.network.model.RespBody
import com.xty.network.model.SportDetail
import org.json.JSONObject

class SportDetailVm : BaseVm() {
    var userId = ""
    var id = ""

    val detailLive = MutableLiveData<RespBody<SportDetail>>()
    val statusLive = MutableLiveData<RespBody<String>>()
    val sportFish = MutableLiveData<RespBody<String>>()
    val accessLive = MutableLiveData<RespBody<String>>()

    fun findSportsPlanDetail() {
        startHttp {
            val body = apiInterface().findSportsPlanDetail(id)
            body.getCodeStatus(detailLive, nowData)
        }
    }

    /**
     * 开始运动
     */
    fun udpateSportsStatus(status: Int) {
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            json.put("status", status)
            val body = apiInterface().udpateSportsStatus(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(statusLive, nowData)
        }
    }

    /**
     * 修改运动方案
     *
     * 运动完成
     *
     */
    fun updateSportsPlan(bean: SportDetail) {
        val json = Gson().toJson(bean)
        startHttp {
            val body = apiInterface().updateSportsPlan(retrofits.getRequestBody(json))
            body.getCodeStatus(sportFish, nowData)
        }
    }

    //  "id": 1,
    //  "sportsWay": "运动方式",
    //  "sportsStrength": "运动强度",
    //  "sportsAerobic": "有氧运动",
    //  "duration": "持续时间",
    //  "distance": "距离",
    //  "speedResistance": "速度/阻力",
    //  "calorie": "卡路里",
    //
    //  "finishTime": "2023-09-06 16:00:00"
    //"evaluationResult": "评估结果内容",
    //}
    //提交结果
    fun submitResult(
        sportsWay: String,
        sportsStrength: String,
        sportsAerobic: String,
        duration: String,
        distance: String,
        speedResistance: String,
        calorie: String,
        finishTime: String,
        evaluationResult: String
    ) {
        if (sportsWay.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_1)
            return
        }
        if (sportsStrength.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_2)
            return
        }
        if (sportsAerobic.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_3)
            return
        }
        if (duration.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_4)

            return
        }
        if (distance.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_5)
            return
        }
        if (speedResistance.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_6)
            return
        }
        if (calorie.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_7)
            return
        }
        if (finishTime.isEmpty()) {
            CommonToastUtils.showToast(R.string.input_detail_result_8)
            return
        }
        if (evaluationResult.isEmpty()) {
            CommonToastUtils.showToast("请输入评估内容")
            return
        }
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            json.put("sportsWay", sportsWay)
            json.put("sportsStrength", sportsStrength)
            json.put("sportsAerobic", sportsAerobic)
            json.put("duration", duration)
            json.put("distance", distance)
            json.put("speedResistance", speedResistance)
            json.put("calorie", calorie)
            json.put("finishTime", finishTime)
            json.put("evaluationResult", evaluationResult)
            val body = apiInterface().submitResult(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(accessLive, nowData)
        }
    }
}