package com.a2i.forwarder.core

import android.app.Notification
import android.content.Context
import android.service.notification.StatusBarNotification
import com.a2i.forwarder.A2iApp
import com.a2i.forwarder.model.BarkMessage
import java.util.LinkedHashMap

class NotificationProcessor(private val context: Context) {
    private val settings get() = A2iApp.instance.settings
    private val rules get() = A2iApp.instance.appRules

    data class Decision(
        val message: BarkMessage?,
        val reason: String,
        val appLabel: String,
        val code: String?,
    )

    // ========== 去重缓存 ==========
    // 防止同一条通知在短时间内被 Android 系统反复投递导致重复转发
    // key = "${pkg}|${title}|${body}" 的 hash, value = 上次转发时间戳
    private val dedupCache = object : LinkedHashMap<String, Long>(64, 0.75f, true) {
        override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, Long>?): Boolean {
            return size > 128
        }
    }
    private val DEDUP_WINDOW_MS = 5_000L  // 5 秒内相同内容不重复转发

    // ========== 系统状态消息过滤模式 ==========
    private val systemStatusPatterns = listOf(
        Regex("""正在运行"""),
        Regex("""is running""", RegexOption.IGNORE_CASE),
        Regex("""运行中"""),
        Regex("""服务(正在|已)启动"""),
        Regex("""service (is )?running""", RegexOption.IGNORE_CASE),
        Regex("""^.*(?:正在|is)\s*(?:运行|running|run).*$""", RegexOption.IGNORE_CASE),
    )

    // ========== 系统剪贴板/无用服务包名 ==========
    private val noisePackages = setOf(
        "com.samsung.android.honeyboard",
        "com.samsung.android.app.clipboard",
        "com.samsung.android.clipboard",
        "com.android.systemui",
        "com.google.android.inputmethod.latin",
        "com.google.android.gms",
    )

    fun process(sbn: StatusBarNotification): Decision {
        val pkg = sbn.packageName
        val n = sbn.notification ?: return Decision(null, "无通知", pkg, null)
        val extras = n.extras

        // 提取标题和文本（尝试多个来源）
        val title = extras.getCharSequence(Notification.EXTRA_TITLE)?.toString().orEmpty()
        var text = extras.getCharSequence(Notification.EXTRA_TEXT)?.toString().orEmpty()
        val big = extras.getCharSequence(Notification.EXTRA_BIG_TEXT)?.toString().orEmpty()
        val sub = extras.getCharSequence(Notification.EXTRA_SUB_TEXT)?.toString().orEmpty()

        // ---- 修复 ***** 脱敏问题 ----
        // 部分 ROM 会把验证码通知的 EXTRA_TEXT 脱敏为 *****
        // 尝试从 tickerText 和其他备用源获取真实内容
        if (isRedacted(text)) {
            // 优先用 bigText（通常未被脱敏）
            if (!isRedacted(big) && big.isNotBlank()) {
                text = big
            } else {
                // 尝试 tickerText（deprecated 但仍可能包含原文）
                val ticker = n.tickerText?.toString().orEmpty()
                if (!isRedacted(ticker) && ticker.isNotBlank()) {
                    text = ticker
                }
            }
        }

        // 确定最终 content：优先非空、非脱敏的内容
        val content = listOf(text, big)
            .firstOrNull { it.isNotBlank() && !isRedacted(it) }
            ?: text.ifBlank { big }

        val appLabel = runCatching {
            context.packageManager.getApplicationLabel(context.packageManager.getApplicationInfo(pkg, 0)).toString()
        }.getOrDefault(pkg)

        // ---- 全局开关 ----
        if (!settings.forwardingEnabled.value)
            return Decision(null, "全局转发已关", appLabel, null)

        // ---- 持续性通知过滤 ----
        val isOngoing = n.flags and Notification.FLAG_ONGOING_EVENT != 0
        if (settings.skipOngoing.value && isOngoing)
            return Decision(null, "持续性通知", appLabel, null)

        // ---- App 黑白名单 ----
        if (!rules.isForwardingOn(pkg))
            return Decision(null, "此 App 已关", appLabel, null)

        // ---- 空内容 / 脱敏内容过滤 ----
        if (title.isBlank() && content.isBlank())
            return Decision(null, "空内容", appLabel, null)

        // 如果所有文本源都被脱敏了，不要转发无意义内容
        if (isRedacted(content) && isRedacted(title))
            return Decision(null, "内容已脱敏", appLabel, null)

        // ---- 系统噪音包名过滤 ----
        if (pkg in noisePackages && (content.isBlank() || content.contains("clipboard", true) || content.contains("剪贴", true)))
            return Decision(null, "系统噪音通知", appLabel, null)

        // ---- 系统状态消息过滤 ----
        if (isSystemStatusMessage("$title $content $sub"))
            return Decision(null, "系统状态消息", appLabel, null)

        // ---- 近空内容过滤（纯符号、纯空格、仅 app 名等）----
        val trimmedContent = content.trim()
        if (trimmedContent.length < 2 && title.isBlank())
            return Decision(null, "内容过短", appLabel, null)

        // ---- 广告过滤 ----
        if (settings.adFilterEnabled.value && AdFilter.isAd("$title $content $sub", settings.adKeywords.value))
            return Decision(null, "广告过滤", appLabel, null)

        // ---- 去重检查 ----
        val dedupKey = "$pkg|${title.take(80)}|${content.take(80)}"
        val lastTime = dedupCache[dedupKey]
        val now = System.currentTimeMillis()
        if (lastTime != null && (now - lastTime) < DEDUP_WINDOW_MS) {
            return Decision(null, "去重（${now - lastTime}ms 内重复）", appLabel, null)
        }
        dedupCache[dedupKey] = now

        // ---- 验证码提取 ----
        var code: String? = null
        if (settings.codeEnabled.value) {
            code = CodeExtractor.extract("$title $content")
        }

        // ---- 特殊 App 解析 ----
        var msgTitle = title.ifBlank { appLabel }
        var group: String = pkg
        var clickUrl: String? = null
        var important = false
        if (settings.specialEnabled.value) {
            val p = AppParsers.parse(pkg, title, content)
            p.titleOverride?.let { msgTitle = it }
            p.groupKey?.let { group = it }
            clickUrl = p.clickUrl
            important = p.important
        }
        if (code != null) important = true

        if (code != null) {
            msgTitle = title.ifBlank { "验证码" }
        }

        val iconPrefix = settings.iconPrefix.value.trim()
        val icon = if (iconPrefix.isBlank()) null else iconPrefix.trimEnd('/') + "/" + pkg + ".png"
        val level = if (important) "timeSensitive" else "active"

        val msg = BarkMessage(
            deviceKey = settings.currentServer.value?.deviceKey.orEmpty(),
            title = msgTitle.take(100),
            body = content.ifBlank { title }.take(500),
            level = level,
            icon = icon,
            group = group,
            url = clickUrl,
            autoCopy = if (code != null) "1" else null,
            copy = code,
            isArchive = "1",
        )
        return Decision(msg, "转发", appLabel, code)
    }

    // ---- 辅助方法 ----

    /** 检测文本是否被系统脱敏（全部或大部分是 * 号） */
    private fun isRedacted(text: String): Boolean {
        if (text.isBlank()) return false
        val asteriskCount = text.count { it == '*' }
        // 如果 * 占比超过 60%，认为是脱敏内容
        return asteriskCount >= 3 && asteriskCount.toDouble() / text.length > 0.6
    }

    /** 检测是否为系统状态消息（如"短信正在运行"） */
    private fun isSystemStatusMessage(text: String): Boolean {
        return systemStatusPatterns.any { it.containsMatchIn(text) }
    }
}
