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.rsa.RSA
import com.abbidot.tracker.bean.CreditCardBean
import com.abbidot.tracker.bean.PayResultBean
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>>()

    /**
     * Stripe支付创建消费用户
     */
    fun createCustomer(card: CreditCardBean) {
//        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 b =
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAofuJbnkwyF7SWg3eRadSYT+ttfHN+CUmCzH0WW1EWTUQOG+01Nv8ux+UJUlxbZA2X7959Et/+VKE03+4OY0/26dlgkROMlXCUnzsucuaoHW+9whGi6xklRZrDLi22QBae/8XV1YDEU2vfYXtcvwf66J0t+bRIhGytQUZvUrL7OEaAHvwJU0QwCy3wQGWcCgSkPpDruIe4ar19iZMleE+BdPGIfKfVdPvWFls63g1eMFECb8F9zN6ydGTCvlqvx/4gfDuFQnF+JuTSre8euSpTRTQn/Q0s3K6KiROVcAYCqDW6cKxNUr2ha1C9ZFxMws9C0qhx5wYzfSlIjFthPC1qwIDAQAB"

            val rsaKey = RSA.encrypt(cardJso, b)
            LogUtil.e(cardJso)
            val result = NetworkApi.createCustomer(
                rsaKey, userId, userName, email
            )
            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
        }
    }
}