export const BillingViewModel = (configuration) => {

    return `
    package ${configuration.packageName}.viewmodels

    import android.app.Activity
    import android.content.ContentValues.TAG
    import android.util.Log
    import androidx.lifecycle.ViewModel
    import com.android.billingclient.api.AcknowledgePurchaseParams
    import com.android.billingclient.api.BillingClient
    import com.android.billingclient.api.BillingClient.ProductType
    import com.android.billingclient.api.BillingFlowParams
    import com.android.billingclient.api.BillingResult
    import com.android.billingclient.api.Purchase
    import com.android.billingclient.api.PurchasesUpdatedListener
    import com.android.billingclient.api.QueryPurchasesParams
    import com.android.billingclient.api.SkuDetailsParams
    import ${configuration.packageName}.data.local.AppPreferences
    
    class BillingViewModel(private val pref: AppPreferences) : ViewModel() {
        private lateinit var billingClient: BillingClient
    
        fun initializeBillingClient(
            activity: Activity,
            billingClientStateListener: com.android.billingclient.api.BillingClientStateListener
        ) {
            billingClient = BillingClient.newBuilder(activity)
                .enablePendingPurchases()
                .setListener(purchaseUpdateListener)
                .build()
    
            billingClient.startConnection(object :
                com.android.billingclient.api.BillingClientStateListener {
                override fun onBillingSetupFinished(billingResult: BillingResult) {
                    if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                        billingClientStateListener.onBillingSetupFinished(billingResult)
                    } else {
                        Log.d(TAG, "onBillingSetupFinished: Failed: \${billingResult.debugMessage}")
                        billingClientStateListener.onBillingServiceDisconnected()
                    }
                }
    
                override fun onBillingServiceDisconnected() {
                    Log.d(TAG, "onBillingServiceDisconnected: Failed")
    
                    billingClientStateListener.onBillingServiceDisconnected()
                }
            })
        }
    
        private val purchaseUpdateListener = PurchasesUpdatedListener { billingResult, purchases ->
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
                for (purchase in purchases) {
                    handlePurchase(purchase)
                }
            } else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
                //pref.isPremium = false
                Log.d(TAG, "purchaseUpdateListener: \${billingResult.debugMessage}")
            } else {
                //pref.isPremium = false
                Log.d(TAG, "purchaseUpdateListener: \${billingResult.debugMessage}")
    
            }
        }
    
        private fun handlePurchase(purchase: Purchase) {
            if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
                val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.purchaseToken)
                    .build()
    
                billingClient.acknowledgePurchase(acknowledgePurchaseParams) { billingResult ->
                    //pref.isPremium = billingResult.responseCode == BillingClient.BillingResponseCode.OK
                }
            }
        }
    
        fun querySubscriptionStatus() {
            val skuDetailsParams = QueryPurchasesParams.newBuilder()
                .setProductType(ProductType.SUBS)
                .build()
            billingClient.queryPurchasesAsync(skuDetailsParams) { billingResult: BillingResult, purchases: MutableList<Purchase> ->
                if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                    if (purchases.isNotEmpty()){
                        for (purchase in purchases) {
                            handlePurchase(purchase)
                        }
                    } //else pref.isPremium = false
                } else {
                    Log.d(TAG, "querySubscriptionStatus: \${billingResult.debugMessage}")
                }
            }
    
        }
    
        fun launchBillingFlow(activity: Activity) {
            val skuDetailsParams = SkuDetailsParams.newBuilder()
                .setType(BillingClient.SkuType.SUBS)
                .setSkusList(listOf("in_app_items"))
                .build()
    
            billingClient.querySkuDetailsAsync(skuDetailsParams) { billingResult, skuDetailsList ->
                if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && !skuDetailsList.isNullOrEmpty()) {
                    val skuDetails = skuDetailsList[0]
                    if (skuDetails != null) {
                        val billingFlowParams = BillingFlowParams.newBuilder()
                            .setSkuDetails(skuDetails)
                            .build()
    
                        billingClient.launchBillingFlow(activity, billingFlowParams)
                    }
                } else {
                    Log.d(TAG, "querySkuDetailsAsync: \${billingResult.debugMessage}")
                }
            }
        }
    
        override fun onCleared() {
            super.onCleared()
            billingClient.endConnection()
        }
    }
    
    `
}