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

import android.content.Context
import android.view.View
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.AdResult
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.basic.NativeAdViewHolder
import com.composition.android.lib.ad.util.adLog

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/13
 * description: 穿山甲原生模板广告View
 */
class CSJNativeTemplateAdView(context: Context?) : BasicAdView<View>(context) {

    private var viewHolder: NativeAdViewHolder? = null

    fun setViewHolder(viewHolder: NativeAdViewHolder) {
        this.viewHolder = viewHolder
    }

    override fun adContentView(): View? {
        if (adResult == null || viewHolder == null) {
            return null
        }
        populateNativeTemplateAdView(context, adResult = adResult!!, viewHolder = viewHolder!!)
        val ttNativeExpressAd = adResult?.adObject as TTNativeExpressAd
        return ttNativeExpressAd.expressAdView
    }

    override fun onResume() {
    }

    override fun onPause() {

    }

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

    companion object {

        fun populateNativeTemplateAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder) {

            val ttNativeExpressAd = adResult.adObject as TTNativeExpressAd
            ttNativeExpressAd.render()
            viewHolder.activity?.let {
                ttNativeExpressAd.setDislikeCallback(it, object : TTAdDislike.DislikeInteractionCallback {
                    override fun onShow() {

                    }

                    override fun onSelected(p0: Int, p1: String?, p2: Boolean) {
                        adLog(msg = "穿山甲广告-原生模板-点击不喜欢按钮")
                        viewHolder.adListener?.onAdDisLike()
                    }

                    override fun onCancel() {
                    }

                })
            }

            ttNativeExpressAd.setExpressInteractionListener(object : TTNativeExpressAd.AdInteractionListener {
                override fun onAdClicked(p0: View?, p1: Int) {
                    adLog(msg = "穿山甲广告-原生模板-点击广告")
                    viewHolder.adListener?.onAdClick()
                }

                override fun onAdShow(p0: View?, p1: Int) {
                    adLog(msg = "穿山甲广告-原生模板-广告展示")
                    viewHolder.adListener?.onAdShow(adResult.adBean)

                }

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

                override fun onRenderSuccess(p0: View?, width: Float, height: Float) {
                    adLog(AdManager.TAG, "穿山甲广告-原生模板-渲染成功 width:${width},height:${height}")
                }

                override fun onAdDismiss() {
                    adLog(msg = "穿山甲广告-原生模板-广告关闭")
                    viewHolder.adListener?.onAdClose()
                }
            })

        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        adResult?.adObject = null
    }
}