package ru.forbion.f7cloud.core.push

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat

object F7PushNotificationHelper {
    fun show(
        context: Context,
        title: String,
        body: String,
        openUrl: String?,
        highPriority: Boolean,
        type: String?,
        channelHint: String? = null,
        roomToken: String? = null,
        messageId: String? = null,
    ) {
        F7NotificationChannels.ensureAll(context)
        val manager = context.getSystemService(NotificationManager::class.java) ?: return
        val launch = context.packageManager.getLaunchIntentForPackage(context.packageName)
        val pending = if (launch != null) {
            val intent = Intent(launch)
            if (!openUrl.isNullOrBlank()) {
                intent.putExtra(PushIntents.EXTRA_OPEN_URL, openUrl)
            }
            if (!roomToken.isNullOrBlank()) {
                intent.putExtra(PushIntents.EXTRA_ROOM_TOKEN, roomToken)
            }
            if (!messageId.isNullOrBlank()) {
                intent.putExtra(PushIntents.EXTRA_MESSAGE_ID, messageId)
            }
            PendingIntent.getActivity(
                context,
                1001,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
            )
        } else {
            null
        }
        val channel = F7NotificationChannels.resolveChannel(channelHint, highPriority || type == "call")
        val priority = if (channel == F7NotificationChannels.CALLS) {
            NotificationCompat.PRIORITY_HIGH
        } else {
            NotificationCompat.PRIORITY_DEFAULT
        }
        val iconRes = context.applicationInfo.icon.takeIf { it != 0 }
            ?: android.R.drawable.stat_notify_chat
        // На локскрине — ОБЕЗЛИЧЕННАЯ версия (минимум информации, решение владельца): отправитель
        // и текст не утекают на заблокированном экране. Полный контент — после разблокировки.
        val genericText = when (type) {
            "chat", "room", "message" -> "Новое сообщение"
            "call" -> "Входящий звонок"
            else -> "Новое уведомление"
        }
        val publicVersion = NotificationCompat.Builder(context, channel)
            .setSmallIcon(iconRes)
            .setContentTitle("F7cloud")
            .setContentText(genericText)
            .setAutoCancel(true)
            .setPriority(priority)
            .setContentIntent(pending)
            .build()
        val notification = NotificationCompat.Builder(context, channel)
            .setSmallIcon(iconRes)
            .setContentTitle(title)
            .setContentText(body)
            .setStyle(NotificationCompat.BigTextStyle().bigText(body))
            .setAutoCancel(true)
            .setPriority(priority)
            .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
            .setPublicVersion(publicVersion)
            .setContentIntent(pending)
            .build()
        manager.notify((System.currentTimeMillis() % Int.MAX_VALUE).toInt(), notification)
    }
}
