package com.zxhl.cms.googlepay

import android.view.View
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?.setLoading(getString(R.string.wait_ing2))
            mLoading?.show()
            googlePay?.googlePay("box_text_0001_1cent", "6", object : GooglePayCallBack {
                override fun onPaySuccess(purchase: GooglePayResult, googleJson: String) {
                    showToast("success" + purchase.orderId)
                    mLoading?.setResult(true, "Pay for 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}")
//        }
//    }

}