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.DeviceDetailBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/2/26/026.
 * @link
 * @description:设备设置相关
 */
class TrackerSetViewModel : ViewModel() {
    val mAddBindDeviceLiveData = MutableLiveData<Result<DeviceDetailBean>>()
    val mTurnoffLiveData = MutableLiveData<Result<String>>()
    val mReportIntervalLiveData = MutableLiveData<Result<String>>()
    val mSetLiveTimeLiveData = MutableLiveData<Result<String>>()
    val mUnbindDeviceLiveData = MutableLiveData<Result<String>>()
    val mAssignOtherTrackerLiveData = MutableLiveData<Result<String>>()

    /**
     * 关闭设备
     */
    fun turnOff(deviceInfoId: String, turnOff: Int = 0) {
//        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.turnoff(deviceInfoId, turnOff)
            mTurnoffLiveData.value = result
        }
    }

    /**
     * 设置设备上报下发时间
     */
    fun reportInterval(
        activity: BaseActivity<*>, deviceInfoId: String, gnssInterval: Int, modeType: Int
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.reportInterval(deviceInfoId, gnssInterval, modeType)
            mReportIntervalLiveData.value = result
        }
    }

    /**
     * 设置设备的直播时间
     */
    fun setLiveTime(activity: BaseActivity<*>, deviceId: String, liveTime: Int) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.setLiveTime(deviceId, liveTime)
            mSetLiveTimeLiveData.value = result
        }
    }

    /**
     * 解绑删除设备
     */
    fun unbindDevice(activity: BaseActivity<*>, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.unbindDevice(userId, deviceId)
            mUnbindDeviceLiveData.value = result
        }
    }

    /**
     * 绑定设备
     */
    fun bindDevice(activity: BaseActivity<*>, deviceOutId: String) {
        activity.showLoading(true)
        val userId = MMKVUtil.getString(MMKVKey.UserId)
        viewModelScope.launch {
            val result = NetworkApi.bindDevice(userId, deviceOutId)
            mAddBindDeviceLiveData.value = result
        }
    }

    /**
     *  关联其他宠物/设备
     */
    fun assignOtherPetTracker(
        activity: BaseActivity<*>, petId: String, originDeviceId: String, assignDeviceId: String
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result =
                NetworkApi.assignOtherPetTracker(userId, petId, originDeviceId, assignDeviceId)
            mAssignOtherTrackerLiveData.value = result
        }
    }
}