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.bean.CommonDataBean
import com.abbidot.tracker.bean.OrderBean
import com.abbidot.tracker.bean.PackageBean
import com.abbidot.tracker.bean.PackageTypeBean
import com.abbidot.tracker.bean.SubscriptionsOrderBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2024/9/4/004.
 * @link
 * @description:
 */
class SubscriptionManageViewModel : ViewModel() {

    val mSubscriptionsOrderLiveData = MutableLiveData<Result<MutableList<SubscriptionsOrderBean>>>()
    val mPackageTypeLiveData = MutableLiveData<Result<MutableList<PackageTypeBean>>>()
    val mPackageListLiveData = MutableLiveData<Result<MutableList<PackageBean>>>()
    val mCommitRefundListLiveData = MutableLiveData<Result<OrderBean>>()
    val mUpdateStatusListLiveData = MutableLiveData<Result<String>>()
    val mSubscriptionsHistoryLiveData =
        MutableLiveData<Result<MutableList<SubscriptionsOrderBean>>>()
    val mReactivationFeeLiveData = MutableLiveData<Result<CommonDataBean>>()
    val mRefundLiveData = MutableLiveData<Result<String>>()
    val mRegionalTaxRateLiveData = MutableLiveData<Result<CommonDataBean>>()

    fun getSubscriptionsOrder() {
        viewModelScope.launch {
            val useId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getSubscriptionsOrder(useId)
            mSubscriptionsOrderLiveData.value = result
        }
    }

    fun getPackageType() {
        viewModelScope.launch {
            val result = NetworkApi.getPackageType()
            mPackageTypeLiveData.value = result
        }
    }

    fun getPackageList(planTypeName: String) {
        viewModelScope.launch {
            val result = NetworkApi.getPackageList(planTypeName)
            mPackageListLiveData.value = result
        }
    }

    fun getSubscriptionsHistory(deviceId: String) {
        viewModelScope.launch {
            val useId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getSubscriptionsHistory(useId, deviceId)
            mSubscriptionsHistoryLiveData.value = result
        }
    }

    fun getHistoryIncludeRenew(subscriptionId: String) {
        viewModelScope.launch {
            val result = NetworkApi.getHistoryIncludeRenew(subscriptionId)
            mSubscriptionsHistoryLiveData.value = result
        }
    }

    fun commitRefund(
        orderId: String,
        chargeId: String,
        amount: Int,
        subscriptionId: String,
        planName: String,
        refundDescription: String,
        deviceId: String,
        deviceOutId: String,
        payType: Int
    ) {
        viewModelScope.launch {
            val result = NetworkApi.commitRefund(
                "USD",
                orderId,
                chargeId,
                amount,
                subscriptionId,
                planName,
                refundDescription,
                deviceId,
                deviceOutId,
                payType
            )
            mCommitRefundListLiveData.value = result
        }
    }

    /**
     * 退款
     */
    fun refund(orderNum: String) {
        viewModelScope.launch {
            val result = NetworkApi.refund(orderNum)
            mRefundLiveData.value = result
        }
    }

    /**
     * 更新订阅状态 状态 0是取消 1使其变为正常
     */
    fun updateSubscriptionStatus(subscriptionId: String, status: Int) {
        viewModelScope.launch {
            val result = NetworkApi.updateSubscriptionStatus(subscriptionId, status)
            mUpdateStatusListLiveData.value = result
        }
    }

    fun getReactivationFee(orderId: String) {
        viewModelScope.launch {
            val result = NetworkApi.getReactivationFee(orderId)
            mReactivationFeeLiveData.value = result
        }
    }

    /**
     * 获取具体国家地区税率
     */
    fun getTaxRateByRegionName(countryCode: String, regionName: String) {
        viewModelScope.launch {
            val result = NetworkApi.getTaxRateByRegionName(countryCode, regionName)
            mRegionalTaxRateLiveData.value = result
        }
    }
}