package com.swago.baseswago

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.android.billingclient.api.Purchase
import com.google.protobuf.Api
import com.swago.baseswago.http.SwagoException
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.libpay.PayManager
import com.swago.baseswago.model.BenefitModel
import com.swago.baseswago.model.pay.CoinModel
import com.swago.baseswago.model.pay.OrderModel
import com.swago.baseswago.util.*

/**
 *@date 2021/11/25 19:42
 *description:
 */
class PayVm(application: Application) : BaseViewModel(application) {

    var orderModel: OrderModel? = null

    fun getProductList() {
        requestData2 {
            requestData {
                val data = ApiManager.payApi.getProductList()
                if (data.isNotEmpty()) {
                    data[0].isSelected = true
                }
                PayManager.queryProductPrice(data)
            }

            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }


    fun createOrder(productId: String) {
        requestData2 {
            requestData {
                val data = ApiManager.payApi.createOrder(productId)
                orderModel = data
                SwagoLoading.cancelLoadingDialog()
                UserInfo.getUserInfo()?.let {
                    PayManager.pay(data.google_product_id, it.id, data.order_number)
                }

            }

            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }


    fun payOrderToServer(purchase: Purchase?, payType: Int) {
        requestData2 {
            requestData {
                if (payType == 1 || payType == 4) {
                    purchase?.let {
                        ApiManager.payApi.payOrderToServer(
                            purchase.originalJson,
                            purchase.signature,
                            payType,
                            purchase.accountIdentifiers?.obfuscatedProfileId ?: ""
                        )
                        getCoins()
                        PayManager.consumeOrder(purchase)
                    }
                } else {
                    orderModel?.order_number?.let {
                        ApiManager.payApi.payOrderToServer("", "", payType, it)
                    }

                }
                SwagoLoading.cancelLoadingDialog()
            }

            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }

    val coinLiveData by lazy {
        MutableLiveData<CoinModel>()
    }

    fun getCoins() {
        requestData {
            val data = ApiManager.payApi.getCoins()
            coinLiveData.value = data
            UserInfo.getUserInfo()?.user_coins = data.user_coins
        }
    }


    val benefitLiveData by lazy {
        MutableLiveData<BenefitModel>()
    }

    /**
     * 获取特惠礼包
     */
    fun getBenefit(){
        requestData {
            val data = ApiManager.payApi.getBenefit()
            benefitLiveData.value = data
        }
    }

}