package com.jiooption.trading.firebase

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.jiooption.trading.MainActivity
import com.jiooption.trading.R
import com.jiooption.trading.utils.Constants

/**
 * Notification channel creation and notification display helper
 */
object NotificationHelper {

    private var notificationId = 0

    /**
     * Create all notification channels (call once at app startup)
     */
    fun createNotificationChannels(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val manager = context.getSystemService(NotificationManager::class.java)

            val channels = listOf(
                Triple(Constants.CHANNEL_DEFAULT, "General", NotificationManager.IMPORTANCE_DEFAULT),
                Triple(Constants.CHANNEL_TRADES, "Trade Alerts", NotificationManager.IMPORTANCE_HIGH),
                Triple(Constants.CHANNEL_DEPOSITS, "Deposit Updates", NotificationManager.IMPORTANCE_HIGH),
                Triple(Constants.CHANNEL_WITHDRAWALS, "Withdrawal Updates", NotificationManager.IMPORTANCE_DEFAULT),
                Triple(Constants.CHANNEL_PROMO, "Promotions", NotificationManager.IMPORTANCE_LOW)
            )

            channels.forEach { (id, name, importance) ->
                val channel = NotificationChannel(id, name, importance).apply {
                    description = "$name notifications from BinaryPro"
                    enableVibration(importance >= NotificationManager.IMPORTANCE_DEFAULT)
                    if (importance >= NotificationManager.IMPORTANCE_HIGH) {
                        enableLights(true)
                        lightColor = context.getColor(R.color.accent_green)
                    }
                }
                manager.createNotificationChannel(channel)
            }
        }
    }

    /**
     * Show a local notification
     */
    fun showNotification(
        context: Context,
        title: String,
        body: String,
        channelId: String = Constants.CHANNEL_DEFAULT,
        data: Map<String, String> = emptyMap()
    ) {
        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Create intent to open app on tap
        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
            data.forEach { (key, value) ->
                putExtra(key, value)
            }
        }

        val pendingIntent = PendingIntent.getActivity(
            context,
            notificationId,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        // Choose icon tint based on notification type
        val iconColor = when (channelId) {
            Constants.CHANNEL_TRADES -> context.getColor(R.color.accent_blue)
            Constants.CHANNEL_DEPOSITS -> context.getColor(R.color.accent_green)
            Constants.CHANNEL_WITHDRAWALS -> context.getColor(R.color.accent_yellow)
            Constants.CHANNEL_PROMO -> context.getColor(R.color.accent_purple)
            else -> context.getColor(R.color.accent_green)
        }

        val notification = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(body)
            .setColor(iconColor)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setStyle(NotificationCompat.BigTextStyle().bigText(body))
            .build()

        manager.notify(notificationId++, notification)
    }
}
