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.PayResultBean
import com.abbidot.tracker.bean.RechargeDeviceBean
import com.abbidot.tracker.bean.SetMealBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/3/26/026.
 * @link
 * @description:开通/关闭自动订阅,自动续费
 */
class AutomaticSubscribeViewModel : ViewModel() {
    val mCancelSubscribeLiveData = MutableLiveData<Result<String>>()
    fun cancelSubscriptions(activity: BaseActivity<*>, rechargeDeviceBean: RechargeDeviceBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.cancelSubscriptions(rechargeDeviceBean.orderId,
                    rechargeDeviceBean.payType, rechargeDeviceBean.subscriptionsId)
            mCancelSubscribeLiveData.value = result
        }
    }

    val mSubscribeLiveData = MutableLiveData<Result<PayResultBean>>()
    fun subscription(activity: BaseActivity<*>, setMealBean: SetMealBean, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId )
            val username = MMKVUtil.getString(MMKVKey.UserName)
            val result = NetworkApi.subscription(setMealBean.name, deviceId,
                    setMealBean.accountFee.toString(), setMealBean.subscriptionFee.toString(),
                    setMealBean.period.toString(), userId, username)
            mSubscribeLiveData.value = result
        }
    }
}