package com.base.scanqrclear.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
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.scanqrclear.R
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_STAY_SCAN
import com.base.scanqrclear.service.StayJobService.Companion.NOTIFICATION_STAY_ID
import com.base.scanqrclear.ui.main.MainActivity
import com.base.scanqrclear.ui.start.StartActivity
import kotlin.random.Random

object StayNotification {


    private fun customRemoteViews(context: Context, contentView: RemoteViews, expendView: RemoteViews) {
        val requestCode1 = Random.nextInt(1800)
        val intent1 = Intent(context, StartActivity::class.java).apply {
            putExtra("actionId", ACTION_ID_STAY_SCAN)
        }
        val pendingIntent1 =
            PendingIntent.getActivity(
                context,
                requestCode1,
                intent1,
                PendingIntent.FLAG_IMMUTABLE
            )
        contentView.setOnClickPendingIntent(R.id.ivQR, pendingIntent1)
        contentView.setOnClickPendingIntent(R.id.ivTime, pendingIntent1)
        contentView.setOnClickPendingIntent(R.id.llScan, pendingIntent1)
        expendView.setOnClickPendingIntent(R.id.ivQR, pendingIntent1)
        expendView.setOnClickPendingIntent(R.id.ivTime, pendingIntent1)
        expendView.setOnClickPendingIntent(R.id.llScan, pendingIntent1)
    }

    fun notifyStayNotification(context: Context, desc: String) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(NOTIFICATION_STAY_ID, createPermanentNotification(context))
    }

    fun createPermanentNotification(context: Context): Notification {

        val channelName = "Stay Foreground Service Channel"
        val channelId = "Stay_channel"
        val contentView = RemoteViews(context.packageName, R.layout.stay_notification_small_qr)
        val expendView = RemoteViews(context.packageName, R.layout.stay_notification_big_qr)


        customRemoteViews(context, contentView, expendView)


        val builder = NotificationCompat.Builder(context, channelId)
        val smallIcon = IconCompat.createFromIcon(
            context, Icon.createWithResource(
                context, R.drawable.log_svg
            )
        )

        smallIcon?.let {
            builder.setSmallIcon(smallIcon) //设置状态栏内的小图标
        }

        val nfIntent = Intent(context, MainActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)
        builder.setLargeIcon(
            BitmapFactory.decodeResource(
                context.resources,
                R.mipmap.logo
            )
        )
        builder.setContentTitle(context.resources.getString(R.string.app_name))
        builder.setContentIntent(pendingIntent) //设置PendingIntent
        builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
        builder.setAutoCancel(false)
        builder.setOngoing(true)
        builder.setPriority(NotificationCompat.PRIORITY_MAX) //优先级为：重要通知
        builder.setWhen(System.currentTimeMillis())
        builder.setCustomContentView(contentView)
        builder.setCustomBigContentView(expendView)
        builder.setCustomHeadsUpContentView(expendView)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
            channel.lockscreenVisibility = 1
            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
            builder.setChannelId(channelId)
        }
        return builder.build()
    }


}