package com.base.superfilemanager.helps.ads

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.blankj.utilcode.util.SPUtils
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdValue
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.OnPaidEventListener
import com.google.android.gms.ads.ResponseInfo
import com.google.android.gms.ads.appopen.AppOpenAd
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import com.base.superfilemanager.R
import com.base.superfilemanager.activity.photocompress.photo.CustomDialog
import com.base.superfilemanager.helps.BaseApplication
import com.base.superfilemanager.helps.ConfigHelper
import com.base.superfilemanager.helps.EventHelper
import org.json.JSONObject
import java.util.Calendar
import java.util.UUID

object AdmobUtils {

    private val mRequest = AdRequest.Builder().build()

    private var openLoadTime = Long.MAX_VALUE
    private var interLoadTime = Long.MAX_VALUE
    private var nativeLoadTime = Long.MAX_VALUE

    private var mOpenAd: AppOpenAd? = null
    private const val typeShow = "Show"

    var adShowCount = 80
        get() {
            return SPUtils.getInstance().getInt("adShowCount", field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put("adShowCount", value)
        }
    var nativeAdShowCount = 40
        get() {
            return SPUtils.getInstance().getInt("nativeAdShowCount", field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put("nativeAdShowCount", value)
        }
    var isNowPlayAd = 0
        get() {
            return SPUtils.getInstance().getInt("isNowPlayAd", field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put("isNowPlayAd", value)
        }

    fun isAdLimit(isNative: Boolean = false) =
        getTodayCount(typeShow, isNative) % 1000 >= getAdShowCount(isNative)

    private fun getAdShowCount(isNative: Boolean) = if (isNative) nativeAdShowCount else adShowCount
    private fun getTodayCount(type: String, isNative: Boolean): Long {
        val last = Calendar.getInstance().apply {
            timeInMillis = SPUtils.getInstance()
                .getLong("${if (isNative) "native" else "other"}AdToday${type}Count", 0)
        }
        return Calendar.getInstance().run {
            if (get(Calendar.DAY_OF_YEAR) == last[Calendar.DAY_OF_YEAR]) {
                last
            } else {
                set(Calendar.MILLISECOND, 0)
                this
            }
        }.timeInMillis
    }

    private fun addTodayCount(type: String, isNative: Boolean) {
        val todayCount = getTodayCount(type, isNative)
        SPUtils.getInstance()
            .put("${if (isNative) "native" else "other"}AdToday${type}Count", todayCount + 1)
    }

    var isBlack = false
        get() {
            return SPUtils.getInstance().getBoolean("isBlack", field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put("isBlack", value, true)
        }

    fun loadAppOpenAd(skip: Boolean = false, onLoad: (() -> Unit)? = null) {
        if (isBlack) {
            EventHelper.event("blacklist_filter")
            onLoad?.invoke()
            return
        }

        if (mOpenAd != null || skip) {
            onLoad?.invoke()
            return
        }
        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("ad_type", "openAd")
        EventHelper.event("ad_pull_start", ext = obj)

        AppOpenAd.load(
            BaseApplication.context,
            ConfigHelper.openAdmobId,
            mRequest,
            object : AppOpenAd.AppOpenAdLoadCallback() {
                override fun onAdLoaded(ad: AppOpenAd) {
                    openLoadTime = System.currentTimeMillis()
                    mOpenAd = ad
                    onLoad?.invoke()
                    pull(ad.responseInfo, "openAd", reqId = reqId)
                    ad.onPaidEventListener = EventOnPaidEventListener(ad)
                    // Log.e("MXL", "OpenonAdLoaded: ")
                }

                override fun onAdFailedToLoad(p0: LoadAdError) {
                    mOpenAd = null
                    onLoad?.invoke()
                    Log.d("glc", "开屏拉取失败：" + p0.message)
                    pull(p0.responseInfo, "openAd", p0.message, reqId = reqId)
                    //Log.e("MXL", "onAdFailedToLoad: " + p0.message)
                    //ToastUtils.showShort("开屏拉取失败" + p0.message)
                }
            })
    }

    fun isOpenAdLoaded() = mOpenAd != null
    fun showAppOpenAd(activity: Activity, skip: Boolean = false, onHidden: (() -> Unit)? = null) {

        if (isBlack) {
            EventHelper.event("blacklist_filter")
            onHidden?.invoke()
            return
        }

//        if (!AdUtils.isShowAd("splash_slot")) {
//            onHidden?.invoke()
//            return
//        }
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
        val obj = JSONObject()
        obj.put("ad_unit", "openAd")
        EventHelper.event("ad_prepare_show", ext = obj)
        if (mOpenAd == null || skip) {
            onHidden?.invoke()
            return
        }
        if (System.currentTimeMillis() - openLoadTime > 1000 * 60 * 60) {
            mOpenAd = null
            loadAppOpenAd()
            onHidden?.invoke()
            val obj = JSONObject()
            obj.put("ad_unit", "openAd")
            EventHelper.event("ad_expire", ext = obj)
            return
        }
        if (mOpenAd != null) {
            var thisMOpenAd = mOpenAd
            mOpenAd = null
            thisMOpenAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdClicked() {
                    click(thisMOpenAd?.responseInfo, "openAd")
                    onAdClick(thisMOpenAd)
                }

                override fun onAdDismissedFullScreenContent() {
                    mOpenAd = null
                    onHidden?.invoke()
                    loadAppOpenAd()
                }

                override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                    mOpenAd = null
                    onHidden?.invoke()
                    loadAppOpenAd()
                    val obj = JSONObject()
                    obj.put("reason", p0.message)
                    obj.put("ad_unit", "openAd")
                    EventHelper.event("ad_show_error", ext = obj)
                }

                override fun onAdShowedFullScreenContent() {
                    show(thisMOpenAd?.responseInfo, "openAd", activity)
                }
            }
            thisMOpenAd?.show(activity)
        } else {
            onHidden?.invoke()
            loadAppOpenAd()
            val obj = JSONObject()
            obj.put("reason", "no_ad")
            obj.put("ad_unit", "openAd")
            EventHelper.event("ad_show_error", ext = obj)
        }
    }

    private var nativeAd: NativeAd? = null

    private var loadingListener: (() -> Unit)? = null

    fun showNativeAd(activity: Activity?, parent: ViewGroup) {
        if (isBlack) {
            EventHelper.event("blacklist_filter")
            return
        }
//        if (!AdUtils.isShowAd("native_slot")) {
//            EventHelper.event("native_strategy_filter")
//            return
//        }
        if (isAdLimit(true)) {
            EventHelper.event("native_is_limit")
            return
        }
        val obj = JSONObject()
        obj.put("ad_unit", "NativeAd")
        EventHelper.event("ad_prepare_show", ext = obj)

        loadingListener = {
            if (System.currentTimeMillis() - nativeLoadTime <= 1000 * 60 * 60) {
                nativeAd?.let {
                    NativeView(parent.context).run {
                        parent.removeAllViews()
                        setNativeAd(it)
                        parent.addView(this)
                        parent.isVisible = true
                        show(nativeAd?.responseInfo, "nativeAd", activity)
                    }
                }
            }
            nativeAd = null
            loadingListener = null
            loadNativeAd()
        }
        if (nativeAd == null) {
            loadNativeAd()
            val obj = JSONObject()
            obj.put("reason", "no_ad")
            obj.put("ad_unit", "nativeAd")
            EventHelper.event("ad_show_error", ext = obj)
        } else {
            loadingListener?.invoke()
        }
    }

    private var isLoading = false

    fun loadNativeAd() {
        if (isBlack) {
            EventHelper.event("blacklist_filter")
            return
        }
        if (isAdLimit()) {
            EventHelper.event("native_limit")
            return
        }
        if (nativeAd != null) {
            return
        }

        if (isLoading) {
            return
        }
        isLoading = true

        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("ad_type", "nativeAd")
        EventHelper.event("ad_pull_start", ext = obj)

        val adLoader = AdLoader.Builder(
            BaseApplication.context, ConfigHelper.nativeAdmobId
        ).forNativeAd {
            nativeLoadTime = System.currentTimeMillis()
            nativeAd = it

            isLoading = false
            loadingListener?.invoke()
            pull(it.responseInfo, "nativeAd", reqId = reqId)
            it.setOnPaidEventListener(EventOnPaidEventListener(it))
        }.withAdListener(object : AdListener() {
            override fun onAdClicked() {
                click(nativeAd?.responseInfo, "nativeAd")
                onAdClick(nativeAd)
            }

            override fun onAdFailedToLoad(p0: LoadAdError) {
                nativeAd = null
                isLoading = false
                pull(p0.responseInfo, "nativeAd", p0.message, reqId = reqId)
                //   Log.e("MXL", "NativeAdFailedToLoad: " + p0.message)
            }
        }).build()
        adLoader.loadAd(mRequest)
    }

    private var interAd: InterstitialAd? = null
    fun isInterLoaded() = interAd != null
    fun loadInterstitialAd(activity: Activity, onLoad: (() -> Unit)? = null) {
        if (isBlack) {
            EventHelper.event("blacklist_filter")
            onLoad?.invoke()
            return
        }
        if (isAdLimit()) {
            EventHelper.event("inter_load_limit")
            onLoad?.invoke()
            return
        }

        if (interAd != null) {
            onLoad?.invoke()
            return
        }
        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("ad_type", "interAd")
        obj.put("from", activity.javaClass.simpleName)
        EventHelper.event("ad_pull_start", ext = obj)

        InterstitialAd.load(
            activity,
            ConfigHelper.interAdmobId,
            mRequest,
            object : InterstitialAdLoadCallback() {
                override fun onAdFailedToLoad(p0: LoadAdError) {
                    Log.d("glc", "广告拉取失败：" + p0.message)
                    interAd = null
                    onLoad?.invoke()
                    pull(p0.responseInfo, "interAd", p0.message, reqId = reqId)
                    //  Log.e("MXL", "InterAdFailedToLoad: " + p0.message)
                }

                override fun onAdLoaded(ad: InterstitialAd) {
                    interAd = ad
                    onLoad?.invoke()
                    interLoadTime = System.currentTimeMillis()
                    pull(ad.responseInfo, "interAd", reqId = reqId)
                    ad.onPaidEventListener = EventOnPaidEventListener(ad)
                    // Log.e("MXL", "InteronAdLoaded: ")
                }
            })
    }

    fun showInterstitialAd(
        activity: Activity,
        skip: Boolean = false,
        isASkip: Boolean = true,
        isFirst: Boolean = true,
        onHidden: (() -> Unit)? = null
    ) {
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
        if (isBlack) {
            EventHelper.event("blacklist_filter")
            onHidden?.invoke()
            return
        }
//        if (!AdUtils.isShowAd("interstitial_slot")) {
//            EventHelper.event("inter_strategy_filter")
//            return
//        }
        if (isAdLimit()) {
            EventHelper.event("inter_limit_filter")
            onHidden?.invoke()
            return
        }
        val obj = JSONObject()
        obj.put("ad_unit", "interAd")
        if(isASkip){
            EventHelper.event("ad_prepare_show", ext = obj)
        }
//        if (!AdUtils.isShowAd("interstitial_slot")) {
//            EventHelper.event("inter_strategy_filter")
//            onHidden?.invoke()
//            return
//        }
        if (skip) {
            onHidden?.invoke()
            return
        }
        if (System.currentTimeMillis() - interLoadTime > 1000 * 60 * 60) {
            interAd = null
            val obj = JSONObject()
            obj.put("ad_unit", "interAd")
            EventHelper.event("ad_expire", ext = obj)
            loadInterstitialAd(activity)
            onHidden?.invoke()
            return
        }
        if (interAd != null) {
            val thisInterAd = interAd
            interAd = null
            thisInterAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdClicked() {
                    click(thisInterAd?.responseInfo, "interAd")
                    onAdClick(thisInterAd)
                }

                override fun onAdDismissedFullScreenContent() {
                    interAd = null
                    onHidden?.invoke()
                    loadInterstitialAd(activity)
                }

                override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                    interAd = null
                    onHidden?.invoke()
                    loadInterstitialAd(activity)
                    val obj = JSONObject()
                    obj.put("reason", p0.message)
                    obj.put("ad_unit", "interAd")
                    EventHelper.event("ad_show_error", ext = obj)
                }

                override fun onAdShowedFullScreenContent() {
                    show(thisInterAd?.responseInfo, "interAd", activity)
                }
            }
            thisInterAd?.show(activity)

        } else {
            var mDialog: Dialog? = null
            mDialog = CustomDialog(activity, R.layout.dialog_ad_loading)
            mDialog.show()

            loadInterstitialAd(activity) {
                if (isFirst) {
                    EventHelper.event("ad_reload_inter")
                }
                mDialog?.dismiss()
                if (isASkip) {
                    showInterstitialAd(activity, false,false) {
                        onHidden?.invoke()
                    }
                }
            }

            val obj = JSONObject()
            obj.put("reason", "no_ad")
            obj.put("ad_unit", "interAd")
            EventHelper.event("ad_show_error", ext = obj)
            if (!isASkip) {
                mDialog?.dismiss()
                onHidden?.invoke()
            }
        }
    }

