package com.remax.notification.config

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import com.remax.base.ext.KvStringDelegate
import com.remax.base.utils.RemoteConfigManager
import com.remax.notification.utils.NotiLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.IOException

/**
 * 推送通知内容控制器
 */
object PushContentController {
    
    private const val CONTENT_CONFIG_FILE_NAME = "push_content_config.json"
    private const val SP_NAME = "push_content_prefs"
    private const val KEY_CURRENT_INDEX = "current_index"
    
    private var pushContents: List<PushContent>? = null
    private lateinit var sharedPreferences: SharedPreferences
    private var contentJsonFromRemote by KvStringDelegate("notificationContentJsonRemote", "")
    
    /**
     * 初始化内容配置
     * @param context 上下文
     * @return 是否初始化成功
     */
    fun initialize(context: Context): Boolean {
        return try {
            sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
            val jsonString = contentJsonFromRemote.orEmpty().takeIf { it.isNotEmpty() }?:loadContentConfigFromAssets(context)
            pushContents = parseContentConfig(jsonString)
            NotiLogger.d("推送内容配置初始化成功，共 ${pushContents?.size} 条")
            
            // 异步获取远程配置
            fetchRemotePushContent()
            
            true
        } catch (e: Exception) {
            NotiLogger.e("推送内容配置初始化失败", e)
            false
        }
    }
    
    /**
     * 获取下一个推送内容（顺序获取）
     * @return 推送内容，首次调用返回第一条，之后按顺序返回
     */
    fun getNextPushContent(): PushContent? {
        val contents = pushContents ?: return null
        if (contents.isEmpty()) return null
        
        val currentIndex = getCurrentIndex()
        val nextIndex = if (currentIndex == -1) 0 else (currentIndex + 1) % contents.size
        
        // 保存下一个索引
        saveCurrentIndex(nextIndex)
        
        val content = contents[nextIndex]
        NotiLogger.d("获取推送内容: ${content.id}, 索引: $nextIndex")
        
        return content
    }
    
    /**
     * 检查是否已初始化
     * @return 是否已初始化
     */
    fun isInitialized(): Boolean {
        return pushContents != null
    }
    
    /**
     * 异步获取远程推送内容配置
     */
    private fun fetchRemotePushContent() {
        CoroutineScope(Dispatchers.IO).launch {
            try {
                NotiLogger.d("开始获取远程推送内容配置")
                val remoteJsonString = RemoteConfigManager.getString("pushContentJson", "")
                
                if (remoteJsonString != null && remoteJsonString.isNotEmpty()) {
                    NotiLogger.d("成功获取远程推送内容配置")
                    val remoteContents = parseContentConfig(remoteJsonString)
                    
                    // 更新本地配置
                    pushContents = remoteContents
                    contentJsonFromRemote = remoteJsonString
                    NotiLogger.d("远程推送内容配置更新成功，共 ${remoteContents.size} 条")
                } else {
                    NotiLogger.w("远程推送内容配置为空或获取超时，使用本地配置")
                }
                
            } catch (e: Exception) {
                NotiLogger.e("获取远程推送内容配置异常", e)
            }
        }
    }
    
    /**
     * 从 assets 加载内容配置文件
     * @param context 上下文
     * @return JSON 字符串
     */
    private fun loadContentConfigFromAssets(context: Context): String {
        return try {
            context.assets.open(CONTENT_CONFIG_FILE_NAME).bufferedReader().use { it.readText() }
        } catch (e: IOException) {
            NotiLogger.e("加载推送内容配置文件失败", e)
            throw e
        }
    }
    
    /**
     * 解析内容配置 JSON
     * @param jsonString JSON 字符串
     * @return 内容列表
     */
    private fun parseContentConfig(jsonString: String): List<PushContent> {
        return try {
            val type = object : TypeToken<List<PushContent>>() {}.type
            Gson().fromJson(jsonString, type)
        } catch (e: JsonSyntaxException) {
            NotiLogger.e("解析推送内容配置文件失败", e)
            throw e
        }
    }
    
    /**
     * 获取当前索引
     * @return 当前索引，首次调用返回-1
     */
    private fun getCurrentIndex(): Int {
        return sharedPreferences.getInt(KEY_CURRENT_INDEX, -1)
    }
    
    /**
     * 保存当前索引到 SharedPreferences
     * @param index 索引
     */
    private fun saveCurrentIndex(index: Int) {
        sharedPreferences.edit().putInt(KEY_CURRENT_INDEX, index).apply()
    }
}
