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

import android.content.Context
import android.graphics.Color
import android.view.View
import androidx.core.content.ContextCompat
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.AdManager
import com.composition.android.lib.ad.basic.BannerAdViewHolder
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.util.adLogE

/**
 * @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() {
                viewHolder?.adListener?.onAdClose()
            }

            override fun onAdClicked() {
                viewHolder?.adListener?.onAdClick()
            }

            override fun onAdShow() {
                adResult?.adBean?.let { viewHolder?.adListener?.onAdShow(it) }
            }

            override fun onAdShowFail(p0: AdError) {
                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
    }
}