package com.base.locationsharewhite.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.locationsharewhite.R
import com.base.locationsharewhite.service.StayJobService.Companion.NOTIFICATION_PERMANENT_ID
import com.base.locationsharewhite.ui.main.MainActivity

object StayNotification {

    private var bigDesc = "Location sharing not enable yet"

    private fun customRemoteViews(contentView: RemoteViews, expendView: RemoteViews) {
//        val requestCode1 = Random.nextInt(1800)
//        val intent1 = Intent(context, MyStartActivity::class.java).apply {
//            putExtra("actionId", ConstObject.NOTIFICATION_ACTION_LOG)
//        }
//        val pendingIntent1 =
//            PendingIntent.getActivity(
//                context,
//                requestCode1,
//                intent1,
//                PendingIntent.FLAG_IMMUTABLE
//            )
//        contentView.setOnClickPendingIntent(R.id.fl_1, pendingIntent1)
//        expendView.setOnClickPendingIntent(R.id.fl_1, pendingIntent1)
//
//        val requestCode2 = Random.nextInt(1800)
//        val intent2 = Intent(context, MyStartActivity::class.java).apply {
//            putExtra("actionId", ConstObject.NOTIFICATION_ACTION_DOCUMENT)
//        }
//        val pendingIntent2 =
//            PendingIntent.getActivity(
//                context,
//                requestCode2,
//                intent2,
//                PendingIntent.FLAG_IMMUTABLE
//            )
//        contentView.setOnClickPendingIntent(R.id.ll_2, pendingIntent2)
//        expendView.setOnClickPendingIntent(R.id.ll_2, pendingIntent2)
//
//        val requestCode3 = Random.nextInt(1800)
//        val intent3 = Intent(context, MyStartActivity::class.java).apply {
//            putExtra("actionId", ConstObject.NOTIFICATION_ACTION_BOOKMARK)
//        }
//        val pendingIntent3 =
//            PendingIntent.getActivity(
//                context,
//                requestCode3,
//                intent3,
//                PendingIntent.FLAG_IMMUTABLE
//            )
//        contentView.setOnClickPendingIntent(R.id.ll_3, pendingIntent3)
//        expendView.setOnClickPendingIntent(R.id.ll_3, pendingIntent3)
//
//
//        val requestCode4 = Random.nextInt(1800)
//        val intent4 = Intent(context, MyStartActivity::class.java).apply {
//            putExtra("actionId", ConstObject.NOTIFICATION_ACTION_NEW_IMAGE_PDF)
//        }
//        val pendingIntent4 =
//            PendingIntent.getActivity(
//                context,
//                requestCode4,
//                intent4,
//                PendingIntent.FLAG_IMMUTABLE
//            )
//        contentView.setOnClickPendingIntent(R.id.ll_4, pendingIntent4)
//        expendView.setOnClickPendingIntent(R.id.ll_4, pendingIntent4)

        expendView.setTextViewText(R.id.tv_desc, bigDesc)

    }

    fun notifyStayNotification(context: Context, desc: String) {
        bigDesc = desc

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

        notificationManager.notify(
            NOTIFICATION_PERMANENT_ID,
            createPermanentNotification(context)
        )

    }

    fun createPermanentNotification(context: Context): Notification {

        val channelName = "Permanent Foreground Service Channel"
        val channelId = "permanent_channel"
        val contentView = RemoteViews(context.packageName, R.layout.stay_notification_small_location)
        val expendView = RemoteViews(context.packageName, R.layout.stay_notification_big_location)


        customRemoteViews(contentView, expendView)


        val builder = NotificationCompat.Builder(context, channelId)
        val smallIcon = IconCompat.createFromIcon(
            context, Icon.createWithResource(
                context, R.drawable.location_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()
    }


}