    private fun pull(
        responseInfo: ResponseInfo?,
        adUnit: String,
        error: String? = null,
        reqId: String? = null
    ) {
        val obj = JSONObject()
        if (responseInfo != null) {
            val response = responseInfo.adapterResponses.getOrNull(0)
            if (response != null) {
                obj.put("source", response.adSourceName)

                val credentials = mapOf(
                    "placementid" to response.credentials.get("placementid"),
                    "appid" to response.credentials.get("appid"),
                    "pubid" to response.credentials.get("pubid")
                )
                obj.put("credentials", credentials.toString())
            }
            obj.put("session_id", responseInfo.responseId)
        }
        obj.put("networkname", responseInfo?.mediationAdapterClassName)
        obj.put("ad_unit", adUnit)
        obj.put("req_id", reqId)
        if (error == null) {
            obj.put("status", "1")
        } else {
            obj.put("errMsg", error)
            obj.put("status", "2")
        }
        EventHelper.event("ad_pull", ext = obj)
    }

    private fun show(responseInfo: ResponseInfo?, adUnit: String, activity: Activity? = null) {
        addTodayCount(typeShow, adUnit == "nativeAd")
        val response = responseInfo?.adapterResponses?.getOrNull(0)
        val obj = JSONObject()
        obj.put("source", response?.adSourceName)
        obj.put("ad_unit", adUnit)
        obj.put("networkname", responseInfo?.mediationAdapterClassName)
        val credentials = mapOf(
            "placementid" to response?.credentials?.get("placementid"),
            "appid" to response?.credentials?.get("appid"),
            "pubid" to response?.credentials?.get("pubid")
        )
        obj.put("credentials", credentials.toString())
        obj.put("session_id", responseInfo?.responseId)
        obj.put("from", activity?.javaClass?.simpleName)
        EventHelper.event("ad_show", ext = obj)
    }

