package com.example.mydemo.strategy.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Icon
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import com.base.locationsharewhite.helper.ConstConfig
import com.example.mydemo.R
import kotlin.random.Random

/**
 *通知管理类
 */
object NotificationMgr {

    fun init(context: Context) {
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //创建channel
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                ConstConfig.CHANNEL_ID,
                ConstConfig.CHANNEL_NAME,
                NotificationManager.IMPORTANCE_HIGH
            )
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            channel.enableLights(false)
            channel.enableVibration(false)
            channel.vibrationPattern = longArrayOf(0)
            channel.setSound(null, null)
            notificationManager.createNotificationChannel(channel)
        }
    }

    fun sendNotification(
        context: Context, intent: Intent,
        bigRemoteViews: RemoteViews,
        smallRemoteViews: RemoteViews
    ) {
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        // Create the notification
        val builder: NotificationCompat.Builder =
            NotificationCompat.Builder(context, ConstConfig.CHANNEL_ID)

        //设置状态栏内的小图标
        val smallIcon = IconCompat.createFromIcon(
            context, Icon.createWithResource(
                context, ConstConfig.NOTIFICATION_LOGO
            )
        )
        smallIcon?.let {
            builder.setSmallIcon(smallIcon)
        }
        builder.setContentTitle(context.resources.getString(R.string.app_name))
            .setContentText("notification")

        val requestCode = Random.nextInt(1000)
        val pendingIntent =
            PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)
        val deleteIntent =
            PendingIntent.getBroadcast(
                context,
                requestCode,
                Intent(context, DeleteNotificationReceiver::class.java),
                PendingIntent.FLAG_IMMUTABLE
            )
        builder.setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)
            .setVibrate(longArrayOf(0))
            .setSound(null)
            .setDeleteIntent(deleteIntent)
        // 设置小视图
        builder.setCustomContentView(smallRemoteViews)

        // 设置悬浮通知视图（Android 12 及以上）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            builder.setCustomHeadsUpContentView(bigRemoteViews)
        }

        // 设置大视图
        builder.setCustomBigContentView(bigRemoteViews)

        notificationManager.notify(ConstConfig.NOTIFICATION_ID, builder.build())
    }
}