package com.composition.android.ad.admob.load.view

import android.annotation.SuppressLint
import android.content.Context
import com.composition.android.lib.ad.basic.BannerAdViewHolder
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.util.adLog
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdView

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/6
 * description:Admob横幅广告
 */
@SuppressLint("ViewConstructor")
class AdmobBannerView(context: Context?) : BasicAdView<AdView>(context) {

    private var viewHolder : BannerAdViewHolder? = null

    fun setBannerAdViewHolder(bannerAdViewHolder: BannerAdViewHolder){
        this.viewHolder = bannerAdViewHolder
    }

    override fun adContentView(): AdView? {
        val adView = adResult?.adObject as? AdView
        adView?.adListener = object : AdListener(){
            override fun onAdClicked() {
                super.onAdClicked()
                adLog(msg = "Admob广告-横幅-点击")
                viewHolder?.adListener?.onAdClick()
            }

            override fun onAdClosed() {
                super.onAdClosed()
                adLog(msg = "Admob广告-横幅-关闭")
                viewHolder?.adListener?.onAdClose()
            }

            override fun onAdOpened() {
                super.onAdOpened()
                adLog(msg = "Admob广告-横幅-打开")
                adResult?.adBean?.let { viewHolder?.adListener?.onAdShow(it) }
            }
        }

        return adView
    }

    override fun onResume() {
        adView?.resume()
    }

    override fun onPause() {
        adView?.pause()
    }

    override fun onDestroy() {
        adView?.pause()
        adView?.destroy()
        adResult?.adObject = null
    }

}