package com.remax.bill.ads.ext

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.remax.bill.BuildConfig
import com.remax.bill.ads.AdResult
import com.remax.bill.ads.AppOpenAdController
import com.remax.bill.ads.BannerAdController
import com.remax.bill.ads.FullScreenNativeAdController
import com.remax.bill.ads.InterstitialAdController
import com.remax.bill.ads.NativeAdController
import com.remax.bill.ads.RewardedAdController
import com.remax.bill.ads.bidding.AppOpenBiddingManager
import com.remax.bill.ads.bidding.BannerBiddingManager
import com.remax.bill.ads.bidding.BiddingWinner
import com.remax.bill.ads.bidding.FullScreenNativeBiddingManager
import com.remax.bill.ads.bidding.InterstitialBiddingManager
import com.remax.bill.ads.bidding.NativeBiddingManager
import com.remax.bill.ads.bidding.RewardedBiddingManager
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.pangle.PangleRewardedAdController
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.TopOnSplashAdController
import com.remax.bill.ads.topon.TopOnNativeAdController
import com.remax.bill.ads.topon.TopOnRewardedAdController
import com.remax.bill.ui.FullScreenNativeAdActivity
import com.remax.bill.ui.NativeAdStyle
import com.remax.bill.ui.pangle.PangleFullScreenNativeAdActivity
import com.remax.bill.ui.pangle.PangleNativeAdStyle
import com.remax.bill.ui.topon.ToponFullScreenNativeAdActivity
import com.remax.bill.ui.topon.ToponNativeAdStyle

/**
 * 广告展示扩展
 * 统一处理竞价逻辑，根据竞价结果调用对应平台的广告展示
 */
object AdShowExt {

    // ==================== 开屏广告 ====================
    
    /**
     * 展示开屏广告（带竞价）
     * @param activity Activity
     * @param onLoaded 广告加载完成回调
     * @return AdResult
     */
    suspend fun showAppOpenAd(
        activity: Activity,
        onLoaded: ((Boolean) -> Unit)? = null
    ): AdResult<Unit> {
        AdLogger.d("开屏广告竞价开始")
        
        val winner = AppOpenBiddingManager.bidding(activity)
        AdLogger.d("开屏广告竞价结果: $winner")
        
        return when (winner) {
            BiddingWinner.ADMOB -> {
                AdLogger.d("使用 AdMob 展示开屏广告")
                AppOpenAdController.getInstance().showAd(
                    activity,
                    BuildConfig.ADMOB_SPLASH_ID,
                    onLoaded = onLoaded
                )
            }
            BiddingWinner.PANGLE -> {
                AdLogger.d("使用 Pangle 展示开屏广告")
                onLoaded?.invoke(true)
                PangleAppOpenAdController.getInstance().showAd(
                    activity,
                    BuildConfig.PANGLE_SPLASH_ID,
                    onLoaded = onLoaded
                )
            }
            BiddingWinner.TOPON -> {
                AdLogger.d("使用 TopOn 展示开屏广告")
                onLoaded?.invoke(true)
                TopOnSplashAdController.getInstance().showAd(
                    activity,
                    BuildConfig.TOPON_SPLASH_ID,
                    onLoaded = onLoaded
                )
            }
        }
    }

    // ==================== 插页广告 ====================
    
