package com.base.scanqrclear.luma

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
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.scanqrclear.R
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView

class NativeView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
): FrameLayout(context, attrs) {

    fun setExitNativeAd(nativeAd: NativeAd) {
        setNativeAd(nativeAd, R.layout.layout_ad_native_exit)
    }

    fun setExitNativeAd(
        nativeAdLoader: MaxNativeAdLoader,
        nativeAd: MaxAd,
        @LayoutRes resource: Int = R.layout.layout_ad_native_exit
    ) {
        setNativeAd(nativeAdLoader, nativeAd, resource)
    }

    fun setNativeAd(
        nativeAdLoader: MaxNativeAdLoader,
        nativeAd: MaxAd,
        @LayoutRes resource: Int = R.layout.layout_ad_native
    ) {
        val builder = MaxNativeAdViewBinder.Builder(resource)
            .setTitleTextViewId(R.id.ad_headline)
            .setIconImageViewId(R.id.ad_icon)
            .setMediaContentViewGroupId(R.id.ad_media)
            .setCallToActionButtonId(R.id.ad_call_to_action)
        if (resource != R.layout.layout_ad_native)
            builder.setBodyTextViewId(R.id.ad_body)

        visibility = VISIBLE
        val adView = MaxNativeAdView(builder.build(), context)

        nativeAdLoader.render(adView, nativeAd)
        removeAllViews()
        addView(adView)
    }

    fun setNativeAd(nativeAd: NativeAd, @LayoutRes resource: Int ?= R.layout.layout_ad_native) {
        visibility = VISIBLE
        val adView = resource?.let { LayoutInflater.from(context).inflate(it, null) } as NativeAdView
        adView.mediaView = adView.findViewById(R.id.ad_media)
        adView.headlineView = adView.findViewById(R.id.ad_headline)
        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.callToAction != null) {
            (adView.callToActionView as TextView?)?.text = nativeAd.callToAction
        }

        if (nativeAd.icon != null) {
            (adView.iconView as ImageView?)?.setImageDrawable(nativeAd.icon!!.drawable)
        }

        val body = adView.findViewById<TextView>(R.id.ad_body)
        if (body != null) {
            adView.bodyView = body
            if (nativeAd.body == null) {
                adView.bodyView?.visibility = GONE
            } else {
                adView.bodyView?.visibility = VISIBLE
                (adView.bodyView as TextView?)?.text = nativeAd.body
            }
        }

        adView.setNativeAd(nativeAd)
        removeAllViews()
        addView(adView)
    }
}