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.GMNativeExpressAdListener
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
import com.composition.android.lib.ad.util.adLog
import com.composition.android.lib.ad.util.adLogWithFrame

/**
 * @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
        }
        val ttNativeAdView = LayoutInflater.from(context).inflate(R.layout.layout_gro_more_native_template_ad_view_root, null) as TTNativeAdView
        populateNativeAdView(context, adResult!!, viewHolder!!, ttNativeAdView)
        getGmNativeAd()?.expressView?.let {
            ttNativeAdView.addView(it)
        }
        return ttNativeAdView
    }

    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, 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?) {
                        adLog(msg = "GroMore广告-原生模板-点击了不喜欢")
                        nativeAdViewHolder.adListener?.onAdDisLike()
                    }

                    override fun onCancel() {
                    }

                    override fun onRefuse() {
                    }

                    override fun onShow() {
                    }

                })
            }
            gmNativeAd.setNativeAdListener(object : GMNativeExpressAdListener {
                override fun onAdClick() {
                    adLogWithFrame(msg = "GroMore广告-原生模板-点击")
                    nativeAdViewHolder.adListener?.onAdClick()
                }

                override fun onAdShow() {
                    adLogWithFrame(msg = "GroMore广告-原生模板-展示")
                    nativeAdViewHolder.adListener?.onAdShow(adUnitBean = success.adBean)
                }

                override fun onRenderFail(p0: View?, p1: String?, p2: Int) {
                    adLogWithFrame(msg = "GroMore广告-原生模板-展示失败")
                    nativeAdViewHolder.adListener?.onAdShowFail(p1?:"GroMore原生模板广告渲染失败")
                }

                override fun onRenderSuccess(width: Float, height: Float) {

                    nativeAdViewHolder.adListener?.onAdShow(success.adBean)
//                    //获取视频播放view,该view SDK内部渲染，在媒体平台可配置视频是否自动播放等设置。
//                    val sWidth: Int
//                    val sHeight: Int
//                    val video: View? = gmNativeAd.expressView // 获取广告view  如果存在父布局，需要先从父布局中移除
//                    if (width == GMAdSize.FULL_WIDTH.toFloat() && height == GMAdSize.AUTO_HEIGHT.toFloat()) {
//                        sWidth = FrameLayout.LayoutParams.MATCH_PARENT
//                        sHeight = FrameLayout.LayoutParams.WRAP_CONTENT
//                    } else {
//                        sWidth = context.getScreenWidth()
//                        sHeight = (sWidth * height / width).toInt()
//                    }
//
//                    if (video != null){
//                        //如果存在父布局，需要先从父布局中移除
//                        video.removeParentView()
//                        ttNativeAdView.removeAllViews()
//                        val layoutParams = FrameLayout.LayoutParams(sWidth, sHeight)
//                        ttNativeAdView.addView(gmNativeAd.expressView, layoutParams)
//                    }
                }
            })
            //进行广告渲染
            gmNativeAd.render()
        }
    }
}