package importantedto.admireedto.sweptedto.testedto.huntedto.storageedto

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 importantedto.admireedto.sweptedto.Monarchedto
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.Snapedto.clickAd
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.Snapedto.pullAd
import importantedto.admireedto.sweptedto.testedto.huntedto.storageedto.Snapedto.showAd
import importantedto.afteredto.constitutionaledto.Interferenceedto
import importantedto.afteredto.constitutionaledto.Neckedto
import importantedto.gallonedto.Rideredto
import importantedto.gallonedto.Tumbleedto
import org.json.JSONObject
import java.util.UUID

object Orbitedto {

    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 (!Monarchedto.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(
            Interferenceedto.context,
            if (BuildConfig.DEBUG) Neckedto.nativeAdmobIdTest else Neckedto.nativeAdmobId
        ).forNativeAd {
            nativeLoadTime = System.currentTimeMillis()
            nativeAd = it
            Tumbleedto.logDebug(TAG, "nativeAd=${nativeAd.toString()}")

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

            override fun onAdFailedToLoad(p0: LoadAdError) {
                Tumbleedto.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.feastedto) {
        if (!Monarchedto.getInstance().shouldShowAd("NativeAd")) {
            return
        }
        loadingListener = {
            if (System.currentTimeMillis() - nativeLoadTime <= 1000 * 60 * 60) {
                nativeAd?.let {
                    Veryedto(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")
            Rideredto.event("ad_nocache_pull", ext = obj2)
        } else {
            loadingListener?.invoke()
        }
    }
}