package com.remax.bill.ads

import android.content.Context
import com.google.android.gms.ads.MobileAds
import com.remax.bill.ads.log.AdLogger
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

/**
 * AdMob SDK 管理器
 * 负责SDK初始化和全局配置
 */
object AdMobManager {
    
    private const val TAG = "AdMobManager"
    
    private val _initializationState = MutableStateFlow<AdResult<Unit>>(AdResult.Loading)
    val initializationState: StateFlow<AdResult<Unit>> = _initializationState.asStateFlow()
    
    private var isInitialized = false
    
    /**
     * 初始化 AdMob SDK
     */
    suspend fun initialize(context: Context): AdResult<Unit> {
        if (isInitialized) {
            return AdResult.Success(Unit)
        }
        
        return suspendCancellableCoroutine { continuation ->
            _initializationState.value = AdResult.Loading
            
            MobileAds.initialize(context) { initializationStatus ->
                try {
                    val statusMap = initializationStatus.adapterStatusMap
                    AdLogger.d("AdMob SDK初始化完成")
                    
                    // 输出各个适配器的状态
                    for ((className, status) in statusMap) {
                        AdLogger.d("AdMob 适配器: $className, 状态: ${status.initializationState}, 描述: ${status.description}")
                    }
                    
                    isInitialized = true
                    val result = AdResult.Success(Unit)
                    _initializationState.value = result
                    continuation.resume(result)
                    
                } catch (e: Exception) {
                    AdLogger.e("AdMob SDK初始化过程中发生异常", e)
                    val result = AdResult.Failure(
                        AdException(
                            code = AdException.ERROR_INTERNAL,
                            message = "SDK初始化异常: ${e.message}",
                            cause = e
                        )
                    )
                    _initializationState.value = result
                    continuation.resume(result)
                }
            }
        }
    }
    
    /**
     * 检查SDK是否已初始化
     */
    fun isInitialized(): Boolean {
        return isInitialized
    }
    
    /**
     * 获取当前初始化状态
     */
    fun getCurrentInitializationState(): AdResult<Unit> {
        return _initializationState.value
    }
    
    /**
     * 获取所有广告控制器的快捷访问器
     */
    object Controllers {
        val interstitial: InterstitialAdController
            get() = InterstitialAdController.getInstance()
            
        val appOpen: AppOpenAdController
            get() = AppOpenAdController.getInstance()
            
        val native: NativeAdController
            get() = NativeAdController.getInstance()
            
        val fullScreenNative: FullScreenNativeAdController
            get() = FullScreenNativeAdController.getInstance()
            
        val banner: BannerAdController
            get() = BannerAdController.getInstance()
    }
    
    /**
     * 清理所有控制器资源
     */
    fun destroyAll() {
//        Controllers.interstitial.destroy()
        Controllers.appOpen.destroy()
        Controllers.native.destroy()
        Controllers.fullScreenNative.destroy()
        Controllers.banner.destroy()
        AdLogger.d("所有广告控制器已清理")
    }
} 