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

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.bytedance.msdk.api.format.TTNativeAdView
import com.bytedance.msdk.api.v2.GMDislikeCallback
import com.bytedance.msdk.api.v2.ad.nativeAd.GMNativeAd
import com.bytedance.msdk.api.v2.ad.nativeAd.GMNativeAdListener
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.composition.android.ad.gromore.R
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.basic.NativeAdViewHolder

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/19
 * description: GroMore原生模板广告View
 */
class GMNativeTemplateAdView(context: Context?) : BasicAdView<View>(context) {


    private var viewHolder: NativeAdViewHolder? = null

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

    private fun getGmNativeAd() : GMNativeAd?{
        return adResult?.adObject as? GMNativeAd
    }

    override fun adContentView(): View? {
        if (adResult == null || viewHolder == null) {
            return null
        }

        return populateNativeAdView(context, adResult!!, viewHolder!!)
    }

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

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

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

    companion object {
        fun populateNativeAdView(context: Context, success: AdResult.Success, nativeAdViewHolder: NativeAdViewHolder) : View{
            val ttNativeAdView = LayoutInflater.from(context).inflate(R.layout.layout_gm_native_ad_view_root, null) as TTNativeAdView
            populateNativeAdView(context, success, nativeAdViewHolder, ttNativeAdView)
            return ttNativeAdView
        }


        fun populateNativeAdView(context: Context, success: AdResult.Success, nativeAdViewHolder: NativeAdViewHolder, ttNativeAdView: TTNativeAdView) {

            val gmNativeAd = success.adObject as GMNativeAd
            if (gmNativeAd.hasDislike() && nativeAdViewHolder.activity != null) {
                gmNativeAd.setDislikeCallback(nativeAdViewHolder.activity, object : GMDislikeCallback {
                    override fun onSelected(p0: Int, p1: String?) {
                        nativeAdViewHolder.adListener?.onAdDisLike()
                    }

                    override fun onCancel() {
                    }

                    override fun onRefuse() {
                    }

                    override fun onShow() {
                    }

                })
            }
            gmNativeAd.setNativeAdListener(object : GMNativeAdListener {
                override fun onAdClick() {
                    nativeAdViewHolder.adListener?.onAdClick()
                }

                override fun onAdShow() {
                    nativeAdViewHolder.adListener?.onAdShow(adUnitBean = success.adBean)
                }
            })
            gmNativeAd.render()
            ttNativeAdView.removeAllViews()
            ttNativeAdView.addView(gmNativeAd.expressView)
        }
    }
}