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

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

    private val CHANNEL_ID = "Super Easy Clean Channel"
    private val CHANNEL_NAME = "Super Easy Clean 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 + "Clean up the junk files on your phone now!"
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    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)
                    debugAction.invoke(this)
                }
            }

            BATTERY_INFO -> {
//                val text = testNotificationId + "Check your phone battery consumption recently!"
//                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
//                    setTextViewText(R.id.tv1, text)
//                    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)
//                    debugAction.invoke(this)
//                }
                val contentView = RemoteViews(context.packageName, R.layout.layout_battery_stay_small)
                val expendView = RemoteViews(context.packageName, R.layout.layout_battery_stay_large)
                BatteryNotification.customRemoteViews(context,contentView,expendView)
                sendBean.smallRemoteViews= contentView
                sendBean.bigRemoteViews=expendView
            }

            LARGE_FILE_CLEAN -> {
                val text = testNotificationId + "Clear out large files to free up your storage space!"
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    debugAction.invoke(this)
                }
                sendBean.bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_large).apply {
                    setTextViewText(R.id.tv1, text)
                    debugAction.invoke(this)
                    setImageViewResource(R.id.ivIcon, R.drawable.icon_file_clean_result)
                }
            }

            SIMILAR_PHOTOS -> {
                val text = testNotificationId + "Clean Up Similar Photos – Save Space!"
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    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_photos_clean_result)
                    debugAction.invoke(this)
                }
            }

            SCREENSHOT_CLEAN -> {
                val text = testNotificationId + "Free up space by clearing screenshot clutter!"
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    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_2)
                    debugAction.invoke(this)
                }
            }

            PHOTO_COMPRESSION -> {
                val text = testNotificationId + "Free up your phone storage space by compressing photos."
                sendBean.smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small).apply {
                    setTextViewText(R.id.tv1, text)
                    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)
                    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.mipmap.ic_launcher
            )
        )
        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())
    }
}