package com.remax.bill.ads.pangle

import android.content.Context
import com.bytedance.sdk.openadsdk.api.PAGMInitSuccessModel
import com.bytedance.sdk.openadsdk.api.init.PAGConfig
import com.bytedance.sdk.openadsdk.api.init.PAGMConfig
import com.bytedance.sdk.openadsdk.api.init.PAGMSdk
import com.bytedance.sdk.openadsdk.api.init.PAGSdk
import com.bytedance.sdk.openadsdk.api.model.PAGErrorModel
import com.remax.bill.ads.AdException
import com.remax.bill.ads.AdResult
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

/**
 * Pangle SDK 管理器
 * 负责SDK初始化和全局配置
 * 参考文档: https://www.pangleglobal.com/integration/android-initialize-pangle-sdk
 */
object PangleManager {
    
    private const val TAG = "PangleManager"
    
    private val _initializationState = MutableStateFlow<AdResult<Unit>>(AdResult.Loading)
    val initializationState: StateFlow<AdResult<Unit>> = _initializationState.asStateFlow()
    
    private var isInitialized = false
    
    /**
     * 初始化 Pangle SDK
     * @param context 上下文
     * @param appId Pangle App ID
     * @param appIconId 应用图标资源ID（用于App Open Ads）
     */
    suspend fun initialize(context: Context, appId: String, appIconId: Int? = null): AdResult<Unit> {
        if (isInitialized || PAGSdk.isInitSuccess()) {
            isInitialized = true
            return AdResult.Success(Unit)
        }
        
        return suspendCancellableCoroutine { continuation ->
            _initializationState.value = AdResult.Loading
            
            try {
                val configBuilder = PAGMConfig.Builder()
                    .appId(appId)
                    .debugLog(AdLogger.isLogEnabled()) // 测试阶段打开，可以通过日志排查问题，上线时关闭该开关
                    .supportMultiProcess(false) // 是否支持多进程
                
                // 如果提供了应用图标，设置它（App Open Ads需要）
                appIconId?.let {
                    configBuilder.appIcon(it)
                }
                
                val config = configBuilder.build()

                PAGMSdk.init(context, config, object : PAGMSdk.PAGMInitCallback {
                    override fun success(pagmInitSuccessModel:PAGMInitSuccessModel) {
                        AdLogger.d("Pangle SDK初始化完成")
                        isInitialized = true
                        val result = AdResult.Success(Unit)
                        _initializationState.value = result
                        continuation.resume(result)
                    }
                    
                    override fun fail(pagErrorModel:PAGErrorModel) {
                        val code = pagErrorModel.errorCode
                        val msg = pagErrorModel.errorMessage
                        AdLogger.e("Pangle SDK初始化失败，错误码: %d, 错误信息: %s", code, msg ?: "")
                        val result = AdResult.Failure(
                            AdException(
                                code = AdException.ERROR_INTERNAL,
                                message = "SDK初始化失败: $msg (code: $code)"
                            )
                        )
                        _initializationState.value = result
                        continuation.resume(result)
                    }
                })
            } catch (e: Exception) {
                AdLogger.e("Pangle 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 || PAGSdk.isInitSuccess()
    }
    
    /**
     * 获取当前初始化状态
     */
    fun getCurrentInitializationState(): AdResult<Unit> {
        return _initializationState.value
    }
    
    /**
     * 获取所有广告控制器的快捷访问器
     */
    object Controllers {
        val appOpen: PangleAppOpenAdController
            get() = PangleAppOpenAdController.getInstance()
            
        val interstitial: PangleInterstitialAdController
            get() = PangleInterstitialAdController.getInstance()
            
        val banner: PangleBannerAdController
            get() = PangleBannerAdController.getInstance()
            
        val native: PangleNativeAdController
            get() = PangleNativeAdController.getInstance()
            
        val fullScreenNative: PangleFullScreenNativeAdController
            get() = PangleFullScreenNativeAdController.getInstance()
            
        val rewarded: PangleRewardedAdController
            get() = PangleRewardedAdController.getInstance()
    }
    
    /**
     * 清理所有控制器资源
     */
    fun destroyAll() {
        Controllers.appOpen.destroy()
        Controllers.interstitial.destroy()
        Controllers.banner.destroy()
        Controllers.native.destroy()
        Controllers.fullScreenNative.destroy()
        Controllers.rewarded.destroy()
        AdLogger.d("所有Pangle广告控制器已清理")
    }
}

