package com.zxhl.cms.googlepay

import android.util.Log
import android.view.View
import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient.SkuType
import com.ishin.google.bean.GooglePayResult
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.widget.LoadingDialog
import kotlinx.android.synthetic.main.activity_google_pay.*


class GooglePayActivity : BaseActivity() {
    private val TAG = "GooglePayActivity"
//    private val purchasesUpdatedListener =
//        PurchasesUpdatedListener { billingResult, purchases ->
//            // To be implemented in a later section.
//            Log.d(TAG, "purchasesUpdatedListener" + billingResult.responseCode)
//            Log.d(TAG, "purchasesUpdatedListener" + purchases?.size)
//        }
//
//    private var billingClient = BillingClient.newBuilder(AppContext.get())
//        .setListener(purchasesUpdatedListener)
//        .enablePendingPurchases()
//        .build()

    override fun onClick(v: View?) {

    }

    var googlePay: GooglePayHelper? = null
    override fun layoutID(): Int {
        return R.layout.activity_google_pay
    }
    private var mLoading: LoadingDialog? = null
    override fun init() {
        mLoading = LoadingDialog.getLoadingDialog(
            this,
            getString(R.string.wait_ing2),
            false,
            false
        )
        id_btn_google_pay.setOnClickListener {
            mLoading?.show()
            googlePay?.googlePay("0.1", object : GooglePayCallBack {
                override fun onPaySuccess(purchase: GooglePayResult) {
                    showToast("success" + purchase.orderId)
                    mLoading?.setResult(true, "pay success", 1000)
                }

                override fun onCancel() {
                    showToast("onCancel")
                    mLoading?.setResult(true, "pay cancel", 1000)
                }

                override fun onError(error: String) {
                    showToast("onError$error")
                    mLoading?.setResult(true, "pay error", 1000)
                }
            })
        }
//        startConnection()
        googlePay= GooglePayHelper(this)
        googlePay?.init()
    }

    override fun onDestroy() {
        super.onDestroy()
        googlePay?.release()
    }
//
//
//    fun startConnection() {
//        if(billingClient.isReady){
//
//        }
//        billingClient.startConnection(object : BillingClientStateListener {
//            override fun onBillingSetupFinished(billingResult: BillingResult) {
//                if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
//                    // The BillingClient is ready. You can query purchases here.
//                    // BillingClient准备好了。你可以在这里查询购买情况。
//                    //querySkuDetails()
//                    querySkuDetails()
//                }
//                Log.d(TAG, billingResult.toString())
//            }
//
//            override fun onBillingServiceDisconnected() {
//                // Try to restart the connection on the next request to
//                // 尝试在下一次请求时重新启动连接
//                // Google Play by calling the startConnection() method.
//                // 谷歌通过调用startConnection()方法来播放。
//                startConnection()
//                Log.d(TAG, "onBillingServiceDisconnected")
//            }
//        })
//    }
//
//    fun querySkuDetails() {
//        val skuList: MutableList<String> = ArrayList()
//        skuList.add("premium_upgrade")
//        skuList.add("gas")
//        val params = SkuDetailsParams.newBuilder()
//        params.setSkusList(skuList).setType(SkuType.INAPP)
//        billingClient.querySkuDetailsAsync(
//            params.build()
//        ) { billingResult, skuDetailsList ->
//            // Process the result.
//            Log.d(TAG, "billingResult ${billingResult.responseCode}")
//            Log.d(TAG, "skuDetailsList ${skuDetailsList?.size}")
//        }
//    }

}