package com.remax.bill.ads

import android.app.Activity
import android.content.Context
import com.remax.bill.BuildConfig
import com.remax.bill.ads.log.AdLogger
import com.remax.bill.ads.pangle.PangleAppOpenAdController
import com.remax.bill.ads.pangle.PangleBannerAdController
import com.remax.bill.ads.pangle.PangleFullScreenNativeAdController
import com.remax.bill.ads.pangle.PangleInterstitialAdController
import com.remax.bill.ads.pangle.PangleNativeAdController
import com.remax.bill.ads.topon.TopOnBannerAdController
import com.remax.bill.ads.topon.TopOnFullScreenNativeAdController
import com.remax.bill.ads.topon.TopOnInterstitialAdController
import com.remax.bill.ads.topon.TopOnNativeAdController
import com.remax.bill.ads.topon.TopOnSplashAdController
import com.remax.bill.ads.bidding.BiddingPlatformController
import com.remax.bill.ads.bidding.BiddingPlatformController.AdType
import com.remax.bill.ads.bidding.BiddingPlatformController.Platform
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

object PreloadController {
    fun preload(context: Context){
        MainScope().launch {
            try {
                AdLogger.d("插页开始异步预加载下一个广告，广告位ID: %s", BuildConfig.ADMOB_INTERSTITIAL_ID)
                InterstitialAdController.getInstance().preloadAd(context, BuildConfig.ADMOB_INTERSTITIAL_ID)
            }catch (e: Exception){
                AdLogger.e("插页异步预加载广告失败", e)
            }
        }
        MainScope().launch {
            try {
                AdLogger.d("banner开始异步预加载下一个广告，广告位ID: %s", BuildConfig.ADMOB_BANNER_ID)
                BannerAdController.getInstance().preloadAd(context,BuildConfig.ADMOB_BANNER_ID)
            }catch (e: Exception){
                AdLogger.e("banner异步预加载广告失败", e)
            }
        }

        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
            try {
                AdLogger.d("原生开始异步预加载下一个广告，广告位ID: %s", BuildConfig.ADMOB_NATIVE_ID)
                NativeAdController.getInstance().preloadAd(context, BuildConfig.ADMOB_NATIVE_ID)
            } catch (e: Exception) {
                AdLogger.e("原生异步预加载广告失败", e)
            }
        }
        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
            try {
                AdLogger.d("全屏原生开始异步预加载下一个广告，广告位ID: %s", BuildConfig.ADMOB_FULL_NATIVE_ID)
                FullScreenNativeAdController.getInstance().preloadAd(context,BuildConfig.ADMOB_FULL_NATIVE_ID)
            } catch (e: Exception) {
                AdLogger.e("全屏原生异步预加载广告失败", e)
            }
        }
    }

    /**
     * 预加载 Pangle 所有广告类型（根据 BiddingPlatformController 配置）
     */
    fun preloadPangle(context: Context) {
        // 开屏广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.APP_OPEN, Platform.PANGLE)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("Pangle开屏开始异步预加载，广告位ID: %s", BuildConfig.PANGLE_SPLASH_ID)
                    PangleAppOpenAdController.getInstance().preloadAd(context, BuildConfig.PANGLE_SPLASH_ID)
                } catch (e: Exception) {
                    AdLogger.e("Pangle开屏异步预加载广告失败", e)
                }
            }
        }

        // 插页广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.INTERSTITIAL, Platform.PANGLE)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("Pangle插页开始异步预加载，广告位ID: %s", BuildConfig.PANGLE_INTERSTITIAL_ID)
                    PangleInterstitialAdController.getInstance().preloadAd(context, BuildConfig.PANGLE_INTERSTITIAL_ID)
                } catch (e: Exception) {
                    AdLogger.e("Pangle插页异步预加载广告失败", e)
                }
            }
        }

        // Banner广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.BANNER, Platform.PANGLE)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("Pangle Banner开始异步预加载，广告位ID: %s", BuildConfig.PANGLE_BANNER_ID)
                    PangleBannerAdController.getInstance().preloadAd(context, BuildConfig.PANGLE_BANNER_ID)
                } catch (e: Exception) {
                    AdLogger.e("Pangle Banner异步预加载广告失败", e)
                }
            }
        }

        // 原生广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.NATIVE, Platform.PANGLE)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("Pangle原生开始异步预加载，广告位ID: %s", BuildConfig.PANGLE_NATIVE_ID)
                    PangleNativeAdController.getInstance().preloadAd(context, BuildConfig.PANGLE_NATIVE_ID)
                } catch (e: Exception) {
                    AdLogger.e("Pangle原生异步预加载广告失败", e)
                }
            }
        }

        // 全屏原生广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.FULL_SCREEN_NATIVE, Platform.PANGLE)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("Pangle全屏原生开始异步预加载，广告位ID: %s", BuildConfig.PANGLE_FULL_NATIVE_ID)
                    PangleFullScreenNativeAdController.getInstance().preloadAd(context, BuildConfig.PANGLE_FULL_NATIVE_ID)
                } catch (e: Exception) {
                    AdLogger.e("Pangle全屏原生异步预加载广告失败", e)
                }
            }
        }
    }

    /**
     * 预加载 TopOn 所有广告类型（根据 BiddingPlatformController 配置）
     */
    fun preloadTopOn(context: Activity) {
        // 开屏广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.APP_OPEN, Platform.TOPON)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("TopOn开屏开始异步预加载，广告位ID: %s", BuildConfig.TOPON_SPLASH_ID)
                    TopOnSplashAdController.getInstance().preloadAd(context, BuildConfig.TOPON_SPLASH_ID)
                } catch (e: Exception) {
                    AdLogger.e("TopOn开屏异步预加载广告失败", e)
                }
            }
        }

        // 插页广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.INTERSTITIAL, Platform.TOPON)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("TopOn插页开始异步预加载，广告位ID: %s", BuildConfig.TOPON_INTERSTITIAL_ID)
                    TopOnInterstitialAdController.getInstance().preloadAd(context, BuildConfig.TOPON_INTERSTITIAL_ID)
                } catch (e: Exception) {
                    AdLogger.e("TopOn插页异步预加载广告失败", e)
                }
            }
        }

        // Banner广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.BANNER, Platform.TOPON)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("TopOn Banner开始异步预加载，广告位ID: %s", BuildConfig.TOPON_BANNER_ID)
                    TopOnBannerAdController.getInstance().preloadAd(context, BuildConfig.TOPON_BANNER_ID)
                } catch (e: Exception) {
                    AdLogger.e("TopOn Banner异步预加载广告失败", e)
                }
            }
        }

        // 原生广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.NATIVE, Platform.TOPON)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("TopOn原生开始异步预加载，广告位ID: %s", BuildConfig.TOPON_NATIVE_ID)
                    TopOnNativeAdController.getInstance().preloadAd(context, BuildConfig.TOPON_NATIVE_ID)
                } catch (e: Exception) {
                    AdLogger.e("TopOn原生异步预加载广告失败", e)
                }
            }
        }

        // 全屏原生广告
        if (BiddingPlatformController.isPlatformEnabled(AdType.FULL_SCREEN_NATIVE, Platform.TOPON)) {
            CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
                try {
                    AdLogger.d("TopOn全屏原生开始异步预加载，广告位ID: %s", BuildConfig.TOPON_FULL_NATIVE_ID)
                    TopOnFullScreenNativeAdController.getInstance().preloadAd(context, BuildConfig.TOPON_FULL_NATIVE_ID)
                } catch (e: Exception) {
                    AdLogger.e("TopOn全屏原生异步预加载广告失败", e)
                }
            }
        }
    }
}