package com.swiftcleaner.chovey.business.newAdmob

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.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import com.swiftcleaner.chovey.R

class NativeView1 @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(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.ad_media)
        adView.headlineView = adView.findViewById(R.id.ad_headline)
        adView.bodyView = adView.findViewById(R.id.ad_body)
        adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)
        adView.iconView = adView.findViewById(R.id.ad_icon)
        (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)
    }
    fun setNativeAd(
        nativeAdLoader: MaxNativeAdLoader,
        nativeAd: MaxAd,
    ) {
//        val layout = resource ?: R.layout.native_custom_ad_view
        val binder: MaxNativeAdViewBinder =
            MaxNativeAdViewBinder.Builder(layout)
                .setTitleTextViewId(R.id.ad_headline)
//                .setBodyTextViewId(R.id.body_text_view)
//                .setAdvertiserTextViewId(R.id.advertiser_text_view)
                .setIconImageViewId(R.id.ad_icon)
                .setMediaContentViewGroupId(R.id.ad_media)
//                .setOptionsContentViewGroupId(R.id.options_view)
//                .setStarRatingContentViewGroupId(R.id.star_rating_view)
                .setCallToActionButtonId(R.id.ad_call_to_action)
                .build()
        val adView = MaxNativeAdView(binder, context)
        nativeAdLoader.render(adView, nativeAd)
        removeAllViews()
        addView(adView)
    }
}