    /**
     * 展示插页广告（带竞价）
     * @param activity Activity
     * @return AdResult
     */
    suspend fun showInterstitialAd(
        activity: Activity,
        ignoreFullNative: Boolean  = false
    ): AdResult<Unit> {
        AdLogger.d("插页广告竞价开始")
        
        val winner = InterstitialBiddingManager.bidding(activity)
        AdLogger.d("插页广告竞价结果: $winner")
        
        return when (winner) {
            BiddingWinner.ADMOB -> {
                AdLogger.d("使用 AdMob 展示插页广告")
                InterstitialAdController.getInstance().showAd(
                    activity,
                    BuildConfig.ADMOB_INTERSTITIAL_ID,
                    ignoreFullNative = ignoreFullNative
                )
            }
            BiddingWinner.PANGLE -> {
                AdLogger.d("使用 Pangle 展示插页广告")
                PangleInterstitialAdController.getInstance().showAd(
                    activity,
                    BuildConfig.PANGLE_INTERSTITIAL_ID,
                    ignoreFullNative = ignoreFullNative
                )
            }
            BiddingWinner.TOPON -> {
                AdLogger.d("使用 TopOn 展示插页广告")
                TopOnInterstitialAdController.getInstance().showAd(
                    activity,
                    BuildConfig.TOPON_INTERSTITIAL_ID,
                    ignoreFullNative = ignoreFullNative
                )
            }
        }
    }

    // ==================== 原生广告 ====================
    
    /**
     * 在容器中展示原生广告（带竞价）
     * @param context Context
     * @param container 广告容器
     * @param style 广告样式
     * @return Boolean 是否展示成功
     */
    suspend fun showNativeAdInContainer(
        context: Context,
        container: ViewGroup,
        style: NativeAdStyle// admob的样式
    ): Boolean {
        AdLogger.d("原生广告竞价开始")
        
        val winner = NativeBiddingManager.bidding(context)
        AdLogger.d("原生广告竞价结果: $winner")
        
        return when (winner) {
            BiddingWinner.ADMOB -> {
                AdLogger.d("使用 AdMob 展示原生广告")
                NativeAdController.getInstance().showAdInContainer(
                    context,
                    container,
                    style,
                    BuildConfig.ADMOB_NATIVE_ID
                )
            }
            BiddingWinner.PANGLE -> {
                AdLogger.d("使用 Pangle 展示原生广告")
                PangleNativeAdController.getInstance().showAdInContainer(
                    context,
                    container,
                    style = if(style == NativeAdStyle.STANDARD) PangleNativeAdStyle.STANDARD else PangleNativeAdStyle.LARGE,
                    adUnitId = BuildConfig.PANGLE_NATIVE_ID
                )
            }
            BiddingWinner.TOPON -> {
                AdLogger.d("使用 TopOn 展示原生广告")
                TopOnNativeAdController.getInstance().showAdInContainer(
                    context,
                    container,
                    style = if(style == NativeAdStyle.STANDARD) ToponNativeAdStyle.STANDARD else ToponNativeAdStyle.LARGE,
                    placementId = BuildConfig.TOPON_NATIVE_ID
                )
            }
        }
    }

    // ==================== 全屏原生广告 ====================
    
    /**
     * 在容器中展示全屏原生广告（带竞价）
     * @param context Context
     * @param container 广告容器
     * @param style 广告样式
     * @return Boolean 是否展示成功
     */
    suspend fun showFullScreenNativeAdInContainer(
        activity: Activity,
        showInterstitial: Boolean = true
    ): AdResult<Unit> {
        AdLogger.d("全屏原生广告竞价开始")
        
        val winner = FullScreenNativeBiddingManager.bidding(activity)
        AdLogger.d("全屏原生广告竞价结果: $winner")
        
        return when (winner) {
            BiddingWinner.ADMOB -> {
                AdLogger.d("使用 AdMob 展示全屏原生广告")
                FullScreenNativeAdActivity.start(activity,showInterstitial)
            }
            BiddingWinner.PANGLE -> {
                AdLogger.d("使用 Pangle 展示全屏原生广告")
                PangleFullScreenNativeAdActivity.start(activity,showInterstitial)
            }
            BiddingWinner.TOPON -> {
                AdLogger.d("使用 TopOn 展示全屏原生广告")
                ToponFullScreenNativeAdActivity.start(activity,showInterstitial)
            }
        }
    }

    // ==================== Banner广告 ====================
    
