package com.remax.bill.ads.config

import android.annotation.SuppressLint
import android.content.Context
import com.google.gson.Gson
import com.remax.base.controller.UserChannelController
import com.remax.base.ext.KvStringDelegate
import com.remax.base.utils.RemoteConfigManager
import com.remax.bill.ads.log.AdLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.IOException
import kotlin.text.isNotEmpty

/**
 * 广告配置管理器
 */
@SuppressLint("StaticFieldLeak")
object AdConfigManager {
    private const val CONFIG_FILE_NAME = "ad_config.json"

    private const val CONFIG_KEY_INTERSTITIAL = "interstitial"
    private const val CONFIG_KEY_NATIVE = "native"
    private const val CONFIG_KEY_BANNER = "banner"
    private const val CONFIG_KEY_APP_OPEN = "app_open"
    private const val CONFIG_KEY_FULLSCREEN_NATIVE = "fullscreen_native"

    private var adConfigJsonFromRemote by KvStringDelegate("adConfigJsonRemote", "")

    private var configData: AdConfigData? = null
    private var context: Context? = null

    /**
     * 初始化所有配置
     */
    fun initialize(context: Context) {
        try {
            // 保存Context引用
            this.context = context

            // 1. 先使用本地配置进行初始化
            initializeWithLocalConfig(context)

            // 2. 监听用户渠道变化
            setupChannelListener()

            //fetchRemoteConfig()

            AdLogger.d("广告配置初始化成功，当前渠道: ${UserChannelController.getCurrentChannel()}")
        } catch (e: Exception) {
            AdLogger.e("广告配置初始化失败", e)
        }
    }

    /**
     * 异步获取远程配置
     */
    private fun fetchRemoteConfig() {
        CoroutineScope(Dispatchers.IO).launch {
            try {
                AdLogger.d("开始获取远程广告配置")
                val remoteJsonString = RemoteConfigManager.getString("adConfigJson", "")

                if (remoteJsonString != null && remoteJsonString.isNotEmpty()) {
                    AdLogger.d("成功获取远程广告配置")
                    val remoteConfig = parseConfigFromAssets(remoteJsonString)

                    // 更新本地配置
                    configData = remoteConfig
                    adConfigJsonFromRemote = remoteJsonString
                    AdLogger.d("远程广告配置更新成功")
                } else {
                    AdLogger.w("远程广告配置为空或获取超时，使用本地配置")
                }

            } catch (e: Exception) {
                AdLogger.e("获取远程广告配置异常", e)
            }
        }
    }

    /**
     * 设置渠道监听器
     */
    private fun setupChannelListener() {
        UserChannelController.addChannelChangeListener(object : UserChannelController.ChannelChangeListener {
            override fun onChannelChanged(oldChannel: UserChannelController.UserChannelType, newChannel: UserChannelController.UserChannelType) {
                AdLogger.d("广告渠道变化: ${oldChannel.value} -> ${newChannel.value}")
                // 渠道变化时，可以在这里做一些额外的处理
                // 比如重新加载配置、清理缓存等
            }
        })
    }

    /**
     * 使用本地配置初始化
     */
    private fun initializeWithLocalConfig(context: Context) {
        // 解析 JSON 配置
        val jsonString = adConfigJsonFromRemote.orEmpty().takeIf { it.isNotEmpty() }?: context.assets.open(CONFIG_FILE_NAME).bufferedReader().use { it.readText() }
        configData = parseConfigFromAssets(jsonString)

        AdLogger.d("本地广告配置初始化完成")
    }

    /**
     * 从 assets 解析配置
     */
    private fun parseConfigFromAssets(jsonString: String): AdConfigData {
        return try {
            Gson().fromJson(jsonString, AdConfigData::class.java)
        } catch (e: IOException) {
            AdLogger.e("读取配置文件失败", e)
            throw e
        } catch (e: Exception) {
            AdLogger.e("解析配置文件失败", e)
            throw e
        }
    }

    /**
     * 获取插页广告配置
     */
    fun getInterstitialConfig(): AdConfig {
        return createInterstitialConfig()
    }

    /**
     * 获取原生广告配置
     */
    fun getNativeConfig(): AdConfig {
        return createNativeConfig()
    }

    /**
     * 获取Banner广告配置
     */
    fun getBannerConfig(): AdConfig {
        return createBannerConfig()
    }

    /**
     * 获取开屏广告配置
     */
    fun getAppOpenConfig(): AdConfig {
        return createAppOpenConfig()
    }

    /**
     * 获取全屏原生广告配置
     */
    fun getFullscreenNativeConfig(): AdConfig {
        return createFullscreenNativeConfig()
    }

    /**
     * 获取激励广告配置
     */
    fun getRewardedConfig(): AdConfig? {
        return null
    }

