package com.base.appzxhy.business.push.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.appzxhy.BuildConfig
import com.base.appzxhy.R
import com.base.appzxhy.bean.FeatureBean.Companion.ANTIVIRUS
import com.base.appzxhy.bean.FeatureBean.Companion.BATTERY_INFO
import com.base.appzxhy.bean.FeatureBean.Companion.JUNK_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.base.appzxhy.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.base.appzxhy.bean.push.NotificationSendBean
import com.base.appzxhy.bean.config.PopupConfigBean
import com.base.appzxhy.ui.splash.SplashActivity
import kotlin.random.Random

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

    private val CHANNEL_ID = "dumpster cleaner Channel"
    private val CHANNEL_NAME = "dumpster cleaner Channel NAME"

    /**
     * 这部分准备UI的代码不能放入IO
     */
    fun setNotification(sendBean: NotificationSendBean): NotificationSendBean {
        val context = sendBean.context
        //弹不同的通知用不同的NotificationId
        if (PopupConfigBean.popupConfigBean.isDifferNotificationId) {
            sendBean.notificationId = Random.nextInt(1000, 2000)
        }
        val intent = Intent(sendBean.context, SplashActivity::class.java)
//        val bean = sendBean.valueMap["bean"] as NotificationInfoBean
        intent.putExtra("actionId", sendBean.actionId)
//        intent.putExtra("linkUrl", bean.linkUrl)
        intent.putExtra("notificationId", sendBean.notificationId)
        sendBean.intent = intent

        val testNotificationId = if (BuildConfig.DEBUG) "${sendBean.notificationId}_${sendBean.actionId}_" else ""
        var debugAction: (remoteViews: RemoteViews) -> Unit = { remoteViews ->
            if (BuildConfig.DEBUG) {
                remoteViews.setTextViewText(R.id.tv_btn, sendBean.where)
            }
        }
        when (sendBean.actionId) {

            JUNK_CLEAN -> {
                val text = testNotificationId + context.getString(R.string.notify_junk_clean)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_clean_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_clean_push)
                    debugAction.invoke(this)
                }
            }

            ANTIVIRUS -> {
                val text = testNotificationId + context.getString(R.string.notif_antivirus)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_antivirus_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_antivirus_push)
                    debugAction.invoke(this)
                }
            }

            BATTERY_INFO -> {
                val text = testNotificationId + context.getString(R.string.notify_battry_info)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_battery_info_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_battery_info_push)
                    debugAction.invoke(this)
                }
            }

            LARGE_FILE_CLEAN -> {
                val text = testNotificationId + context.getString(R.string.notify_large_file)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_large_file_cleaner_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_large_file_cleaner_push)
                    debugAction.invoke(this)
                }
            }

            SIMILAR_PHOTOS -> {
                val text = testNotificationId + context.getString(R.string.notify_similar_photos)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_similar_pictures_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_similar_pictures_push)
                    debugAction.invoke(this)
                }
            }

            SCREENSHOT_CLEAN -> {
                val text = testNotificationId + context.getString(R.string.notify_screenshot)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_screenshot_clean_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_screenshot_clean_push)
                    debugAction.invoke(this)
                }
            }

            PHOTO_COMPRESSION -> {
                val text = testNotificationId + context.getString(R.string.notify_photo_compression)
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_compression_push)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_compression_push)
                    debugAction.invoke(this)
                }
            }

            else -> {

            }
        }

        sendCustomNotification(sendBean)
        return sendBean
    }

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