package com.narutohuo.xindazhou.push.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import cn.jpush.android.api.JPushInterface
import com.google.gson.Gson
import com.narutohuo.xindazhou.core.log.ILog
import com.narutohuo.xindazhou.push.factory.PushServiceFactory
import com.narutohuo.xindazhou.push.impl.PushServiceImpl

/**
 * 极光推送消息接收器（完全封装在模块内部）
 * 
 * 自动接收极光推送的消息，并通过 PushService 处理
 * 业务层不需要知道这个类的存在，所有消息会自动回调到 PushService 的监听器
 */
class JPushReceiver : BroadcastReceiver() {
    
    override fun onReceive(context: Context, intent: Intent) {
        val bundle = intent.extras ?: return
        
        when (intent.action) {
            JPushInterface.ACTION_MESSAGE_RECEIVED -> {
                // 自定义消息（透传消息）
                val title = bundle.getString(JPushInterface.EXTRA_TITLE) ?: ""
                val content = bundle.getString(JPushInterface.EXTRA_MESSAGE) ?: ""
                val extrasJson = bundle.getString(JPushInterface.EXTRA_EXTRA) ?: "{}"
                val messageId = bundle.getString(JPushInterface.EXTRA_MSG_ID)
                
                ILog.d("JPushReceiver", "收到自定义消息: title=$title, content=$content")
                
                // 解析 extras（JSON 格式）
                val extrasMap = try {
                    val jsonObject = Gson().fromJson(extrasJson, Map::class.java) as? Map<*, *>
                    jsonObject?.mapKeys { it.key.toString() }?.mapValues { it.value.toString() } ?: emptyMap()
                } catch (e: Exception) {
                    ILog.e("JPushReceiver", "解析 extras 失败", e)
                    emptyMap()
                }
                
                // 调用 PushService 处理消息
                val pushService = PushServiceFactory.getInstance()
                (pushService as? PushServiceImpl)?.handleReceivedMessage(title, content, extrasMap, messageId)
            }
            
            JPushInterface.ACTION_NOTIFICATION_RECEIVED -> {
                // 通知消息（系统通知栏）
                val title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE) ?: ""
                val content = bundle.getString(JPushInterface.EXTRA_ALERT) ?: ""
                ILog.d("JPushReceiver", "收到通知消息: title=$title, content=$content")
                // 通知消息通常由系统处理，这里可以记录日志或做其他处理
            }
            
            JPushInterface.ACTION_NOTIFICATION_OPENED -> {
                // 用户点击了通知
                val title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE) ?: ""
                val content = bundle.getString(JPushInterface.EXTRA_ALERT) ?: ""
                val messageId = bundle.getString(JPushInterface.EXTRA_MSG_ID)
                val extrasJson = bundle.getString(JPushInterface.EXTRA_EXTRA) ?: "{}"
                
                ILog.d("JPushReceiver", "用户点击通知: title=$title, content=$content")
                
                // 解析 extras（JSON 格式）
                val extrasMap = try {
                    val jsonObject = Gson().fromJson(extrasJson, Map::class.java) as? Map<*, *>
                    jsonObject?.mapKeys { it.key.toString() }?.mapValues { it.value.toString() } ?: emptyMap()
                } catch (e: Exception) {
                    ILog.e("JPushReceiver", "解析 extras 失败", e)
                    emptyMap()
                }
                
                // 调用 PushService 处理通知点击
                val pushService = PushServiceFactory.getInstance()
                (pushService as? PushServiceImpl)?.handleNotificationClick(title, content, extrasMap, messageId)
            }
        }
    }
}