    /**
     * 获取是否显示引导页全屏原生广告
     */
    fun shouldShowGuideFullscreenNative(): Boolean {
        return configData?.let { config ->
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural.showGuideFullscreenNative == 1
                UserChannelController.UserChannelType.PAID -> config.paid.showGuideFullscreenNative == 1
            }
        } ?: false
    }

    /**
     * 获取插屏结束后的全屏Native广告数量
     */
    fun getFullscreenNativeAfterInterstitialCount(): Int {
        return configData?.let { config ->
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural.fullscreenNativeAfterInterstitial
                UserChannelController.UserChannelType.PAID -> config.paid.fullscreenNativeAfterInterstitial
            }
        } ?: 0
    }

    /**
     * 获取开屏失败后是否展示插屏
     */
    fun shouldShowInterstitialAfterAppOpenFailure(): Boolean {
        return configData?.let { config ->
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural.showInterstitialAfterAppOpenFailure == 1
                UserChannelController.UserChannelType.PAID -> config.paid.showInterstitialAfterAppOpenFailure == 1
            }
        } ?: false
    }

    /**
     * 获取返回主页时是否展示插屏
     */
    fun shouldShowInterstitialOnHomeReturn(): Boolean {
        return configData?.let { config ->
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural.showInterstitialOnHomeReturn == 1
                UserChannelController.UserChannelType.PAID -> config.paid.showInterstitialOnHomeReturn == 1
            }
        } ?: false
    }

    /**
     * 获取语言选择时是否展示开屏广告
     */
    fun shouldShowAppOpenOnLanguageSelection(): Boolean {
        return configData?.let { config ->
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural.showAppOpenOnLanguageSelection == 1
                UserChannelController.UserChannelType.PAID -> config.paid.showAppOpenOnLanguageSelection == 1
            }
        } ?: false
    }

    /**
     * 获取随机插屏页间隔（秒）
     */
    fun getRandomInterstitialInterval(): Int {
        return configData?.let { config ->
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural.randomInterstitialInterval
                UserChannelController.UserChannelType.PAID -> config.paid.randomInterstitialInterval
            }
        }?.takeIf { it > 0 } ?: 60
    }

    /**
     * 创建插页广告配置（根据当前渠道）
     */
    private fun createInterstitialConfig(): AdConfig {
        val config = checkNotNull(configData) { "请先调用 initialize 初始化配置" }
        val ctx = checkNotNull(context) { "Context 未初始化" }
        val channelConfig = getCurrentChannelConfig(config)

        return AdConfig.Builder(ctx, CONFIG_KEY_INTERSTITIAL)
            .setMaxDailyShow(channelConfig.interstitial.maxDailyShow)
            .setMaxDailyClick(channelConfig.interstitial.maxDailyClick)
            .setMinInterval(channelConfig.interstitial.minInterval.toLong())
            .build()
    }

    /**
     * 创建原生广告配置（根据当前渠道）
     */
    private fun createNativeConfig(): AdConfig {
        val config = checkNotNull(configData) { "请先调用 initialize 初始化配置" }
        val ctx = checkNotNull(context) { "Context 未初始化" }
        val channelConfig = getCurrentChannelConfig(config)

        return AdConfig.Builder(ctx, CONFIG_KEY_NATIVE)
            .setMaxDailyShow(channelConfig.native.maxDailyShow)
            .setMaxDailyClick(channelConfig.native.maxDailyClick)
            .setMinInterval(channelConfig.native.minInterval.toLong())
            .build()
    }

    /**
     * 创建Banner广告配置（根据当前渠道）
     */
    private fun createBannerConfig(): AdConfig {
        val config = checkNotNull(configData) { "请先调用 initialize 初始化配置" }
        val ctx = checkNotNull(context) { "Context 未初始化" }
        val channelConfig = getCurrentChannelConfig(config)

        return AdConfig.Builder(ctx, CONFIG_KEY_BANNER)
            .setMaxDailyShow(channelConfig.native.maxDailyShow)
            .setMaxDailyClick(channelConfig.native.maxDailyClick)
            .setMinInterval(channelConfig.native.minInterval.toLong())
            .build()
    }

    /**
     * 创建开屏广告配置（根据当前渠道）
     */
    private fun createAppOpenConfig(): AdConfig {
        val config = configData ?: run {
            AdLogger.e("配置数据为空，请先调用 initialize 初始化配置")
            throw IllegalStateException("配置数据为空，请先调用 initialize 初始化配置")
        }

        val ctx = context ?: run {
            AdLogger.e("Context 未初始化")
            throw IllegalStateException("Context 未初始化")
        }

        val channelConfig = try {
            getCurrentChannelConfig(config)
        } catch (e: Exception) {
            AdLogger.e("获取渠道配置失败，使用默认配置", e)
            // 使用默认的natural配置
            config.natural
        }

        return AdConfig.Builder(ctx, CONFIG_KEY_APP_OPEN)
            .setMaxDailyShow(channelConfig.appOpen.maxDailyShow)
            .setMaxDailyClick(channelConfig.appOpen.maxDailyClick)
            .setMinInterval(channelConfig.appOpen.minInterval.toLong())
            .build()
    }

    /**
     * 创建全屏原生广告配置（根据当前渠道）
     */
    private fun createFullscreenNativeConfig(): AdConfig {
        val config = checkNotNull(configData) { "请先调用 initialize 初始化配置" }
        val ctx = checkNotNull(context) { "Context 未初始化" }
        val channelConfig = getCurrentChannelConfig(config)

        return AdConfig.Builder(ctx, CONFIG_KEY_FULLSCREEN_NATIVE)
            .setMaxDailyShow(channelConfig.native.maxDailyShow)
            .setMaxDailyClick(channelConfig.native.maxDailyClick)
            .setMinInterval(channelConfig.native.minInterval.toLong())
            .build()
    }

    /**
     * 获取当前渠道的配置
     */
    private fun getCurrentChannelConfig(config: AdConfigData): AdConfigData.ChannelConfig {
        return try {
            when (UserChannelController.getCurrentChannel()) {
                UserChannelController.UserChannelType.NATURAL -> config.natural
                UserChannelController.UserChannelType.PAID -> config.paid
            }
        } catch (e: Exception) {
            AdLogger.e("获取用户渠道失败，使用默认natural配置", e)
            config.natural
        }
    }
} 