package com.abbidot.tracker.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.ActiveTimeBean
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.MessageBean
import com.abbidot.tracker.bean.SleepTimeBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/3/16/016.
 * @link
 * @description:
 */
class DataDetailViewModel : ViewModel() {

    val mActiveDetailLiveData = MutableLiveData<Result<ActiveTimeBean>>()
    val mSleepDetailLiveData = MutableLiveData<Result<SleepTimeBean>>()
    val mHistoryDetailLive = MutableLiveData<Result<MutableList<HistoryDataBean>>>()
    val mHistoryFenceEvents = MutableLiveData<Result<MutableList<MessageBean>>>()
    val mSetGoalLiveData = MutableLiveData<Result<String>>()
    val mPetActivityInfoLiveData = MutableLiveData<Result<ActiveTimeBean>>()

    /**
     * 运动时间详情
     */
    fun getActiveTimeByDay(
        activity: BaseActivity<*>, deviceId: String, dayTime: String, petId: String
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getActiveTimeByDay(deviceId, dayTime, petId)
            mActiveDetailLiveData.value = result
        }
    }

    /**
     * 获取SleepTime数据
     */
    fun getSleepTimeByDay(
        activity: BaseActivity<*>, deviceId: String, dayTime: String, petId: String
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getSleepTimeByDay(deviceId, dayTime, petId)
            mSleepDetailLiveData.value = result
        }
    }

    fun getHistoryByDay(activity: BaseActivity<*>, deviceId: String, fromTime: Long, toTime: Long) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getHistoryByDay(deviceId, fromTime, toTime)
            mHistoryDetailLive.value = result
        }
    }

    fun getHistoryFenceList(
        activity: BaseActivity<*>, deviceId: String, fromTime: Long, toTime: Long
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getHistoryFenceList(deviceId, fromTime, toTime)
            mHistoryFenceEvents.value = result
        }
    }

    /**
     *设置运动目标
     * @param goalTime min
     */
    fun setGoal(activity: BaseActivity<*>, deviceId: String, goalTime: Int) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.setGoal(deviceId, goalTime)
            mSetGoalLiveData.value = result
        }
    }

    /**
     * 获取宠物活动统计
     */
    fun getPetActivityDetailInfo(activity: BaseActivity<*>, petId: String, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getPetActivityDetailInfo(petId, deviceId)
            mPetActivityInfoLiveData.value = result
        }
    }
}