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 ReportVm : BaseVm() {
    val shareLive by lazy { MutableLiveData<RespBody<Any>>() }
    val infoLive by lazy { MutableLiveData<RespBody<ReportBean>>() }
    val avgLive by lazy { MutableLiveData<RespBody<ReportAvgBean>>() }
    val analysisLive by lazy { MutableLiveData<RespBody<AnalysisBean>>() }
    val exceptionUricAcidData by lazy { MutableLiveData<RespBody<UricAcidExcpBean>>() }

    val sleepLive by lazy { MutableLiveData<SleepBean>() } //睡眠数据

    enum class ReportInfoType(val reportType: Int) {
        Day(1),//日报
        Week(2),//周报
        Month(3),//月报
        Year(4),//年报
        Hour(5)//24小时
    }

    //1 日报、2 周报、3 月报、4 年报
    fun getReportInfo(type: ReportInfoType, id: String = "") {
        startHttp {
            val json = JSONObject()
            json.put("type", type.reportType)
            json.put("id", id)
            val response = apiInterface().getReportInfo(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(infoLive, nowData)
        }
    }

    /**
     * 24小时健康报告
     */
    fun get24HourReportInfo(id: String = "", isD: Boolean) {
        if (isD) {
            startHttp {
                val json = JSONObject()
                val response = apiInterface().find24HourDemonstrate(
                    retrofits.getRequestBody(
                        json.toString()
                    )
                )
                response.getCodeStatus(infoLive, nowData)
            }
        } else {
            startHttp {
                val json = JSONObject()
                json.put("id", id)
                val response = apiInterface().get24HourReportInfo(
                    retrofits.getRequestBody(
                        json.toString()
                    )
                )
                response.getCodeStatus(infoLive, nowData)
            }
        }
    }

    fun getWatchAbnormalDataUricAcid(id: String, type: String, date: String, signType: String) {
        var endDate: String? = ""
        var date = date
        if (date.contains("至")) {
            val split = date.split("至")
            val start = split[0]
            val end = split[1]
            date = start
            endDate = end
        }
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            json.put("type", type)
            if (type == "1") {  //日报
                json.put("date", date.trim())
                json.put("endDate", "")
            } else {
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
            }
            json.put("signType", signType)
            val response = apiInterface().getWatchAbnormalDataUricAcid(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            response.getCodeStatus(exceptionUricAcidData, nowData)
        }
    }

    //1 日报、2 周报、3 月报、4 年报
    fun getReportInfoNeW(
        type: ReportInfoType,
        date: String,
        id: String = "",
        isDemonstrate: Boolean
    ) {
        if (isDemonstrate) {
            startHttp {
                val json = JSONObject()
                json.put("type", type.reportType)
                json.put("signType", 8)//体征类型1，血压，2，心率，3，呼吸率，4，体温，5，血氧，6，血糖，7，睡眠,8,健康报告
                val response = apiInterface().findDemonstratereport(
                    retrofits.getRequestBody(
                        json.toString()
                    )
                )
                response.getCodeStatus(infoLive, nowData)
            }

        } else {
            var endDate: String? = ""
            var date = date
            if (date.contains("至")) {
                val split = date?.split("至")
                val start = split?.get(0)
                val end = split?.get(1)
                date = start!!
                endDate = end
            }
            startHttp {
                val json = JSONObject()
                json.put("type", type.reportType)
                json.put("date", date.trim())
                json.put("id", id)
                json.put("endDate", endDate?.trim())
                val response = apiInterface().getReportInfo(
                    retrofits.getRequestBody(
                        json.toString()
                    )
                )
                response.getCodeStatus(infoLive, nowData)
            }
        }
    }

    /**
     * (日，周，月，年）分享调佣接口)
     */
    fun shareRecordRequest() {
        startHttp {
            val shareRecord = apiInterface().shareRecord()
            shareRecord.getCodeStatus(shareLive, nowData)
        }
    }

    //1 日报、2 周报、3 月报、4 年报
    fun getReportAvg(type: Int, id: String = "") {
        startHttp(false) {
            val json = JSONObject()
            json.put("type", type)
            json.put("id", id)
            val response = apiInterface().getReportAvg(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            response.getCodeStatus(avgLive, nowData)
        }
    }

    fun getReportAvgNew(type: Int, date: String, id: String = "", isDemonstrate: Boolean) {
        if (isDemonstrate) {
            startHttp(false) {
                val json = JSONObject()
                json.put("type", type)
                val response = apiInterface().findPercentageDemonstrate(
                    retrofits.getRequestBody(
                        json.toString()
                    )
                )
                response.getCodeStatus(avgLive, nowData)
            }

        } else {
            var endDate: String? = ""
            var date = date
            if (date.contains("至")) {
                val split = date.split("至")
                val start = split[0]
                val end = split[1]
                date = start
                endDate = end
            }
            startHttp(false) {
                val json = JSONObject()
                json.put("type", type)
                json.put("id", id)
                json.put("date", date.trim())
                json.put("endDate", endDate?.trim())
                val response = apiInterface().getReportAvgNew(
                    retrofits.getRequestBody(
                        json.toString()
                    )
                )
                response.getCodeStatus(avgLive, nowData)
            }
        }
    }

    //
    fun getAnalyse(id: String) {
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            val response = apiInterface().getAnalyse(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            response.getCodeStatus(analysisLive, nowData)
        }
    }

    /**
     * 最后一条数据
     */
    suspend fun getSleepLastTata(id: String): SleepBean? {
        val json = JSONObject()
        json.put("id", id)
        val sleep = apiInterface().getTodaySleep(
            retrofits.getRequestBody(
                json.toString()
            )
        )
        return if (sleep.code == 0) {
            sleep.data
        } else {
            null
        }
    }

    val functionLiveData by lazy { MutableLiveData<RespBody<MutableList<DeviceFunctionBean>>>() }
    fun getDeviceFunctionList(id:String){
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            val body = apiInterface().getDeviceFunctionList(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(functionLiveData, nowData)
        }
    }
}