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

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.composition.android.lib.ad.AdManager
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.interfaces.AdListener
import com.composition.android.lib.ad.interfaces.AdLoader
import com.composition.android.lib.ad.util.adLogE
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.appopen.AppOpenAd
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.nativead.NativeAdView
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
import com.composition.android.ad.admob.load.request.*
import com.composition.android.ad.admob.load.view.AdmobBannerView
import com.composition.android.ad.admob.load.view.AdmobNativeAdView
import com.composition.android.lib.ad.basic.*

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/5
 * description: Admob广告加载类
 */
class AdmobAdLoader : AdLoader {


    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        if (adUnitBean.isAvailable().not()){
            return AdResult.Fail(adUnitBean, msg = "AdUnitBean无效")
        }
        //获取到具体的广告格式加载类
        val adFormatRequestImpl = when(adUnitBean.getAdType()){
            AdType.REWARDED_VIDEO-> AdmobRewardRequestImpl()
            AdType.INTERSTITIAL-> AdmobInterstitialRequestImpl()
            AdType.REWARDED_INTERSTITIAL -> AdmobRewardInterstitialRequestImpl()
            AdType.APP_OPEN -> AdmobAppOpenRequestImpl()
            AdType.BANNER-> AdmobBannerRequestImpl()
            AdType.NATIVE-> AdMobNativeRequestImpl()
            else-> null
        }
        //根据策略进行加载
        return loadByStrategy(context, adUnitBean, adFormatRequestImpl)
    }

    override fun getBannerView(context: Context, adResult: AdResult.Success,viewHolder: BannerAdViewHolder): BasicAdView<*>? {
        return if (adResult.adObject != null){
            AdmobBannerView(context).apply {
                render(adResult)
                AdManager.instance.globalListener?.invoke(adResult.adBean)
            }
        }else{
            null
        }
    }

    override fun getNativeAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder): View? {
        return if (adResult.adObject != null){
            AdmobNativeAdView(context).apply {
                setNativeAdViewHolder(viewHolder)
                render(adResult)
                AdManager.instance.globalListener?.invoke(adResult.adBean)
            }
        }else{
            null
        }
    }

    override fun populateNativeAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder, adRootView: View) {
        if (adRootView is NativeAdView){
            AdManager.instance.globalListener?.invoke(adResult.adBean)
            AdmobNativeAdView.populateNativeAdView(context, adResult, viewHolder, adRootView)
        }else{
            adLogE(AdManager.TAG, "Admob原生广告填充失败，adRootView 类型错误，根布局应该为：NativeAdView")
        }
    }

    /**
     * 显示插屏广告
     */
    override fun showInterstitialAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {

        if (success.adObject is InterstitialAd){

            val interstitialAd = success.adObject as InterstitialAd
            interstitialAd.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdDismissedFullScreenContent() {
                    super.onAdDismissedFullScreenContent()
                    adListener.onAdClose()
                }

                override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                    super.onAdFailedToShowFullScreenContent(p0)
                    adListener.onAdLoadedFail()
                }

                override fun onAdShowedFullScreenContent() {
                    super.onAdShowedFullScreenContent()
                    adListener.onAdShow(adUnitBean = success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }
            }
            interstitialAd.setOnPaidEventListener {
            }
            interstitialAd.show(activity)
        }else{
            adListener.onAdLoadedFail()
        }
    }

    /**
     * 显示激励广告
     */
    override fun showRewardedAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is RewardedAd) {
            val adObj = success.adObject as RewardedAd
            adObj.fullScreenContentCallback = object : FullScreenContentCallback() {

                override fun onAdDismissedFullScreenContent() {
                    adListener.onAdClose()

                }

                override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                    super.onAdFailedToShowFullScreenContent(p0)
                    adListener.onAdLoadedFail()
                }

                override fun onAdShowedFullScreenContent() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }
            }
            adObj.setOnPaidEventListener {
            }
            adObj.show(activity) {
                adListener.onAdRewarded()
            }
        }
    }

    override fun showRewardedInterstitialAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is RewardedInterstitialAd) {
            val rewardedInterstitialAd = success.adObject as RewardedInterstitialAd
            rewardedInterstitialAd.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdDismissedFullScreenContent() {
                    adListener.onAdClose()
                }

                override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                    super.onAdFailedToShowFullScreenContent(p0)
                    adListener.onAdLoadedFail()
                }

                override fun onAdShowedFullScreenContent() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }
            }
            rewardedInterstitialAd.setOnPaidEventListener {
            }
            rewardedInterstitialAd.show(activity) {
                adListener.onAdRewarded()
            }
        }
    }

    /**
     * 显示全屏广告，根据广告类型进行判断需要展示的广告
     * 包括：
     * 插屏广告，激励视频广告，激励插屏广告
     */
    override fun showFullScreenAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        when(success.adBean.getAdType()){
            AdType.INTERSTITIAL-> showInterstitialAd(activity, success, adListener)
            AdType.REWARDED_VIDEO -> showRewardedAd(activity, success, adListener)
            AdType.REWARDED_INTERSTITIAL -> showRewardedInterstitialAd(activity, success, adListener)
            else-> adListener.onAdShowFail("无对应广告类型的广告进行展示 AdType:${success.adBean.adType}")
        }
    }

    /**
     * 显示开屏广告
     */
    override fun showSplashAd(activity: Activity, splashViewGroup: ViewGroup?, success: AdResult.Success, adListener: AdListener) {
        val appOpenAd = success.adObject as? AppOpenAd
        if (appOpenAd != null) {
            val callback: FullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdDismissedFullScreenContent() {
                    adListener.onAdSkip()
                }

                override fun onAdFailedToShowFullScreenContent(adError: AdError) {
                    adListener.onAdSkip()
                }

                override fun onAdShowedFullScreenContent() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }
            }
            appOpenAd.fullScreenContentCallback = callback
            appOpenAd.setOnPaidEventListener {
            }
            appOpenAd.show(activity)
        }
    }




}