package com.composition.android.ad.gromore.load

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.format.TTNativeAdView
import com.bytedance.msdk.api.reward.RewardItem
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAd
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAdListener
import com.bytedance.msdk.api.v2.ad.reward.GMRewardAd
import com.bytedance.msdk.api.v2.ad.reward.GMRewardedAdListener
import com.bytedance.msdk.api.v2.ad.splash.GMSplashAd
import com.bytedance.msdk.api.v2.ad.splash.GMSplashAdListener
import com.composition.android.ad.gromore.load.request.*
import com.composition.android.ad.gromore.load.view.GMBannerView
import com.composition.android.ad.gromore.load.view.GMNativeTemplateAdView
import com.composition.android.lib.ad.AdManager
import com.composition.android.lib.ad.basic.*
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

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/15
 * description:GroMore广告加载类
 */
class GroMoreAdLoader : AdLoader {

    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        val adRequest = when (adUnitBean.getAdType()) {
            AdType.BANNER -> GMBannerRequestImpl()
            AdType.NATIVE_TEMPLATE -> GMNativeTemplateRequestImpl()
            AdType.INTERSTITIAL, AdType.REWARDED_INTERSTITIAL -> GMInterstitialRequestImpl()
            AdType.REWARDED_VIDEO -> GMRewardRequestImpl()
            AdType.APP_OPEN -> GMAppOpenRequestImpl()
            else -> null
        }
        return loadByStrategy(context, adUnitBean, adRequest)

    }

    override fun getBannerView(context: Context, adResult: AdResult.Success, viewHolder: BannerAdViewHolder): BasicAdView<*>? {
        return if (adResult.adObject != null) {
            GMBannerView(context).apply {
                setBannerViewHolder(viewHolder)
                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) {
            GMNativeTemplateAdView(context).apply {
                setViewHolder(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 TTNativeAdView) {
            GMNativeTemplateAdView.populateNativeAdView(context, adResult, viewHolder, adRootView)
        } else {
            adLogE(AdManager.TAG, "GroMore原生模板广告填充失败， adRootView错误，当前类型为${adRootView.javaClass.simpleName},需要为${TTNativeAdView::class.java.simpleName}")
        }
    }

    override fun showInterstitialAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is GMInterstitialFullAd) {
            val mInterstitialFullAd = success.adObject as GMInterstitialFullAd
            mInterstitialFullAd.setAdInterstitialFullListener(object : GMInterstitialFullAdListener {
                override fun onInterstitialFullShow() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }

                override fun onInterstitialFullShowFail(p0: AdError) {
                    adListener.onAdShowFail(p0.message)
                }

                override fun onInterstitialFullClick() {
                    adListener.onAdClick()
                }

                override fun onInterstitialFullClosed() {
                    adListener.onAdClose()
                }

                override fun onVideoComplete() {
                }

                override fun onVideoError() {
                }

                override fun onSkippedVideo() {
                }

                override fun onAdOpened() {
                }

                override fun onAdLeftApplication() {
                }

                override fun onRewardVerify(p0: RewardItem) {
                    adListener.onAdRewarded()
                }

            })
            mInterstitialFullAd.showAd(activity)

        } else {
            adListener.onAdShowFail("adObject类型错误或为空:${success.adObject?.javaClass?.simpleName}")
        }
    }

    override fun showRewardedAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is GMRewardAd) {
            val gmRewardAd = success.adObject as GMRewardAd
            gmRewardAd.setRewardAdListener(object : GMRewardedAdListener {
                override fun onRewardedAdShow() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }

                override fun onRewardedAdShowFail(p0: AdError) {
                    adListener.onAdShowFail(p0.message)
                }

                override fun onRewardClick() {
                    adListener.onAdClick()
                }

                override fun onRewardedAdClosed() {
                    adListener.onAdClose()
                }

                override fun onVideoComplete() {
                }

                override fun onVideoError() {
                }

                override fun onRewardVerify(p0: RewardItem) {
                    adListener.onAdRewarded()
                }

                override fun onSkippedVideo() {
                }
            })

            if (gmRewardAd.isReady) {
                gmRewardAd.showRewardAd(activity)
            } else {
                adListener.onAdShowFail("GroMore激励视频对象还未准备完成，显示失败")
            }
        } else {
            adListener.onAdShowFail("adObject类型错误或为空:${success.adObject?.javaClass?.simpleName}")
        }

    }

    override fun showRewardedInterstitialAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        adListener.onAdShowFail("GroMore不支持激励插屏广告")
    }

    override fun showSplashAd(activity: Activity, splashViewGroup: ViewGroup?, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is GMSplashAd) {
            val gmAppOpenAd = success.adObject as GMSplashAd
            if (gmAppOpenAd.isReady) {
                gmAppOpenAd.setAdSplashListener(object : GMSplashAdListener {
                    override fun onAdClicked() {
                        adListener.onAdClick()
                    }

                    override fun onAdShow() {
                        adListener.onAdShow(success.adBean)
                        AdManager.instance.globalListener?.invoke(success.adBean)
                    }

                    override fun onAdShowFail(p0: AdError) {
                        adListener.onAdShowFail(p0.message)
                    }

                    override fun onAdSkip() {
                        adListener.onAdSkip()
                    }

                    override fun onAdDismiss() {
                        adListener.onAdClose()
                    }
                })
                gmAppOpenAd.showAd(splashViewGroup)
            }
        }
    }

    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)
            else -> {
                adListener.onAdShowFail("无对应广告类型的广告进行展示 AdType:${success.adBean.adType}")
            }
        }
    }
}