package com.zxhy.fastfilemanagerpro.kokoads

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.VideoController
import com.google.android.gms.ads.VideoOptions
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions
import com.google.android.gms.ads.nativead.NativeAdView
import com.zxhy.fastfilemanagerpro.databinding.AdNativeAdvanceBinding

object AdNativeAdvanceEx {

    fun refreshAd(
        activity: Activity,
        flAd: FrameLayout,
        admobAdUnitId: String,
        actionNativeAd: ((nativeAd: NativeAd) -> Unit)? = null,
    ) {
        activity.apply {
            val builder = AdLoader.Builder(this, admobAdUnitId)
            builder.forNativeAd { nativeAd ->
                val activityDestroyed = isDestroyed
                if (activityDestroyed || isFinishing || isChangingConfigurations) {
                    nativeAd.destroy()
                    return@forNativeAd
                }
                actionNativeAd?.invoke(nativeAd)


                flAd.removeAllViews()
                flAd.addView(bindAdNativeAdView(nativeAd, activity.layoutInflater))
            }

            //静音视频
            val videoOptions =
                VideoOptions.Builder().setStartMuted(true).build()
            val adOptions = NativeAdOptions.Builder().setVideoOptions(videoOptions).build()
            builder.withNativeAdOptions(adOptions)

            val adLoader = builder.withAdListener(object : AdListener() {}).build()
            adLoader.loadAd(AdRequest.Builder().build())
        }
    }

    fun bindAdNativeAdView(
        nativeAd: NativeAd,
        layoutInflater: LayoutInflater
    ): NativeAdView {
        val adNativeAdvanceBinding = AdNativeAdvanceBinding.inflate(layoutInflater)
        adNativeAdvanceBinding.apply {
            val nativeAdView = root
            nativeAdView.mediaView = adMedia

            nativeAdView.headlineView = adHeadline
            nativeAdView.bodyView = adBody
            nativeAdView.callToActionView = adCallToAction
            nativeAdView.iconView = adAppIcon
            nativeAdView.priceView = adPrice
            nativeAdView.starRatingView = adStars
            nativeAdView.storeView = adStore
            nativeAdView.advertiserView = adAdvertiser

            adHeadline.text = nativeAd.headline
            nativeAd.mediaContent?.let { adMedia.mediaContent = it }

            if (nativeAd.body == null) {
                adBody.visibility = View.INVISIBLE
            } else {
                adBody.visibility = View.VISIBLE
                adBody.text = nativeAd.body
            }

            if (nativeAd.callToAction == null) {
                adCallToAction.visibility = View.INVISIBLE
            } else {
                adCallToAction.visibility = View.VISIBLE
                adCallToAction.text = nativeAd.callToAction
            }

            if (nativeAd.icon == null) {
                adAppIcon.visibility = View.GONE
            } else {
                adAppIcon.setImageDrawable(nativeAd.icon?.drawable)
                adAppIcon.visibility = View.VISIBLE
            }

            if (nativeAd.price == null) {
                adPrice.visibility = View.INVISIBLE
            } else {
                adPrice.visibility = View.VISIBLE
                adPrice.text = nativeAd.price
            }

            if (nativeAd.store == null) {
                adStore.visibility = View.INVISIBLE
            } else {
                adStore.visibility = View.VISIBLE
                adStore.text = nativeAd.store
            }

            if (nativeAd.starRating == null) {
                adStars.visibility = View.INVISIBLE
            } else {
                adStars.rating = nativeAd.starRating!!.toFloat()
                adStars.visibility = View.VISIBLE
            }

            if (nativeAd.advertiser == null) {
                adAdvertiser.visibility = View.INVISIBLE
            } else {
                adAdvertiser.text = nativeAd.advertiser
                adAdvertiser.visibility = View.VISIBLE
            }

            nativeAdView.setNativeAd(nativeAd)
        }


        val mediaContent = nativeAd.mediaContent
        val vc = mediaContent?.videoController

        if (vc != null && mediaContent.hasVideoContent()) {
            vc.videoLifecycleCallbacks =
                object : VideoController.VideoLifecycleCallbacks() {
                    override fun onVideoEnd() {
                        super.onVideoEnd()
                    }

                }
        }
        return adNativeAdvanceBinding.root
    }

}