package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.network.model.*
import org.json.JSONObject


class BloodFatCompareVm : BaseVm() {

    val dataLive by lazy { MutableLiveData<RespBody< BloodLipidDataBean>>() }
    val setting by lazy { MutableLiveData<RespBody<List<InfoSetting>>>()  }

    val lastData by lazy {MutableLiveData<RespBody<XlBean.Model>>()   }

    /**
     * id
     */
    fun getBloodLipidData(type: Int,time:String?,comTime:String?,id:String) {
        var endDate:String? = ""
        var date = time
        var  comTimeEnd:String? = ""
        var  comDateTime = comTime
        if (type == 2){
            val split = date?.split("至")
            val end  = split?.get(0)?.trim()
            val start= split?.get(1)?.trim()
            val splitCom = comDateTime?.split("至")
            val comEnd= splitCom?.get(0)?.trim()
            val comStart  = splitCom?.get(1)?.trim()
            date = end
            endDate = start
            comTimeEnd =  comStart
            comDateTime =comEnd
        }
        startHttp {
            getFindNow("$type" , date,endDate,comDateTime,comTimeEnd,id)
        }
    }

    fun getSetting(type:Int){
        startHttp {
            var value=apiInterface().getWatchSetting(type)
            value.getCodeStatus(setting,nowData)
        }
    }

//    fun getLastData(id:String){
//        startHttp {
//            var json=JSONObject()
//            json.put("id",id)
//          var value=  apiInterface().getLastXl(retrofits.getRequestBody(json.toString()))
//          value.getCodeStatus(lastData,nowData)
//        }
//    }

    fun <T> setMapValue(map: MutableMap<Int, T>?, data: T?, type: Int): MutableMap<Int, T>? {
        if (data == null) {
            return null
        } else {  //当得到的值不是空的时候
            var mapNew = mutableMapOf<Int, T>()
            if (map == null) {
                mapNew[type] = data
                return mapNew
            } else {
                map[type] = data
                return map
            }
        }
    }
    //获取当前曲线的信息
    private suspend fun getFindNow(type: String,time:String?,endTime:String?,comTime:String?,comEndTime:String?,id:String?) {

        var json= JSONObject()
        json.put("type",type)
        json.put("date",time)
//         json.put("date","2023-07-02")

        json.put("endDate",endTime)
        json.put("compareDate",comTime)
//        json.put("compareDate","2023-07-01")
        json.put("compareEndDate",comEndTime)

        json.put("id",id)

        try {
            var body = apiInterface().findCompareDataBloodLipid(retrofits.getRequestBody(json.toString()))
            return body.getCodeStatus(dataLive,nowData)
        }catch (e:Exception){
            e.printStackTrace()
        }

    }
}