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

import android.content.Context
import android.view.View
import androidx.core.view.isVisible
import com.bytedance.sdk.openadsdk.TTAdDislike
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
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.adLog

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/13
 * description:穿山甲横幅广告View
 */
class CSJBannerView(context: Context?,var viewHolder: BannerAdViewHolder) : BasicAdView<View>(context) {

    override fun adContentView(): View? {
        val adObj = adResult?.adObject as TTNativeExpressAd
        adObj.setExpressInteractionListener(object : TTNativeExpressAd.ExpressAdInteractionListener{
            override fun onAdClicked(p0: View?, p1: Int) {
                viewHolder.adListener?.onAdClick()
                adLog(AdManager.TAG, "穿山甲广告-横幅-点击")
            }

            override fun onAdShow(p0: View?, p1: Int) {
                adResult?.adBean?.let { viewHolder.adListener?.onAdShow(it) }
                adLog(AdManager.TAG, "穿山甲广告-横幅-展示")

            }

            override fun onRenderFail(p0: View?, p1: String?, p2: Int) {
                viewHolder.adListener?.onAdShowFail("穿山甲广告渲染失败")
                adLog(AdManager.TAG, "穿山甲广告-横幅-渲染失败")
            }

            override fun onRenderSuccess(p0: View?, p1: Float, p2: Float) {
                adLog(AdManager.TAG, "穿山甲广告-横幅-渲染成功 width:${p1}, height:${p2}")
            }
        })
        viewHolder.activity?.let {
            adObj.setDislikeCallback(it, object : TTAdDislike.DislikeInteractionCallback{
                override fun onShow() {

                }

                override fun onSelected(p0: Int, p1: String?, p2: Boolean) {
                    viewHolder.adListener?.onAdDisLike()
                    this@CSJBannerView.isVisible = false
                    adLog(AdManager.TAG, "穿山甲广告-横幅-点击不喜欢按钮")

                }

                override fun onCancel() {
                }

            })
        }

        adObj.render()

        return adObj.expressAdView
    }

    override fun onResume() {
    }

    override fun onPause() {
    }

    override fun onDestroy() {
        if (adResult?.adObject != null) {
            (adResult?.adObject as? TTNativeExpressAd)?.destroy()
            adResult?.adObject = null
        }
    }

}