package pxds.lmmd.zmwf.jyyf.hwbn.wzxb

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.test.basd.supercleanermax.R
import fsir.tqqe.Byfg
import org.json.JSONObject
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Lwfm
import pxds.lmmd.zmwf.Uawv
import pxds.lmmd.zmwf.Wspd
import java.util.Calendar
import java.util.UUID

object Utdc {

    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(R.string.ގޥޙޡޒ_D.string(), valueMicros)
            obj.put(R.string.މށޘޤނ_D.string(), currencyCode)
            obj.put(R.string.ޏޡޏތޡ_D.string(), precision)

            Firebase.analytics.logEvent(R.string.ކޟފޞއ_D.string(), Bundle().apply {
                putDouble(R.string.ގޥޙޡޒ_D.string(), valueMicros / 1000000.0)
            })

            val params = Bundle()
            val currentImpressionRevenue = adValue.valueMicros.toDouble() / 1000000.0
            params.putDouble(FirebaseAnalytics.Param.VALUE, currentImpressionRevenue)
            params.putString(FirebaseAnalytics.Param.CURRENCY, R.string.ޢޠޅޙލ_D.string())
            val precisionType = when (adValue.precisionType) {
                0 -> R.string.ށށޥޞހ_D.string()
                1 -> R.string.ޏޑނބބ_D.string()
                2 -> R.string.ޠނޓޖޙ_D.string()
                3 -> R.string.ގރރށޓ_D.string()
                else -> R.string.ޔފޚލޜ_D.string()
            }
            params.putString(R.string.ހޥޅވޖ_D.string(), precisionType)
            Firebase.analytics.logEvent(R.string.ޥޙޡތޑ_D.string(), params)
            val previousTaichiTroasCache = taichiPref.getFloat(R.string.ޣޕގޙށ_D.string(), 0f)
            val currentTaichiTroasCache = (previousTaichiTroasCache +
                    currentImpressionRevenue).toFloat()
            if (currentTaichiTroasCache >= 0.01) {
                val roasbundle = Bundle()
                roasbundle.putDouble(
                    FirebaseAnalytics.Param.VALUE, currentTaichiTroasCache.toDouble()
                )
                roasbundle.putString(FirebaseAnalytics.Param.CURRENCY, R.string.ޢޠޅޙލ_D.string())
                Firebase.analytics.logEvent(R.string.ޠބޔމށ_D.string(), roasbundle)
                taichiSharedPreferencesEditor.putFloat(R.string.ޣޕގޙށ_D.string(), 0f)
            } else {
                taichiSharedPreferencesEditor.putFloat(R.string.ޣޕގޙށ_D.string(), 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(R.string.ޅސޥޝޓ_D.string())
                    val mediationABTestName = extras.getString(R.string.ހޛނޅބ_D.string())
                    val mediationABTestVariant = extras.getString(R.string.ޖލޘޛޘ_D.string())
                    obj.put(R.string.ޤފޜއވ_D.string(), adUnitId)
                    obj.put(R.string.ދމޕޞޅ_D.string(), adSourceName)
                    obj.put(R.string.ޙޙޖޞލ_D.string(), adSourceId)
                    obj.put(R.string.ޥޤބވވ_D.string(), adSourceInstanceName)
                    obj.put(R.string.ށޣރޘކ_D.string(), adSourceInstanceId)
                    obj.put(R.string.އބޥށހ_D.string(), mediationGroupName)
                    obj.put(R.string.ލތނހވ_D.string(), mediationABTestName)
                    obj.put(R.string.ޞޡޠޕއ_D.string(), mediationABTestVariant)
                    obj.put(R.string.ސސޕޅފ_D.string(), 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(R.string.ޅސޥޝޓ_D.string())
                    val mediationABTestName = extras.getString(R.string.ހޛނޅބ_D.string())
                    val mediationABTestVariant = extras.getString(R.string.ޖލޘޛޘ_D.string())
                    obj.put(R.string.ޤފޜއވ_D.string(), adUnitId)
                    obj.put(R.string.ދމޕޞޅ_D.string(), adSourceName)
                    obj.put(R.string.ޙޙޖޞލ_D.string(), adSourceId)
                    obj.put(R.string.ޥޤބވވ_D.string(), adSourceInstanceName)
                    obj.put(R.string.ށޣރޘކ_D.string(), adSourceInstanceId)
                    obj.put(R.string.އބޥށހ_D.string(), mediationGroupName)
                    obj.put(R.string.ލތނހވ_D.string(), mediationABTestName)
                    obj.put(R.string.ޞޡޠޕއ_D.string(), mediationABTestVariant)
                    obj.put(R.string.ސސޕޅފ_D.string(), 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(R.string.ޅސޥޝޓ_D.string())
                    val mediationABTestName = extras.getString(R.string.ހޛނޅބ_D.string())
                    val mediationABTestVariant = extras.getString(R.string.ޖލޘޛޘ_D.string())
                    obj.put(R.string.ދމޕޞޅ_D.string(), adSourceName)
                    obj.put(R.string.ޙޙޖޞލ_D.string(), adSourceId)
                    obj.put(R.string.ޥޤބވވ_D.string(), adSourceInstanceName)
                    obj.put(R.string.ށޣރޘކ_D.string(), adSourceInstanceId)
                    obj.put(R.string.އބޥށހ_D.string(), mediationGroupName)
                    obj.put(R.string.ލތނހވ_D.string(), mediationABTestName)
                    obj.put(R.string.ޞޡޠޕއ_D.string(), mediationABTestVariant)
                    obj.put(R.string.ސސޕޅފ_D.string(), 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(R.string.ޅސޥޝޓ_D.string())
                    val mediationABTestName = extras?.getString(R.string.ހޛނޅބ_D.string())
                    val mediationABTestVariant = extras?.getString(R.string.ޖލޘޛޘ_D.string())
                    obj.put(R.string.ދމޕޞޅ_D.string(), adSourceName)
                    obj.put(R.string.ޙޙޖޞލ_D.string(), adSourceId)
                    obj.put(R.string.ޥޤބވވ_D.string(), adSourceInstanceName)
                    obj.put(R.string.ށޣރޘކ_D.string(), adSourceInstanceId)
                    obj.put(R.string.އބޥށހ_D.string(), mediationGroupName)
                    obj.put(R.string.ލތނހވ_D.string(), mediationABTestName)
                    obj.put(R.string.ޞޡޠޕއ_D.string(), mediationABTestVariant)
                    obj.put(R.string.ސސޕޅފ_D.string(), sessionId)
                }
            }
            Wspd.event(R.string.ކޟފޞއ_D.string(), ext = obj)
        }
    }

    private var mOpenAd: AppOpenAd? = null

    private var interAd: InterstitialAd? = null

    private var nativeAd: NativeAd? = null

    var isBlack = false
        get() {
            return SPUtils.getInstance().getBoolean(R.string.ޘޓނޑޔ_D.string(), field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put(R.string.ޘޓނޑޔ_D.string(), value, true)
        }

    private var isLoading = false

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

    private val taichiSharedPreferencesEditor by lazy {
        taichiPref.edit()
    }

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

    private val taichiPref by lazy {
        Lwfm.context.getSharedPreferences(R.string.ޣޕގޙށ_D.string(), 0)
    }

    private var curSingleAdClick = 0

    private var lastObj: Any? = null

    private val typeShow = R.string.ޜޓސލޘ_D.string()

    private var nativeLoadTime = Long.MAX_VALUE

    private var interLoadTime = Long.MAX_VALUE

    private var openLoadTime = Long.MAX_VALUE

    var nativeAdShowCount = 40
        get() {
            return SPUtils.getInstance().getInt(R.string.ލޔނޜހ_D.string(), field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put(R.string.ލޔނޜހ_D.string(), value)
        }

    var adShowCount = 80
        get() {
            return SPUtils.getInstance().getInt(R.string.ބޔޅޤޒ_D.string(), field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put(R.string.ބޔޅޤޒ_D.string(), value)
        }

    var isNowPlayAd = 0
        get() {
            return SPUtils.getInstance().getInt(R.string.ޣޝހވށ_D.string(), field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put(R.string.ޣޝހވށ_D.string(), value)
        }

    fun loadNativeAd() {
        if (isBlack) {
            Wspd.event(R.string.ލޞޥވޗ_D.string())
            return
        }
        if (isAdLimit()) {
            Wspd.event(R.string.ޛޏޙޑތ_D.string())
            return
        }
        if (nativeAd != null) {
            return
        }

        if (isLoading) {
            return
        }
        isLoading = true

        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put(R.string.ޤޞޑއޣ_D.string(), reqId)
        obj.put(R.string.ޜށރޥޞ_D.string(), R.string.ޜޓޟޟޤ_D.string())
        Wspd.event(R.string.ޡބޗޚޔ_D.string(), ext = obj)

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

            isLoading = false
            loadingListener?.invoke()
            pull(it.responseInfo, R.string.ޜޓޟޟޤ_D.string(), reqId = reqId)
            it.setOnPaidEventListener(EventOnPaidEventListener(it))
        }.withAdListener(object : AdListener() {
            override fun onAdClicked() {
                click(nativeAd?.responseInfo, R.string.ޜޓޟޟޤ_D.string())
                onAdClick(nativeAd)
            }

            override fun onAdFailedToLoad(p0: LoadAdError) {
                nativeAd = null
                isLoading = false
                pull(p0.responseInfo, R.string.ޜޓޟޟޤ_D.string(), p0.message, reqId = reqId)

            }
        }).build()
        adLoader.loadAd(mRequest)
    }

    private fun click(responseInfo: ResponseInfo?, adUnit: String) {

        val response = responseInfo?.adapterResponses?.getOrNull(0)
        val obj = JSONObject()
        obj.put(R.string.މޘނޑޤ_D.string(), response?.adSourceName)
        obj.put(R.string.ޖރގބޑ_D.string(), adUnit)

        val credentials = mapOf(
            R.string.ޘޞޤޡޜ_D.string() to response?.credentials?.get(R.string.ޘޞޤޡޜ_D.string()), R.string.ޟވވވކ_D.string() to response?.credentials?.get(R.string.ޟވވވކ_D.string()), R.string.ޝޤނޅޏ_D.string() to response?.credentials?.get(R.string.ޝޤނޅޏ_D.string())
        )
        obj.put(R.string.ޥޟޝޡރ_D.string(), credentials.toString())
        obj.put(R.string.ސސޕޅފ_D.string(), responseInfo?.responseId)
        obj.put(R.string.ޕޑޓޑޣ_D.string(), responseInfo?.mediationAdapterClassName)
        Wspd.event(R.string.ޞޗޟވވ_D.string(), ext = obj)
    }

    fun isOpenAdLoaded() = mOpenAd != null

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

    fun showInterstitialAd(
        activity: Activity, skip: Boolean = false, isASkip: Boolean = true, isFirst: Boolean = true, onHidden: (() -> Unit)? = null
    ) {
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
        if (isBlack) {
            Wspd.event(R.string.ލޞޥވޗ_D.string())
            onHidden?.invoke()
            return
        }

        if (isAdLimit()) {
            Wspd.event(R.string.ދޖތބބ_D.string())
            onHidden?.invoke()
            return
        }
        val obj = JSONObject()
        obj.put(R.string.ޖރގބޑ_D.string(), R.string.ފޅގޠޤ_D.string())
        if(isASkip){
            Wspd.event(R.string.ސޤފބޥ_D.string(), ext = obj)
        }

        if (skip) {
            onHidden?.invoke()
            return
        }
        if (System.currentTimeMillis() - interLoadTime > 1000 * 60 * 60) {
            interAd = null
            val obj = JSONObject()
            obj.put(R.string.ޖރގބޑ_D.string(), R.string.ފޅގޠޤ_D.string())
            Wspd.event(R.string.ޓޜޞނރ_D.string(), 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, R.string.ފޅގޠޤ_D.string())
                    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(R.string.ޒނޚޣޅ_D.string(), p0.message)
                    obj.put(R.string.ޖރގބޑ_D.string(), R.string.ފޅގޠޤ_D.string())
                    Wspd.event(R.string.ށޅޢފގ_D.string(), ext = obj)
                }

                override fun onAdShowedFullScreenContent() {
                    show(thisInterAd?.responseInfo, R.string.ފޅގޠޤ_D.string(), activity)
                }
            }
            thisInterAd?.show(activity)

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

            loadInterstitialAd(activity) {
                if (isFirst) {
                    Wspd.event(R.string.ޜޡތޚޟ_D.string())
                }
                mDialog?.dismiss()
                if (isASkip) {
                    showInterstitialAd(activity, false,false) {
                        onHidden?.invoke()
                    }
                }
            }

            val obj = JSONObject()
            obj.put(R.string.ޒނޚޣޅ_D.string(), R.string.ށޥޖފދ_D.string())
            obj.put(R.string.ޖރގބޑ_D.string(), R.string.ފޅގޠޤ_D.string())
            Wspd.event(R.string.ށޅޢފގ_D.string(), ext = obj)
            if (!isASkip) {
                mDialog?.dismiss()
                onHidden?.invoke()
            }
        }
    }

    fun loadInterstitialAd(activity: Activity, onLoad: (() -> Unit)? = null) {
        if (isBlack) {
            Wspd.event(R.string.ލޞޥވޗ_D.string())
            onLoad?.invoke()
            return
        }
        if (isAdLimit()) {
            Wspd.event(R.string.ޅޛޑދފ_D.string())
            onLoad?.invoke()
            return
        }

        if (interAd != null) {
            onLoad?.invoke()
            return
        }
        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put(R.string.ޤޞޑއޣ_D.string(), reqId)
        obj.put(R.string.ޜށރޥޞ_D.string(), R.string.ފޅގޠޤ_D.string())
        obj.put(R.string.ޣޟމޅކ_D.string(), activity.javaClass.simpleName)
        Wspd.event(R.string.ޡބޗޚޔ_D.string(), ext = obj)

        InterstitialAd.load(
            activity, Uawv.interAdmobId, mRequest, object : InterstitialAdLoadCallback() {
                override fun onAdFailedToLoad(p0: LoadAdError) {
                    Log.d(R.string.ޓތޅހޠ_D.string(), R.string.ޥނވޟގ_D.string() + p0.message)
                    interAd = null
                    onLoad?.invoke()
                    pull(p0.responseInfo, R.string.ފޅގޠޤ_D.string(), p0.message, reqId = reqId)

                }

                override fun onAdLoaded(ad: InterstitialAd) {
                    interAd = ad
                    onLoad?.invoke()
                    interLoadTime = System.currentTimeMillis()
                    pull(ad.responseInfo, R.string.ފޅގޠޤ_D.string(), reqId = reqId)
                    ad.onPaidEventListener = EventOnPaidEventListener(ad)

                }
            })
    }

    private fun getTodayCount(type: String, isNative: Boolean): Long {
        val last = Calendar.getInstance().apply {
            timeInMillis = SPUtils.getInstance()
                .getLong(R.string.ޒޕޢޜދ_D.string((if (isNative) "native" else "other").toString(), (type).toString()), 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(R.string.ޒޕޢޜދ_D.string((if (isNative) "native" else "other").toString(), (type).toString()), todayCount + 1)
    }

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

        if (isBlack) {
            Wspd.event(R.string.ލޞޥވޗ_D.string())
            onHidden?.invoke()
            return
        }

        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
        val obj = JSONObject()
        obj.put(R.string.ޖރގބޑ_D.string(), R.string.ކޓޕސޒ_D.string())
        Wspd.event(R.string.ސޤފބޥ_D.string(), 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(R.string.ޖރގބޑ_D.string(), R.string.ކޓޕސޒ_D.string())
            Wspd.event(R.string.ޓޜޞނރ_D.string(), ext = obj)
            return
        }
        if (mOpenAd != null) {
            var thisMOpenAd = mOpenAd
            mOpenAd = null
            thisMOpenAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdClicked() {
                    click(thisMOpenAd?.responseInfo, R.string.ކޓޕސޒ_D.string())
                    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(R.string.ޒނޚޣޅ_D.string(), p0.message)
                    obj.put(R.string.ޖރގބޑ_D.string(), R.string.ކޓޕސޒ_D.string())
                    Wspd.event(R.string.ށޅޢފގ_D.string(), ext = obj)
                }

                override fun onAdShowedFullScreenContent() {
                    show(thisMOpenAd?.responseInfo, R.string.ކޓޕސޒ_D.string(), activity)
                }
            }
            thisMOpenAd?.show(activity)
        } else {
            onHidden?.invoke()
            loadAppOpenAd()
            val obj = JSONObject()
            obj.put(R.string.ޒނޚޣޅ_D.string(), R.string.ށޥޖފދ_D.string())
            obj.put(R.string.ޖރގބޑ_D.string(), R.string.ކޓޕސޒ_D.string())
            Wspd.event(R.string.ށޅޢފގ_D.string(), ext = obj)
        }
    }

    fun isInterLoaded() = interAd != null

    fun loadAppOpenAd(skip: Boolean = false, onLoad: (() -> Unit)? = null) {
        if (isBlack) {
            Wspd.event(R.string.ލޞޥވޗ_D.string())
            onLoad?.invoke()
            return
        }

        if (mOpenAd != null || skip) {
            onLoad?.invoke()
            return
        }
        val reqId = UUID.randomUUID().toString()
        val obj = JSONObject()
        obj.put(R.string.ޤޞޑއޣ_D.string(), reqId)
        obj.put(R.string.ޜށރޥޞ_D.string(), R.string.ކޓޕސޒ_D.string())
        Wspd.event(R.string.ޡބޗޚޔ_D.string(), ext = obj)
        AppOpenAd.load(
            Lwfm.context, Uawv.openAdmobId, mRequest, object : AppOpenAd.AppOpenAdLoadCallback() {
                override fun onAdLoaded(ad: AppOpenAd) {
                    openLoadTime = System.currentTimeMillis()
                    mOpenAd = ad
                    onLoad?.invoke()
                    pull(ad.responseInfo, R.string.ކޓޕސޒ_D.string(), reqId = reqId)
                    ad.onPaidEventListener = EventOnPaidEventListener(ad)

                }

                override fun onAdFailedToLoad(p0: LoadAdError) {
                    mOpenAd = null
                    onLoad?.invoke()
                    pull(p0.responseInfo, R.string.ކޓޕސޒ_D.string(), p0.message, reqId = reqId)

                }
            })
    }

    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(R.string.މޘނޑޤ_D.string(), response.adSourceName)

                val credentials = mapOf(
                    R.string.ޘޞޤޡޜ_D.string() to response.credentials.get(R.string.ޘޞޤޡޜ_D.string()), R.string.ޟވވވކ_D.string() to response.credentials.get(R.string.ޟވވވކ_D.string()), R.string.ޝޤނޅޏ_D.string() to response.credentials.get(R.string.ޝޤނޅޏ_D.string())
                )
                obj.put(R.string.ޥޟޝޡރ_D.string(), credentials.toString())
            }
            obj.put(R.string.ސސޕޅފ_D.string(), responseInfo.responseId)
        }
        obj.put(R.string.ޕޑޓޑޣ_D.string(), responseInfo?.mediationAdapterClassName)
        obj.put(R.string.ޖރގބޑ_D.string(), adUnit)
        obj.put(R.string.ޤޞޑއޣ_D.string(), reqId)
        if (error == null) {
            obj.put(R.string.ށޥޜޏޛ_D.string(), "1")
        } else {
            obj.put(R.string.ވޞޘބޝ_D.string(), error)
            obj.put(R.string.ށޥޜޏޛ_D.string(), "2")
        }
        Wspd.event(R.string.ހޒޜޙމ_D.string(), ext = obj)
    }

    private fun show(responseInfo: ResponseInfo?, adUnit: String, activity: Activity? = null) {
        addTodayCount(typeShow, adUnit == R.string.ޜޓޟޟޤ_D.string())
        val response = responseInfo?.adapterResponses?.getOrNull(0)
        val obj = JSONObject()
        obj.put(R.string.މޘނޑޤ_D.string(), response?.adSourceName)
        obj.put(R.string.ޖރގބޑ_D.string(), adUnit)
        obj.put(R.string.ޕޑޓޑޣ_D.string(), responseInfo?.mediationAdapterClassName)
        val credentials = mapOf(
            R.string.ޘޞޤޡޜ_D.string() to response?.credentials?.get(R.string.ޘޞޤޡޜ_D.string()), R.string.ޟވވވކ_D.string() to response?.credentials?.get(R.string.ޟވވވކ_D.string()), R.string.ޝޤނޅޏ_D.string() to response?.credentials?.get(R.string.ޝޤނޅޏ_D.string())
        )
        obj.put(R.string.ޥޟޝޡރ_D.string(), credentials.toString())
        obj.put(R.string.ސސޕޅފ_D.string(), responseInfo?.responseId)
        obj.put(R.string.ޣޟމޅކ_D.string(), activity?.javaClass?.simpleName)
        Wspd.event(R.string.ގޞލޜޡ_D.string(), ext = obj)
    }

    fun showNativeAd(activity: Activity?, parent: ViewGroup) {
        if (isBlack) {
            Wspd.event(R.string.ލޞޥވޗ_D.string())
            return
        }

        if (isAdLimit(true)) {
            Wspd.event(R.string.ޞޠއޢޟ_D.string())
            return
        }
        val obj = JSONObject()
        obj.put(R.string.ޖރގބޑ_D.string(), R.string.ޤލކޔރ_D.string())
        Wspd.event(R.string.ސޤފބޥ_D.string(), ext = obj)

        loadingListener = {
            if (System.currentTimeMillis() - nativeLoadTime <= 1000 * 60 * 60) {
                nativeAd?.let {
                    Qynl(parent.context).run {
                        parent.removeAllViews()
                        setNativeAd(it)
                        parent.addView(this)
                        parent.isVisible = true
                        show(nativeAd?.responseInfo, R.string.ޜޓޟޟޤ_D.string(), activity)
                    }
                }
            }
            nativeAd = null
            loadingListener = null
            loadNativeAd()
        }
        if (nativeAd == null) {
            loadNativeAd()
            val obj = JSONObject()
            obj.put(R.string.ޒނޚޣޅ_D.string(), R.string.ށޥޖފދ_D.string())
            obj.put(R.string.ޖރގބޑ_D.string(), R.string.ޜޓޟޟޤ_D.string())
            Wspd.event(R.string.ށޅޢފގ_D.string(), ext = obj)
        } else {
            loadingListener?.invoke()
        }
    }

    private fun getAdShowCount(isNative: Boolean) = if (isNative) nativeAdShowCount else adShowCount

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

    }
}
