package rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto

import android.app.Activity
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.base.datarecovery.BuildConfig
import com.base.datarecovery.R
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.LoadAdError
import com.google.android.gms.ads.nativead.NativeAd
import org.json.JSONObject
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Celledto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Driededto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Slantedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Modestedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.Beeedto.clickAd
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.Beeedto.pullAd
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.bounceedto.auditoredto.Beeedto.showAd
import java.util.UUID

object Crosswalkedto {

    private val TAG = "AdmobNativeUtils"
    private var nativeAd: NativeAd? = null
    private var isLoading = false
    private var nativeLoadTime = Long.MAX_VALUE
    private var loadingListener: (() -> Unit)? = null
    private val mRequest = AdRequest.Builder().build()

    fun loadNativeAd() {
        if (nativeAd != null) {
            return
        }

//        if (isLoading) {
//            return
//        }
//        isLoading = true
        if (!Modestedto.getInstance().shouldShowAd("interAd")) {
            return
        }

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

        val adLoader = AdLoader.Builder(
            Driededto.context,
            if (BuildConfig.DEBUG) Celledto.nativeAdmobIdTest else Celledto.nativeAdmobId
        ).forNativeAd {
            nativeLoadTime = System.currentTimeMillis()
            nativeAd = it
            Dishedto.logDebug(TAG, "nativeAd=${nativeAd.toString()}")

           // isLoading = false
            loadingListener?.invoke()
            pullAd(it.responseInfo, "nativeAd", reqId = reqId)
            it.setOnPaidEventListener(Beeedto.EventOnPaidEventListener(it))
        }.withAdListener(object : AdListener() {
            override fun onAdClicked() {
                clickAd(nativeAd?.responseInfo, "nativeAd")
            }

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


    fun showNativeAd(activity: Activity?, parent: ViewGroup, layout: Int = R.layout.rhythmedto) {
        if (!Modestedto.getInstance().shouldShowAd("NativeAd")) {
            return
        }
        loadingListener = {
            if (System.currentTimeMillis() - nativeLoadTime <= 1000 * 60 * 60) {
                nativeAd?.let {
                    Custodyedto(parent.context, layout).run {
                        parent.removeAllViews()
                        setNativeAd(it)
                        parent.addView(this)
                        parent.isVisible = true
                        showAd(nativeAd?.responseInfo, "nativeAd", activity)
                    }
                }
            }
            nativeAd = null
            loadingListener = null
            loadNativeAd()
        }
        if (nativeAd == null) {
            loadNativeAd()
            val obj2 = JSONObject()
            obj2.put("reason", "no coach ad")
            obj2.put("ad_unit", "nativeAd")
            Slantedto.event("ad_nocache_pull", ext = obj2)
        } else {
            loadingListener?.invoke()
        }
    }
}