package com.abbidot.tracker.deprecated.ui.activity.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.OrderBean
import com.abbidot.tracker.bean.RechargeDeviceBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/4/9/009.
 * @link
 * @description:添加绑定设备
 */
class AddTrackerDeviceViewModel : ViewModel() {

    val mGetMacLiveData = MutableLiveData<Result<String>>()
    val mGetIccIdLiveData = MutableLiveData<Result<RechargeDeviceBean>>()
    val mActiveDeviceLiveData = MutableLiveData<Result<String>>()
    val mUpdatePayDeviceStateLiveData = MutableLiveData<Result<String>>()
    val mDeviceOrderStatusLiveData = MutableLiveData<Result<OrderBean>>()

    /**
     * 获取设备mac地址
     */
    fun getMacId(activity: BaseActivity<*>, deviceOutId: String) {
        activity.showLoading(true)
        val userId = MMKVUtil.getString(MMKVKey.UserId)
        viewModelScope.launch {
            val result = NetworkApi.getMacId(userId, deviceOutId)
            mGetMacLiveData.value = result
        }
    }

    /**
     * 获取设备iccid
     */
    fun getIccId(activity: BaseActivity<*>, deviceOutId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getIccId(deviceOutId)
            mGetIccIdLiveData.value = result
        }
    }

    /**
     * 上电激活设备，更新状态
     *@param addStatus 设备添加状态,1 为上电激活成功
     */
    fun activeDevice(activity: BaseActivity<*>, deviceId: String, addStatus: Int) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.updateStatus(deviceId, addStatus)
            mActiveDeviceLiveData.value = result
        }
    }

    /**
     * 支付成功后，更新绑定状态
     */
    fun updatePayDeviceState(activity: BaseActivity<*>, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.updatePayDeviceState(deviceId, userId)
            mUpdatePayDeviceStateLiveData.value = result
        }
    }

    /**
     * 获取设备订单状态
     */
    fun getDeviceOrderStatus(activity: BaseActivity<*>, deviceOutId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getDeviceOrderStatus(deviceOutId)
            mDeviceOrderStatusLiveData.value = result
        }
    }
}