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

import android.content.Context
import androidx.appcompat.widget.AppCompatImageView
import com.applovin.mediation.MaxAd
import com.applovin.mediation.nativeAds.MaxNativeAdListener
import com.applovin.mediation.nativeAds.MaxNativeAdLoader
import com.applovin.mediation.nativeAds.MaxNativeAdView
import com.applovin.mediation.nativeAds.MaxNativeAdViewBinder
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.adLogWithFrame

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/11/16
 * description:
 */
class AppLovinMaxNativeView(context: Context?) : BasicAdView<MaxNativeAdView>(context) {

    private var nativeAdViewHolder: NativeAdViewHolder? = null

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


    override fun adContentView(): MaxNativeAdView? {

        if (nativeAdViewHolder == null || adResult == null){
            return null
        }
        val triple = getAdObj()
        populateNativeTemplateAdView(context, adResult!!, nativeAdViewHolder!!)
        return triple?.second
    }


    private fun getAdObj(): Triple<MaxNativeAdLoader, MaxNativeAdView?, MaxAd?>? {
        return try {
            adResult?.adObject as Triple<MaxNativeAdLoader, MaxNativeAdView?, MaxAd?>
        } catch (e: Exception) {
            null
        }
    }

    override fun onResume() {

    }

    override fun onPause() {
    }

    override fun onDestroy() {
        getAdObj()?.apply {
            first.destroy(third)
        }
        adResult?.adObject = null
    }


    companion object {

        fun populateNativeTemplateAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder) {
            val binder = MaxNativeAdViewBinder.Builder(viewHolder.customLayoutRootView)

            viewHolder.titleView?.let {
                binder.setTitleTextViewId(it.id)
            }
            viewHolder.titleDescView?.let {
                binder.setBodyTextViewId(it.id)
            }
            (viewHolder.iconView as? AppCompatImageView)?.let {
                binder.setIconImageViewId(it.id)
            }
            viewHolder.contentMediaViewGroup?.let {
                binder.setMediaContentViewGroupId(it.id)
            }
            viewHolder.callActionButtonView?.let {
                binder.setCallToActionButtonId(it.id)
            }
            viewHolder.dislikeView?.let {
                binder.setOptionsContentViewGroupId(it.id)
            }
            val triple = adResult.adObject as? Triple<MaxNativeAdLoader, MaxNativeAdView?, MaxAd?>
            triple?.first?.render(MaxNativeAdView(binder.build(), context), triple.third)
            triple?.first?.setNativeAdListener(object : MaxNativeAdListener() {
                override fun onNativeAdClicked(p0: MaxAd?) {
                    super.onNativeAdClicked(p0)
                    viewHolder.adListener?.onAdClick()
                    adLogWithFrame(msg = "AppLovinMax广告-原生-点击")
                }
            })
            viewHolder.adListener?.onAdShow(adResult.adBean)
        }
    }
}