package ru.forbion.f7cloud.core.push

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.media.AudioAttributes
import android.media.RingtoneManager
import android.os.Build

object F7NotificationChannels {
    const val MESSAGES = "f7_mobile_messages"
    /** Silent channel: ringtone is played only by [F7IncomingCallRinger]. */
    const val CALLS = "f7_mobile_calls_v3"

    /** Channel IDs referenced in FCM payloads from f7push server (background tray). */
    private const val SERVER_MESSAGES = "f7cloud_messages_v2"
    private const val SERVER_CALLS = "f7cloud_calls_v2"

    fun ensureAll(context: Context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
        val manager = context.getSystemService(NotificationManager::class.java) ?: return
        val audio = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build()
        val notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val ringtoneAudio = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build()
        val ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)

        fun create(
            id: String,
            name: String,
            importance: Int,
            vibration: LongArray,
            sound: android.net.Uri?,
            soundAttrs: AudioAttributes,
        ) {
            manager.createNotificationChannel(
                NotificationChannel(id, name, importance).apply {
                    description = name
                    enableLights(true)
                    enableVibration(true)
                    vibrationPattern = vibration
                    if (sound != null) {
                        setSound(sound, soundAttrs)
                    }
                    setShowBadge(true)
                    // Приватно на локскрине: система покажет public-версию (generic), а не полный
                    // контент (минимум информации, решение владельца). Public-версия задаётся
                    // per-notification в F7PushNotificationHelper / F7IncomingCallQueue.
                    lockscreenVisibility = android.app.Notification.VISIBILITY_PRIVATE
                },
            )
        }

        val messagesName = context.getString(R.string.notification_channel_messages)
        val callsName = context.getString(R.string.notification_channel_calls)
        val msgVibration = longArrayOf(0, 250, 150, 250)
        val callVibration = longArrayOf(0, 500, 200, 500)

        create(MESSAGES, messagesName, NotificationManager.IMPORTANCE_HIGH, msgVibration, notificationSound, audio)
        create(SERVER_MESSAGES, messagesName, NotificationManager.IMPORTANCE_HIGH, msgVibration, notificationSound, audio)
        create(CALLS, callsName, NotificationManager.IMPORTANCE_HIGH, longArrayOf(0), null, audio)
        create(SERVER_CALLS, callsName, NotificationManager.IMPORTANCE_HIGH, longArrayOf(0), null, audio)
    }

    fun resolveChannel(channelHint: String?, highPriority: Boolean): String {
        if (channelHint == CALLS || channelHint == SERVER_CALLS) return CALLS
        if (channelHint == MESSAGES || channelHint == SERVER_MESSAGES) return MESSAGES
        return if (highPriority) CALLS else MESSAGES
    }
}
