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_EMAIL
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_SCAN
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_TEXT
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_WEBSITE
import com.base.scanqr.bean.NotificationSendBean.Companion.ACTION_ID_WIFI
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)
            }

            ACTION_ID_WIFI -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_wifi)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own wifi QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_wifi)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create wifi QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own wifi QR cord and share it!")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_TEXT -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_text)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own text QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_text)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create text QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own text QR cord and share it!")
                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_EMAIL -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_email)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own email QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_email)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create email QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own email QR cord and share it!")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_WEBSITE -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_website)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own website QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_email)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create website QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own website QR cord and share it!")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

        }

        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.drawable.log_svg
            )
        )
        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())
    }
}