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.AdNativeAdvanceFastBinding
import com.zxhy.fastfilemanagerpro.databinding.AdNativeAdvanceSampleBinding

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(bindAdNativeAdViewSample(nativeAd, activity.layoutInflater))
                flAd.addView(bindFastNativeAd(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())
        }
    }


    /**
     * 官方布局
     */
    private fun bindAdNativeAdViewSample(
        nativeAd: NativeAd,
        layoutInflater: LayoutInflater
    ): NativeAdView {
        val adNativeAdvanceBinding = AdNativeAdvanceSampleBinding.inflate(layoutInflater)

        val nativeAdView = adNativeAdvanceBinding.root
        nativeAdView.mediaView = adNativeAdvanceBinding.adMedia

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

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

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

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

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

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

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

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

        if (nativeAd.advertiser == null) {
            adNativeAdvanceBinding.adAdvertiser.visibility = View.INVISIBLE
        } else {
            adNativeAdvanceBinding.adAdvertiser.text = nativeAd.advertiser
            adNativeAdvanceBinding.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
    }

    fun bindFastNativeAd(
        nativeAd: NativeAd,
        layoutInflater: LayoutInflater
    ): NativeAdView {
        val adNativeAdvanceBinding = AdNativeAdvanceFastBinding.inflate(layoutInflater)

        val nativeAdView = adNativeAdvanceBinding.root
        nativeAdView.mediaView = adNativeAdvanceBinding.adMedia

        nativeAdView.headlineView = adNativeAdvanceBinding.adHeadline
        nativeAdView.bodyView = adNativeAdvanceBinding.adBody
        nativeAdView.callToActionView = adNativeAdvanceBinding.adCallToAction
        nativeAdView.iconView = adNativeAdvanceBinding.adAppIcon
        nativeAdView.starRatingView = adNativeAdvanceBinding.adStars
        nativeAdView.advertiserView = adNativeAdvanceBinding.adAdvertiser

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

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

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

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

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

        if (nativeAd.advertiser == null) {
            adNativeAdvanceBinding.adAdvertiser.visibility = View.INVISIBLE
        } else {
            adNativeAdvanceBinding.adAdvertiser.text = nativeAd.advertiser
            adNativeAdvanceBinding.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
    }

}