    /**
     * 展示Banner广告（带竞价）
     * @param activity Activity
     * @param container 广告容器
     * @return AdResult
     */
    suspend fun showBannerAd(
        activity: Activity,
        container: ViewGroup
    ): AdResult<View> {
        AdLogger.d("Banner广告竞价开始")
        
        val winner = BannerBiddingManager.bidding(activity)
        AdLogger.d("Banner广告竞价结果: $winner")
        
        return when (winner) {
            BiddingWinner.ADMOB -> {
                AdLogger.d("使用 AdMob 展示Banner广告")
                BannerAdController.getInstance().showAd(
                    activity,
                    container,
                    BuildConfig.ADMOB_BANNER_ID
                )
            }
            BiddingWinner.PANGLE -> {
                AdLogger.d("使用 Pangle 展示Banner广告")
                PangleBannerAdController.getInstance().showAd(
                    activity,
                    container,
                    BuildConfig.PANGLE_BANNER_ID
                )
            }
            BiddingWinner.TOPON -> {
                AdLogger.d("使用 TopOn 展示Banner广告")
                TopOnBannerAdController.getInstance().showAd(
                    activity,
                    container,
                    BuildConfig.TOPON_BANNER_ID
                )
            }
        }
    }

    // ==================== 广告展示状态检查 ====================
    
    /**
     * 检查是否有任何插页或全屏原生广告正在展示
     * @return Boolean
     */
    fun isAnyInterstitialOrFullScreenNativeShowing(): Boolean {
        return InterstitialAdController.getInstance().isAdShowing() ||
                FullScreenNativeAdController.getInstance().isAdShowing() ||
                PangleInterstitialAdController.getInstance().isAdShowing() ||
                PangleFullScreenNativeAdController.getInstance().isAdShowing() ||
                TopOnInterstitialAdController.getInstance().isAdShowing() ||
                TopOnFullScreenNativeAdController.getInstance().isAdShowing()
    }




    // ==================== 原生广告 ====================

    /**
     * 在容器中展示原生激励视频广告（带竞价）
     * @param activity Context
     * @param container 广告容器
     * @param style 广告样式
     * @return Boolean 是否展示成功
     */
    suspend fun showRewardedVideoAd(
        activity: Activity,
        rewardCallback: ((CommonRewardedData) -> Unit)? = null,
        closeCallback: ()->Unit
    ): AdResult<Unit> {
        AdLogger.d("激励视频广告竞价开始")
        val winner = RewardedBiddingManager.bidding(activity)
        AdLogger.d("激励视频广告竞价结果: $winner")



        return when (winner) {
            BiddingWinner.ADMOB -> {
                AdLogger.d("使用 AdMob 展示激励视频广告")
                RewardedAdController.getInstance().showAd(
                    activity,
                    BuildConfig.ADMOB_REWARDED_ID,
                    { rewardItem, commonRewardedData ->
                        rewardCallback?.invoke(commonRewardedData)
                        AdLogger.e("AdMob激励视频奖励回调, 奖励值：${commonRewardedData.rewardNum}")
                    }, closeCallback
                )
            }
            BiddingWinner.PANGLE -> {
                AdLogger.d("使用 Pangle 展示激励视频广告")
                PangleRewardedAdController.getInstance().showAd(
                    activity,
                    BuildConfig.PANGLE_REWARDED_ID,
                    { pagRewardItem, commonRewardedData ->
                        rewardCallback?.invoke(commonRewardedData)
                        AdLogger.e("Pangle激励视频奖励回调, 奖励值：${commonRewardedData.rewardNum}")
                    }, closeCallback
                )
            }
            BiddingWinner.TOPON -> {
                AdLogger.d("使用 TopOn 展示激励视频广告")
                TopOnRewardedAdController.getInstance().showAd(
                    activity,
                    BuildConfig.TOPON_REWARDED_ID,
                    { _, _, revenueValue ->
                        val rewardedData = CommonRewardedData(revenueValue.toDouble())
                        rewardCallback?.invoke(rewardedData)
                        //xxxx
                        AdLogger.e("TopOn激励视频奖励回调, 奖励值：$revenueValue")
                    }, closeCallback
                )
            }
        }
    }


}
