package autonomyic.polemicalic.congestionic

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.easy.clean.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 Circumcisionic(context: Context, attrs: AttributeSet? = null) :
    FrameLayout(context, attrs) {

    private val TAG = "NativeParentView"
    private var removeAll = true
    var isAdShowed = false
    var adGravity = android.view.Gravity.CENTER

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

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

            // 回收TypedArray
        }
    }


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

        val layout = resource ?: R.layout.explanatoryic
        val adView =
            layout.let { LayoutInflater.from(context).inflate(it, null) } as NativeAdView
        adView.mediaView = adView.findViewById(R.id.learnic)
        adView.headlineView = adView.findViewById(R.id.baritoneic)
        adView.bodyView = adView.findViewById(R.id.dispensaryic)
        adView.callToActionView = adView.findViewById(R.id.rotationic)
        adView.iconView = adView.findViewById(R.id.resourcefulnessic)
        (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 = adGravity

        addView(adView, params)
    }

    fun setNativeAd(
        nativeAdLoader: MaxNativeAdLoader,
        nativeAd: MaxAd,
        @LayoutRes resource: Int? = null
    ) {
        val layout = resource ?: R.layout.deceitic
        val binder: MaxNativeAdViewBinder =
            MaxNativeAdViewBinder.Builder(layout)
                .setTitleTextViewId(R.id.garishic)
                .setBodyTextViewId(R.id.yeahic)
                .setAdvertiserTextViewId(R.id.famishedic)
                .setIconImageViewId(R.id.approachic)
                .setMediaContentViewGroupId(R.id.coveic)
                .setOptionsContentViewGroupId(R.id.recitationic)
                .setStarRatingContentViewGroupId(R.id.processionalic)
                .setCallToActionButtonId(R.id.guffic)
                .build()
        val adView = MaxNativeAdView(binder, context)
        nativeAdLoader.render(adView, nativeAd)
        setBackgroundResource(0)
        removeAllViews()

        val params = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT
        )
        params.gravity = adGravity

        addView(adView, params)
    }
}
