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.LogUtil
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.baselibrary.util.Utils
import com.abbidot.baselibrary.util.rsa.RSA
import com.abbidot.tracker.bean.CreditCardBean
import com.abbidot.tracker.bean.PayResultBean
import com.abbidot.tracker.bean.PaymentIDBean
import com.abbidot.tracker.bean.StripeSubscriptionPriceBean
import com.abbidot.tracker.bean.SubscriptionsOrderBean
import com.abbidot.tracker.retrofit2.NetworkApi
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2024/9/11/011.
 * @link
 * @description:
 */
class SubscriptionPayViewModel : ViewModel() {

    val mSubscriptionCustomerLiveData = MutableLiveData<Result<StripeSubscriptionPriceBean>>()
    val mCreateStripeOrderLiveData = MutableLiveData<Result<PayResultBean>>()
    val mCreatePaypalOrderLiveData = MutableLiveData<Result<PayResultBean>>()
    val mPaymentMethodIDLiveData = MutableLiveData<Result<PaymentIDBean>>()
    val mSavePaymentMethodIDLiveData = MutableLiveData<Result<String>>()

    /**
     * Stripe支付创建消费用户
     */
    fun createCustomer(card: CreditCardBean, paymentMethodID: String) {
//        activity.showLoading(true)
        viewModelScope.launch(Dispatchers.IO) {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val userName = MMKVUtil.getString(MMKVKey.UserName)
            val email = MMKVUtil.getString(MMKVKey.Email)

            val dates = card.expirationDate.split("/")
            val cardParamsMap = mutableMapOf<String, String>()
            cardParamsMap["month"] = dates[0]
            cardParamsMap["year"] = dates[1]
            cardParamsMap["cvc"] = card.cvv
            cardParamsMap["cardName"] = card.cardName
            cardParamsMap["cardnum"] = card.cardNumber
            val cardJso = Gson().toJson(cardParamsMap)

            val rsaKey = RSA.encrypt(cardJso, Utils.CARD_RSA_KEY)
            LogUtil.e(cardJso)
            val result = NetworkApi.createCustomer(rsaKey, userId, userName, email, paymentMethodID)
            mSubscriptionCustomerLiveData.postValue(result)
        }
    }

    fun createStripeOrder(subscriptionsOrder: SubscriptionsOrderBean) {
        viewModelScope.launch {
            LogUtil.e("$subscriptionsOrder")
            val result = NetworkApi.createStripeOrder(subscriptionsOrder)
            mCreateStripeOrderLiveData.value = result
        }
    }

    fun createPaypalOrder(subscriptionsOrder: SubscriptionsOrderBean) {
        viewModelScope.launch {
            LogUtil.e("$subscriptionsOrder")
            val result = NetworkApi.createPaypalOrder(subscriptionsOrder)
            mCreatePaypalOrderLiveData.value = result
        }
    }

    fun getUserPaymentMethodID() {
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getUserPaymentMethodID(userId)
            mPaymentMethodIDLiveData.value = result
        }
    }

    fun savePaymentMethodInfo(paymentMethodID: String) {
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.savePaymentMethodInfo(userId, paymentMethodID)
            mSavePaymentMethodIDLiveData.value = result
        }
    }
}