package com.abbidot.tracker.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.DeviceBean
import com.abbidot.tracker.bean.DeviceDetailBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/2/16/016.
 * @link
 * @description:设备相关信息
 */
class TrackerInfoViewModel : ViewModel() {

    val getDeviceListLiveData = MutableLiveData<Result<MutableList<DeviceBean>>>()
    val mUnbindTrackerListLiveData = MutableLiveData<Result<MutableList<DeviceBean>>>()
    val mDeviceDetailLiveData = MutableLiveData<Result<DeviceDetailBean>>()
    val mTrackerInfoLiveData = MutableLiveData<Result<MapDeviceBean>>()

    /**
     * 获取设备列表
     * @param type  type=1 是自己的  0是显示授权
     */
    fun getDeviceList(activity: BaseActivity<*>, type: Int) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getDeviceList(MMKVUtil.getString(MMKVKey.UserId), type)
            getDeviceListLiveData.value = result
        }
    }


    fun getDeviceDetail(activity: BaseActivity<*>, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getDeviceDetail(deviceId)
            mDeviceDetailLiveData.value = result
        }
    }


    fun getPetTrackerInfo(activity: BaseActivity<*>, deviceId: String) {
        activity.showLoading(false)
        viewModelScope.launch {
            val result = NetworkApi.getPetTrackerInfo(deviceId)
            mTrackerInfoLiveData.value = result
        }
    }

    /**
     * 获取未绑定的宠物设备列表
     */
    fun getUnbindTrackerList(activity: BaseActivity<*>) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getUnboundDevices(userId)
            mUnbindTrackerListLiveData.value = result
        }
    }
}