package com.composition.android.ad.csj.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.*
import com.composition.android.ad.csj.load.request.*
import com.composition.android.ad.csj.load.view.CSJBannerView
import com.composition.android.ad.csj.load.view.CSJNativeAdView
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

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/13
 * description: 穿山甲广告加载类
 */
class CSJAdLoader : 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 -> CSJRewardRequestImpl()
                AdType.INTERSTITIAL -> CSJInterstitialRequestImpl()
                AdType.APP_OPEN -> CSJAppOpenRequestImpl()
                AdType.BANNER -> CSJBannerRequestImpl()
                AdType.NATIVE_TEMPLATE -> CSJNativeTemplateRequestImpl()
                else -> null
            }
        return loadByStrategy(context, adUnitBean, adFormatRequestImpl)
    }

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

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

    override fun populateNativeAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder, adRootView: View) {
        AdManager.instance.globalListener?.invoke(adResult.adBean)
        CSJNativeAdView.populateNativeAdView(context, adResult, viewHolder, adRootView)
    }

    override fun showInterstitialAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is TTFullScreenVideoAd) {
            val ttFullScreenVideoAd = success.adObject as TTFullScreenVideoAd
            ttFullScreenVideoAd.setFullScreenVideoAdInteractionListener(object : TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {
                override fun onAdShow() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }

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

                override fun onAdClose() {
                    adListener.onAdClose()
                    success.adObject = null
                }

                override fun onVideoComplete() {

                }

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

            })
            ttFullScreenVideoAd.showFullScreenVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, null)
        } else {
            adListener.onAdShowFail("adObject类型错误，当前为:${success.adObject?.javaClass?.simpleName}, 应该为:TTNativeExpressAd")
        }
    }

    override fun showRewardedAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {

        if (success.adObject is TTRewardVideoAd) {
            val ttRewardVideoAd = success.adObject as TTRewardVideoAd


            ttRewardVideoAd.setRewardAdInteractionListener(object : TTRewardVideoAd.RewardAdInteractionListener {
                override fun onAdShow() {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }

                override fun onAdVideoBarClick() {

                }

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

                override fun onVideoComplete() {

                }

                override fun onVideoError() {
                }

                override fun onRewardVerify(p0: Boolean, p1: Int, p2: String?, p3: Int, p4: String?) {
                    adListener.onAdRewarded()
                }

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

                }

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

            })

            ttRewardVideoAd.showRewardVideoAd(activity)

        } else {
            adListener.onAdShowFail("穿山甲激励广告展示失败，类型错误，当前类型为:${success.adObject?.javaClass?.simpleName}, 应为：TTRewardVideoAd")
        }


    }

    override fun showRewardedInterstitialAd(activity: Activity, success: AdResult.Success, adListener: AdListener) {
        adListener.onAdShowFail("穿山甲广告不支持激励插屏广告，请使用激励视频广告")
    }

    override fun showSplashAd(activity: Activity, splashViewGroup: ViewGroup?, success: AdResult.Success, adListener: AdListener) {
        if (success.adObject is TTSplashAd) {
            val ttSplashAd = success.adObject as TTSplashAd
            ttSplashAd.setSplashInteractionListener(object : TTSplashAd.AdInteractionListener {
                override fun onAdClicked(p0: View?, p1: Int) {
                    adListener.onAdClick()
                }

                override fun onAdShow(p0: View?, p1: Int) {
                    adListener.onAdShow(success.adBean)
                    AdManager.instance.globalListener?.invoke(success.adBean)
                }

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

                override fun onAdTimeOver() {
                    adListener.onTimeOver()
                }
            })
            splashViewGroup?.removeAllViews()
            splashViewGroup?.addView(ttSplashAd.splashView)
        } else {
            adListener.onAdShowFail("穿山甲激励广告展示失败，类型错误，当前类型为:${success.adObject?.javaClass?.simpleName}, 应为：TTRewardVideoAd")
        }
    }

    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("不支持的广告类型:${success.adBean.adType}")
            }
        }

    }
}