package com.composition.android.ad.admob.load.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.basic.NativeAdViewHolder
import com.google.android.gms.ads.nativead.MediaView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import com.composition.android.ad.admob.R

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/6
 * description: Admob原生广告View
 */
class AdmobNativeAdView(context: Context?) : BasicAdView<NativeAdView>(context) {


    private var nativeAdViewHolder: NativeAdViewHolder? = null

    fun setNativeAdViewHolder(nativeAdViewHolder: NativeAdViewHolder) {
        this.nativeAdViewHolder = nativeAdViewHolder
    }

    override fun adContentView(): NativeAdView? {

        if (null == adResult || nativeAdViewHolder == null) {
            return null
        }
        return populateNativeAdView(context, success = adResult!!, nativeAdViewHolder = nativeAdViewHolder!!)

    }


    override fun onResume() {

    }

    override fun onPause() {

    }

    override fun onDestroy() {
        adView?.destroy()
    }


    companion object{


        fun populateNativeAdView(context: Context, success: AdResult.Success, nativeAdViewHolder: NativeAdViewHolder) : NativeAdView {
            val nativeAdView = LayoutInflater.from(context).inflate(R.layout.layout_admob_native_ad_view_root,null) as NativeAdView
            populateNativeAdView(context, success, nativeAdViewHolder, nativeAdView)
            return nativeAdView
        }


        fun populateNativeAdView(context: Context, success: AdResult.Success, nativeAdViewHolder: NativeAdViewHolder, nativeAdView: NativeAdView) {

            val nativeAd = success.adObject as NativeAd
            val mediaView = LayoutInflater.from(context).inflate(R.layout.layout_admob_native_media_view, null) as MediaView

            if (nativeAdView != nativeAdViewHolder.rootView && nativeAdViewHolder.rootView != null){
               nativeAdView.addView(nativeAdViewHolder.rootView)
            }
            nativeAdViewHolder.titleView?.let {
                it.text = nativeAd.headline
                nativeAdView.headlineView = it
            }

            nativeAdViewHolder.contentMediaGroup?.let {
                it.removeAllViews()
                it.addView(mediaView)
                nativeAdView.mediaView = mediaView

            }
            if (nativeAd.callToAction.isNullOrEmpty()) {
                nativeAdViewHolder.callActionView?.isVisible = false
            } else {
                nativeAdViewHolder.callActionView?.isVisible = true
                nativeAdViewHolder.callActionView?.let {
                    it.text = nativeAd.callToAction
                    nativeAdView.callToActionView = it
                }
            }
            if (nativeAd.icon == null) {
                nativeAdView.iconView?.visibility = View.GONE
            } else {
                nativeAdView.iconView?.visibility = View.VISIBLE
                nativeAd.icon?.let { icon ->
                    (nativeAdViewHolder.iconView as? AppCompatImageView)?.let {
                        it.setImageDrawable(icon.drawable)
                        nativeAdView.iconView = it
                    }
                }
            }

            if (nativeAd.body == null) {
                nativeAdViewHolder.titleDescView?.visibility = View.INVISIBLE
            } else {
                nativeAdViewHolder.titleDescView?.visibility = View.VISIBLE
                (nativeAdViewHolder.titleDescView as? AppCompatTextView)?.let {
                    it.text = nativeAd.body
                    nativeAdView.bodyView = it
                }
            }
            nativeAdView.setNativeAd(nativeAd)
        }

    }
}


