package com.base.appzxhy.business.ads

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
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.appzxhy.R
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import androidx.core.content.withStyledAttributes

@SuppressLint("ViewConstructor")
class NativeParentView(context: Context, attrs: AttributeSet? = null) :
    FrameLayout(context, attrs) {

    private val TAG = "NativeParentView"
    private var removeAll = true
    var isAdShowed = false

    init {
        // 获取XML属性
        context.withStyledAttributes(attrs, R.styleable.NativeParentView, 0, 0) {

            // 从XML中读取属性值
            // 例如，如果你的XML中有自定义属性 `app:customAttribute="value"`
            removeAll = getBoolean(R.styleable.NativeParentView_removeAll, true)

            // 回收TypedArray
        }
    }


    fun setNativeAd(
        nativeAd: NativeAd,
        @LayoutRes resource: Int? = null,
    ) {

        val layout = resource ?: R.layout.layout_admob_native_custom
        val adView =
            layout.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.bodyView = adView.findViewById(R.id.ad_body)
        adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)
        adView.iconView = adView.findViewById(R.id.ad_app_icon)
        (adView.headlineView as TextView?)?.text = nativeAd.headline
        adView.mediaView?.mediaContent = nativeAd.mediaContent
        if (nativeAd.body == null) {
            adView.bodyView?.visibility = INVISIBLE
        } else {
            adView.bodyView?.visibility = VISIBLE
            (adView.bodyView as TextView?)?.text = nativeAd.body
        }
        if (nativeAd.callToAction == null) {
            adView.callToActionView?.visibility = INVISIBLE
        } else {
            adView.callToActionView?.visibility = VISIBLE
            (adView.callToActionView as Button?)?.text = nativeAd.callToAction
        }
        if (nativeAd.icon == null) {
            adView.iconView?.visibility = GONE
        } else {
            (adView.iconView as ImageView?)?.setImageDrawable(
                nativeAd.icon?.drawable
            )
            adView.iconView?.visibility = VISIBLE
        }
        adView.setNativeAd(nativeAd)
        setBackgroundResource(0)
        if (removeAll) {
            removeAllViews()
        }

        val params = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT
        )
        params.gravity = android.view.Gravity.CENTER

        addView(adView, params)
    }

    fun setNativeAd(
        nativeAdLoader: MaxNativeAdLoader,
        nativeAd: MaxAd,
        @LayoutRes resource: Int? = null
    ) {
        val layout = resource ?: R.layout.layout_max_native_custom
        val binder: MaxNativeAdViewBinder =
            MaxNativeAdViewBinder.Builder(layout)
                .setTitleTextViewId(R.id.title_text_view)
                .setBodyTextViewId(R.id.body_text_view)
                .setAdvertiserTextViewId(R.id.advertiser_text_view)
                .setIconImageViewId(R.id.icon_image_view)
                .setMediaContentViewGroupId(R.id.media_view_container)
                .setOptionsContentViewGroupId(R.id.options_view)
                .setStarRatingContentViewGroupId(R.id.star_rating_view)
                .setCallToActionButtonId(R.id.cta_button)
                .build()
        val adView = MaxNativeAdView(binder, context)
        nativeAdLoader.render(adView, nativeAd)
        setBackgroundResource(0)
        removeAllViews()

        val params = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT
        )
        params.gravity = android.view.Gravity.CENTER

        addView(adView, params)
    }
}
