package rallyedto.sunedto.tumouredto.majorityedto.loseedto

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.applovin.mediation.MaxAd
import com.applovin.mediation.nativeAds.MaxNativeAdLoader
import com.applovin.mediation.nativeAds.MaxNativeAdView
import com.applovin.mediation.nativeAds.MaxNativeAdViewBinder
import com.base.datarecovery.R
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView

class Assumptionedto @JvmOverloads constructor(
    context: Context, val layout: Int, attrs: AttributeSet? = null
): FrameLayout(context, attrs) {

    init {
        layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }

    fun setNativeAd(
        nativeAdLoader: MaxNativeAdLoader,
        nativeAd: MaxAd,
    ) {
//        val layout = resource ?: R.layout.native_custom_ad_view
        val binder: MaxNativeAdViewBinder =
            MaxNativeAdViewBinder.Builder(layout)
                .setTitleTextViewId(R.id.storeedto)
//                .setBodyTextViewId(R.id.aloudedto)
//                .setAdvertiserTextViewId(R.id.partneredto)
                .setIconImageViewId(R.id.earthedto)
                .setMediaContentViewGroupId(R.id.veiledto)
//                .setOptionsContentViewGroupId(R.id.pavementedto)
//                .setStarRatingContentViewGroupId(R.id.approachedto)
                .setCallToActionButtonId(R.id.faithedto)
                .build()
        val adView = MaxNativeAdView(binder, context)
        nativeAdLoader.render(adView, nativeAd)
        removeAllViews()
        addView(adView)
    }

    fun setNativeAd(nativeAd: NativeAd?) {
        nativeAd ?: return

        val adView = LayoutInflater.from(context)
            .inflate(layout, this, false) as NativeAdView

//        runCatching {
//            adView.advertiserView = adView.findViewById(R.id.ad_advertiser)
//        }
        adView.mediaView = adView.findViewById(R.id.veiledto)
        adView.headlineView = adView.findViewById(R.id.storeedto)
        adView.bodyView = adView.findViewById(R.id.fragmentedto)
        adView.callToActionView = adView.findViewById(R.id.faithedto)
        adView.iconView = adView.findViewById(R.id.earthedto)
        (adView.headlineView as TextView?)?.text = nativeAd.headline
        adView.mediaView?.mediaContent = nativeAd.mediaContent
        if (nativeAd.body == null) {
            adView.bodyView?.visibility = View.INVISIBLE
        } else {
            adView.bodyView?.visibility = View.VISIBLE
            (adView.bodyView as TextView?)?.text = nativeAd.body
        }
        if (nativeAd.callToAction == null) {
            adView.callToActionView?.visibility = View.INVISIBLE
        } else {
            adView.callToActionView?.visibility = View.VISIBLE
            (adView.callToActionView as Button?)?.text = nativeAd.callToAction
        }
        if (nativeAd.icon == null) {
            adView.iconView?.visibility = View.GONE
        } else {
            (adView.iconView as ImageView?)?.setImageDrawable(
                nativeAd.icon?.drawable
            )
            adView.iconView?.visibility = View.VISIBLE
        }
        adView.setNativeAd(nativeAd)

        removeAllViews()
        addView(adView)
    }
}