package com.base.locationsharewhite.ads.admob

import android.os.Bundle
import android.view.ViewGroup
import android.view.ViewTreeObserver
import com.base.locationsharewhite.GlobalConfig
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.ads.LimitUtils
import com.google.ads.mediation.admob.AdMobAdapter
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import java.util.UUID

/**
 *banner广告加载显示管理类
 */
class AdBannerMgr {

    private var adView: AdView? = null
    private var listener: ViewTreeObserver.OnGlobalLayoutListener? = null

    fun show(parent: ViewGroup, collapsible: Boolean, adClose: (() -> Unit)? = null) {

        if (!AdsMgr.adsConfigBean.adSwitch) {
            return
        }
        val admobEvent = AdmobEvent("banner", "banner")

        if (!LimitUtils.isAdShow(AdsType.BANNER, admobEvent)) {
            adView = null
            return
        }

        parent.removeAllViews()

        adView?.destroy()
        adView = null

        adView = AdView(parent.context)
        parent.addView(adView)
        adView?.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(adView)

        listener = ViewTreeObserver.OnGlobalLayoutListener {
            val screenPixelDensity = parent.context.resources.displayMetrics.density
            val adWidth = (parent.width / screenPixelDensity).toInt()
            val adSize =
                AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(parent.context, adWidth)
            adView?.adUnitId = GlobalConfig.ID_ADMOB_BANNER
            adView?.setAdSize(adSize)
            loadAd(admobEvent, collapsible, adClose)
            parent.viewTreeObserver.removeOnGlobalLayoutListener(listener)
        }
        parent.viewTreeObserver.addOnGlobalLayoutListener(listener)
    }


    fun loadAd(admobEvent: AdmobEvent, collapsible: Boolean, adClose: (() -> Unit)?) {
        val build = AdRequest.Builder()

        if (collapsible) {
            val extras = Bundle()
            extras.putString("collapsible", "bottom")
            extras.putString("collapsible_request_id", UUID.randomUUID().toString())
            build.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
        }
        val adRequest = build.build()
        adView?.adListener =
            object : AdListener() {
                override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                    super.onAdFailedToLoad(loadAdError)
                    admobEvent.pullAd(loadAdError.responseInfo, loadAdError)
                }

                override fun onAdLoaded() {
                    admobEvent.pullAd(adView?.responseInfo)
                }

                override fun onAdOpened() {

                }

                override fun onAdClosed() {
                    super.onAdClosed()
                    adClose?.invoke()
                }
            }
        adView?.loadAd(adRequest)

    }
}