    private var lastObj: Any? = null
    private var curSingleAdClick = 0

    private fun onAdClick(obj: Any?) {
        if (lastObj == obj) {
            curSingleAdClick++
        } else {
            lastObj = obj
            curSingleAdClick = 1
        }


    }

    private fun click(responseInfo: ResponseInfo?, adUnit: String) {
        // addTodayCount(typeClick, adUnit == "nativeAd")

        val response = responseInfo?.adapterResponses?.getOrNull(0)
        val obj = JSONObject()
        obj.put("source", response?.adSourceName)
        obj.put("ad_unit", adUnit)

        val credentials = mapOf(
            "placementid" to response?.credentials?.get("placementid"),
            "appid" to response?.credentials?.get("appid"),
            "pubid" to response?.credentials?.get("pubid")
        )
        obj.put("credentials", credentials.toString())
        obj.put("session_id", responseInfo?.responseId)
        obj.put("networkname", responseInfo?.mediationAdapterClassName)
        EventHelper.event("ad_click", ext = obj)
    }

    private val taichiPref by lazy {
        BaseApplication.context.getSharedPreferences("TaichiTroasCache", 0)
    }
    private val taichiSharedPreferencesEditor by lazy {
        taichiPref.edit()
    }

    private class EventOnPaidEventListener(private val ad: Any?) : OnPaidEventListener {
        override fun onPaidEvent(adValue: AdValue) {
            val valueMicros = adValue.valueMicros
            val currencyCode = adValue.currencyCode
            val precision = adValue.precisionType

            val obj = JSONObject()
            obj.put("valueMicros", valueMicros)
            obj.put("currencyCode", currencyCode)
            obj.put("precision", precision)

            Firebase.analytics.logEvent("ad_price", Bundle().apply {
                putDouble("valueMicros", valueMicros / 1000000.0)
            })

            val params = Bundle()
            val currentImpressionRevenue = adValue.valueMicros.toDouble() / 1000000.0
            params.putDouble(FirebaseAnalytics.Param.VALUE, currentImpressionRevenue)
            params.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
            val precisionType = when (adValue.precisionType) {
                0 -> "UNKNOWN"
                1 -> "ESTIMATED"
                2 -> "PUBLISHER_PROVIDED"
                3 -> "PRECISE"
                else -> "Invalid"
            }
            params.putString("precisionType", precisionType)
            Firebase.analytics.logEvent("Ad_Impression_Revenue", params)
            val previousTaichiTroasCache = taichiPref.getFloat("TaichiTroasCache", 0f)
            val currentTaichiTroasCache = (previousTaichiTroasCache +
                    currentImpressionRevenue).toFloat()
            if (currentTaichiTroasCache >= 0.01) {//如果超过0.01就触发一次tROAS taichi事件
                val roasbundle = Bundle()
                roasbundle.putDouble(
                    FirebaseAnalytics.Param.VALUE,
                    currentTaichiTroasCache.toDouble()
                )
                roasbundle.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
                Firebase.analytics.logEvent("Total_Ads_Revenue_001", roasbundle)
                taichiSharedPreferencesEditor.putFloat("TaichiTroasCache", 0f)//重新清零，开始计算
            } else {
                taichiSharedPreferencesEditor.putFloat("TaichiTroasCache", currentTaichiTroasCache)
            }
            taichiSharedPreferencesEditor.commit()

            when (ad) {
                is AppOpenAd -> {
                    val adUnitId = ad.adUnitId
                    val loadedAdapterResponseInfo = ad.responseInfo.loadedAdapterResponseInfo
                    val adSourceName = loadedAdapterResponseInfo?.adSourceName
                    val adSourceId = loadedAdapterResponseInfo?.adSourceId
                    val adSourceInstanceName = loadedAdapterResponseInfo?.adSourceInstanceName
                    val adSourceInstanceId = loadedAdapterResponseInfo?.adSourceInstanceId
                    val sessionId = ad.responseInfo.responseId
                    val extras = ad.responseInfo.responseExtras
                    val mediationGroupName = extras.getString("mediation_group_name")
                    val mediationABTestName = extras.getString("mediation_ab_test_name")
                    val mediationABTestVariant = extras.getString("mediation_ab_test_variant")
                    obj.put("adUnitId", adUnitId)
                    obj.put("adSourceName", adSourceName)
                    obj.put("adSourceId", adSourceId)
                    obj.put("adSourceInstanceName", adSourceInstanceName)
                    obj.put("adSourceInstanceId", adSourceInstanceId)
                    obj.put("mediationGroupName", mediationGroupName)
                    obj.put("mediationABTestName", mediationABTestName)
                    obj.put("mediationABTestVariant", mediationABTestVariant)
                    obj.put("session_id", sessionId)
                }

                is InterstitialAd -> {
                    val adUnitId = ad.adUnitId
                    val loadedAdapterResponseInfo = ad.responseInfo.loadedAdapterResponseInfo
                    val adSourceName = loadedAdapterResponseInfo?.adSourceName
                    val adSourceId = loadedAdapterResponseInfo?.adSourceId
                    val adSourceInstanceName = loadedAdapterResponseInfo?.adSourceInstanceName
                    val adSourceInstanceId = loadedAdapterResponseInfo?.adSourceInstanceId
                    val sessionId = ad.responseInfo.responseId
                    val extras = ad.responseInfo.responseExtras
                    val mediationGroupName = extras.getString("mediation_group_name")
                    val mediationABTestName = extras.getString("mediation_ab_test_name")
                    val mediationABTestVariant = extras.getString("mediation_ab_test_variant")
                    obj.put("adUnitId", adUnitId)
                    obj.put("adSourceName", adSourceName)
                    obj.put("adSourceId", adSourceId)
                    obj.put("adSourceInstanceName", adSourceInstanceName)
                    obj.put("adSourceInstanceId", adSourceInstanceId)
                    obj.put("mediationGroupName", mediationGroupName)
                    obj.put("mediationABTestName", mediationABTestName)
                    obj.put("mediationABTestVariant", mediationABTestVariant)
                    obj.put("session_id", sessionId)
                }

                is RewardedAd -> {
                    val loadedAdapterResponseInfo = ad.responseInfo.loadedAdapterResponseInfo
                    val adSourceName = loadedAdapterResponseInfo?.adSourceName
                    val adSourceId = loadedAdapterResponseInfo?.adSourceId
                    val adSourceInstanceName = loadedAdapterResponseInfo?.adSourceInstanceName
                    val adSourceInstanceId = loadedAdapterResponseInfo?.adSourceInstanceId
                    val sessionId = ad.responseInfo.responseId
                    val extras = ad.responseInfo.responseExtras
                    val mediationGroupName = extras.getString("mediation_group_name")
                    val mediationABTestName = extras.getString("mediation_ab_test_name")
                    val mediationABTestVariant = extras.getString("mediation_ab_test_variant")
                    obj.put("adSourceName", adSourceName)
                    obj.put("adSourceId", adSourceId)
                    obj.put("adSourceInstanceName", adSourceInstanceName)
                    obj.put("adSourceInstanceId", adSourceInstanceId)
                    obj.put("mediationGroupName", mediationGroupName)
                    obj.put("mediationABTestName", mediationABTestName)
                    obj.put("mediationABTestVariant", mediationABTestVariant)
                    obj.put("session_id", sessionId)
                }

                is NativeAd -> {
                    val loadedAdapterResponseInfo = ad.responseInfo?.loadedAdapterResponseInfo
                    val adSourceName = loadedAdapterResponseInfo?.adSourceName
                    val adSourceId = loadedAdapterResponseInfo?.adSourceId
                    val adSourceInstanceName = loadedAdapterResponseInfo?.adSourceInstanceName
                    val adSourceInstanceId = loadedAdapterResponseInfo?.adSourceInstanceId
                    val sessionId = ad.responseInfo?.responseId
                    val extras = ad.responseInfo?.responseExtras
                    val mediationGroupName = extras?.getString("mediation_group_name")
                    val mediationABTestName = extras?.getString("mediation_ab_test_name")
                    val mediationABTestVariant = extras?.getString("mediation_ab_test_variant")
                    obj.put("adSourceName", adSourceName)
                    obj.put("adSourceId", adSourceId)
                    obj.put("adSourceInstanceName", adSourceInstanceName)
                    obj.put("adSourceInstanceId", adSourceInstanceId)
                    obj.put("mediationGroupName", mediationGroupName)
                    obj.put("mediationABTestName", mediationABTestName)
                    obj.put("mediationABTestVariant", mediationABTestVariant)
                    obj.put("session_id", sessionId)
                }
            }
            EventHelper.event("ad_price", ext = obj)
        }
    }
}
