package com.swiftcleaner.chovey.business.admob

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

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

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

    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)
        }

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