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

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd
import com.bytedance.sdk.openadsdk.TTNativeAd
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.bytedance.sdk.openadsdk.TTRewardVideoAd
import com.bytedance.sdk.openadsdk.TTSplashAd
import com.composition.android.ad.gromore.load.request.GMAppOpenRequestImpl
import com.composition.android.ad.gromore.load.request.GMBannerRequestImpl
import com.composition.android.ad.gromore.load.request.GMInterstitialRequestImpl
import com.composition.android.ad.gromore.load.request.GMNativeRequestImpl
import com.composition.android.ad.gromore.load.request.GMNativeTemplateRequestImpl
import com.composition.android.ad.gromore.load.request.GMRewardRequestImpl
import com.composition.android.ad.gromore.load.view.CSJNativeAdView
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.AdResult
import com.composition.android.lib.ad.basic.AdType
import com.composition.android.lib.ad.basic.Advertisers
import com.composition.android.lib.ad.basic.BannerAdViewHolder
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.basic.NativeAdViewHolder
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.composition.android.lib.ad.util.adLogWithFrame
import com.composition.android.lib.ad.util.parentRemove

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

    override val advertisers: Advertisers
        get() = Advertisers.GroMore

    /**
     * 加载广告，根据广告类型加载对应格式广告
     * @param adUnitBean 广告配置数据类
     */
    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        val adRequest = when (adUnitBean.getAdType()) {
            AdType.BANNER -> GMBannerRequestImpl()
            AdType.NATIVE -> GMNativeRequestImpl()
            AdType.NATIVE_TEMPLATE -> GMNativeTemplateRequestImpl()
            AdType.INTERSTITIAL, AdType.REWARDED_INTERSTITIAL -> GMInterstitialRequestImpl()
            AdType.REWARDED_VIDEO -> GMRewardRequestImpl()
            AdType.APP_OPEN -> GMAppOpenRequestImpl()
            else -> {
                adLogE(tag = AdManager.TAG, msg = "GroMore广告-不支持加载-${adUnitBean.adType}-类型广告")
                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) {
            AdManager.instance.globalListener?.invoke(adResult.adBean)
            when (adResult.adBean.getAdType()) {
                AdType.NATIVE -> {
                    CSJNativeAdView(context).apply {
                        setViewHolder(viewHolder)
                        render(adResult)
                    }
                }

                AdType.NATIVE_TEMPLATE -> {
                    GMNativeTemplateAdView(context).apply {
                        setViewHolder(viewHolder)
                        render(adResult)
                    }
                }

                else -> {
                    null
                }
            }
        } else {
            null
        }
    }

    override fun populateNativeAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder, advertisersContainerView: ViewGroup) {
        AdManager.instance.globalListener?.invoke(adResult.adBean)
        when (adResult.adBean.getAdType()) {
            AdType.NATIVE -> {
                CSJNativeAdView.populateNativeAdView(context = context, adResult, viewHolder, advertisersContainerView)
            }

            AdType.NATIVE_TEMPLATE -> {
                val ttNativeExpressAd = adResult.adObject as TTNativeExpressAd
                advertisersContainerView.removeAllViews()
                val expressAdView = ttNativeExpressAd.expressAdView
                expressAdView.parentRemove()
                advertisersContainerView.addView(expressAdView)
                GMNativeTemplateAdView.populateNativeTemplateAdView(context, adResult, viewHolder)
            }

            else -> {
                adLogE(tag = AdManager.TAG, msg = "GroMore-填充原生，adType类型错误，不支持")
            }
        }
    }

    override fun showInterstitialAd(activity: Activity, adResult: AdResult.Success, adListener: AdListener) {
        if (adResult.adObject is TTFullScreenVideoAd) {
            val ttFullScreenVideoAd = adResult.adObject as TTFullScreenVideoAd
            ttFullScreenVideoAd.setFullScreenVideoAdInteractionListener(object : TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {
                override fun onAdShow() {
                    adLogWithFrame(msg = "GroMore-插屏-展示")
                    adListener.onAdShow(adResult.adBean)
                    AdManager.instance.globalListener?.invoke(adResult.adBean)
                }

                override fun onAdVideoBarClick() {
                    adLogWithFrame(msg = "GroMore-插屏-点击")
                    adListener.onAdClick()
                }

                override fun onAdClose() {
                    adLogWithFrame(msg = "GroMore-插屏-关闭")
                    adListener.onAdClose()
                    destroyAd(adResult)
                }

                override fun onVideoComplete() {

                }

                override fun onSkippedVideo() {
                    adLogWithFrame(msg = "GroMore-插屏-视频跳过")
                    adListener.onAdSkip()
                }
            })
            ttFullScreenVideoAd.showFullScreenVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, null)
        } else {
            adLogWithFrame(
                msg = "GroMore-插屏-错误-类型错误，当前为:${adResult.adObject?.javaClass?.simpleName}, 应该为:TTNativeExpressAd"
            )
            adListener.onAdShowFail("类型错误，当前为:${adResult.adObject?.javaClass?.simpleName}, 应该为:TTNativeExpressAd")
        }
    }

    override fun showRewardedAd(activity: Activity, adResult: AdResult.Success, adListener: AdListener) {
        if (adResult.adObject is TTRewardVideoAd) {
            val ttRewardVideoAd = adResult.adObject as TTRewardVideoAd
            ttRewardVideoAd.setRewardAdInteractionListener(object : TTRewardVideoAd.RewardAdInteractionListener {
                override fun onAdShow() {
                    adLogWithFrame(msg = "GroMore-激励视频-展示")
                    adListener.onAdShow(adResult.adBean)
                    AdManager.instance.globalListener?.invoke(adResult.adBean)
                }

                override fun onAdVideoBarClick() {
                    adLogWithFrame(msg = "GroMore-激励视频-点击")
                    adListener.onAdClick()
                }

                override fun onAdClose() {
                    adLogWithFrame(msg = "GroMore-激励视频-关闭")
                    adListener.onAdClose()
                    destroyAd(adResult)
                }

                override fun onVideoComplete() {

                }

                override fun onVideoError() {
                    adLogWithFrame(msg = "GroMore-激励视频-视频异常")
                }

                override fun onRewardVerify(p0: Boolean, p1: Int, p2: String?, p3: Int, p4: String?) {
                    adLogWithFrame(msg = "GroMore-激励视频-获取奖励")
                    adListener.onAdRewarded()
                }

                override fun onRewardArrived(p0: Boolean, p1: Int, p2: Bundle?) {

                }

                override fun onSkippedVideo() {
                    adLogWithFrame(msg = "GroMore-激励视频-视频跳过")
                    adListener.onAdSkip()
                }
            })
            ttRewardVideoAd.showRewardVideoAd(activity)
        } else {
            adLogWithFrame(
                msg = "穿山甲广告-激励视频-异常-类型错误，当前类型为:${adResult.adObject?.javaClass?.simpleName}, 应为：TTRewardVideoAd"
            )
            adListener.onAdShowFail("类型错误，当前类型为:${adResult.adObject?.javaClass?.simpleName}, 应为：TTRewardVideoAd")
        }
    }

    override fun showRewardedInterstitialAd(activity: Activity, adResult: AdResult.Success, adListener: AdListener) {
        adLogWithFrame(msg = "GroMore-激励插屏-不支持的广告类型")
        adListener.onAdShowFail("GroMore广告不支持激励插屏广告，请使用激励视频广告")
    }

    override fun showSplashAd(activity: Activity, splashViewGroup: ViewGroup?, adResult: AdResult.Success, adListener: AdListener) {
        if (adResult.adObject is TTSplashAd) {
            val ttSplashAd = adResult.adObject as TTSplashAd
            ttSplashAd.setSplashInteractionListener(object : TTSplashAd.AdInteractionListener {
                override fun onAdClicked(p0: View?, p1: Int) {
                    adLogWithFrame(msg = "GroMore-开屏-点击")
                    adListener.onAdClick()
                }

                override fun onAdShow(p0: View?, p1: Int) {
                    adLogWithFrame(msg = "GroMore-开屏-展示")
                    adListener.onAdShow(adResult.adBean)
                    AdManager.instance.globalListener?.invoke(adResult.adBean)
                }

                override fun onAdSkip() {
                    adLogWithFrame(msg = "GroMore-开屏-跳过")
                    adListener.onAdSkip()
                    destroyAd(adResult)

                }

                override fun onAdTimeOver() {
                    adLogWithFrame(msg = "GroMore-开屏-超时")
                    adListener.onTimeOver()
                }
            })
            splashViewGroup?.removeAllViews()
            splashViewGroup?.addView(ttSplashAd.splashView)
        } else {
            adLogWithFrame(msg = "GroMore-开屏-异常-类型错误，当前类型为:${adResult.adObject?.javaClass?.simpleName}, 应为：TTRewardVideoAd")
            adListener.onAdShowFail("类型错误，当前类型为:${adResult.adObject?.javaClass?.simpleName}, 应为：TTRewardVideoAd")
        }
    }

    override fun showFullScreenAd(activity: Activity, adResult: AdResult.Success, adListener: AdListener) {
        when (adResult.adBean.getAdType()) {
            AdType.INTERSTITIAL -> showInterstitialAd(activity, adResult, adListener)
            AdType.REWARDED_VIDEO -> showRewardedAd(activity, adResult, adListener)
            else -> {
                adLogWithFrame(msg = "GroMore广告-全屏-无对应广告类型的广告进行展示 AdType:${adResult.adBean.adType}")
                adListener.onAdShowFail("GroMore广告-全屏-无对应广告类型的广告进行展示 AdType:${adResult.adBean.adType}")
            }
        }
    }

    override fun destroyAd(adResult: AdResult) {
        try {
            if (adResult is AdResult.Success) {
                when (val ad = adResult.adObject) {
                    is TTNativeExpressAd -> ad.destroy()
                    is TTNativeAd -> ad.destroy()
                    else -> {}
                }
                adResult.adObject = null
            }
            adLogWithFrame(msg = "GroMore广告-销毁广告对象完成")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}