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

import android.content.Context
import android.view.View
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.v2.ad.banner.GMBannerAd
import com.bytedance.msdk.api.v2.ad.banner.GMBannerAdListener
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.composition.android.lib.ad.util.adLogWithFrame

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/15
 * description: GroMore Banner广告View
 */
class GMBannerView(context: Context?) : BasicAdView<View>(context) {

    private var viewHolder : BannerAdViewHolder? = null

    fun setBannerViewHolder(viewHolder: BannerAdViewHolder){
        this.viewHolder = viewHolder
    }

    override fun adContentView(): View? {
        val gmBannerAd = getGmBannerAd()
        gmBannerAd?.setAdBannerListener(object : GMBannerAdListener{
            override fun onAdOpened() {
            }

            override fun onAdLeftApplication() {

            }

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

            override fun onAdClicked() {
                adLogWithFrame(msg = "GroMore广告-横幅-点击")
                viewHolder?.adListener?.onAdClick()
            }

            override fun onAdShow() {
                adLogWithFrame(msg = "GroMore广告-横幅-展示")
                adResult?.adBean?.let { viewHolder?.adListener?.onAdShow(it) }
            }

            override fun onAdShowFail(p0: AdError) {
                adLogWithFrame(msg = "GroMore广告-横幅-展示失败")
                viewHolder?.adListener?.onAdShowFail(p0.message)
            }
        })
        return gmBannerAd?.bannerView
    }

    private fun getGmBannerAd() : GMBannerAd?{
        return adResult?.adObject as? GMBannerAd
    }

    override fun onResume() {
        getGmBannerAd()?.onResume()
    }

    override fun onPause() {
        getGmBannerAd()?.onPause()
    }

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