package com.base.scanqr.fcm.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.scanqr.MyApplication
import com.base.scanqr.R
import com.base.scanqr.bean.NotificationSendBean
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_SCAN
import com.base.scanqr.ui.start.StartActivity
import kotlin.random.Random

/**
 * 发送通知UI部分
 */
object NotificationUiUtil {

    private val CHANNEL_ID = "San Qr Channel"
    private val CHANNEL_NAME = "San Qr Channel NAME"

    fun setNotification(sendBean: NotificationSendBean) {
        when (sendBean.actionId) {
            ACTION_ID_SCAN -> {
                sendBean.bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_scan)
                sendBean.smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_scan)

                val intent = Intent(sendBean.context, StartActivity::class.java)
                intent.putExtra("actionId", sendBean.actionId)
                sendBean.intent = intent
            }
        }

        sendCustomNotification(sendBean)
    }

    private fun sendCustomNotification(
        sendBean: NotificationSendBean
    ) {

        val notificationManager = sendBean.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val context = sendBean.context

        //创建channel
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH
            )
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            channel.enableVibration(false)
            channel.vibrationPattern = longArrayOf(0)
            notificationManager.createNotificationChannel(channel)
        }


        // Create the notification
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, CHANNEL_ID)

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

        val requestCode = Random.nextInt(1000)
        val pendingIntent = PendingIntent.getActivity(context, requestCode, sendBean.intent, PendingIntent.FLAG_IMMUTABLE)
        builder.setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)

        var small: RemoteViews? = sendBean.bigRemoteViews
        //Android 12以下需要适配小RemoteViews
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            small = sendBean.smallRemoteViews
        }
        // 设置小视图
        sendBean.smallRemoteViews?.let { builder.setCustomContentView(it) }

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

        // 设置大视图
        sendBean.bigRemoteViews?.let { builder.setCustomBigContentView(it) }

        notificationManager.notify(sendBean.notificationId, builder.build())
    }
}