package com.zxhyis.toolss.notification

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.zxhyis.toolss.FileApp
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.EventHelper
import com.zxhyis.toolss.ad.PushStrategy
import com.zxhyis.toolss.ad.bean.EventParams
import com.zxhyis.toolss.ad.bean.PushCfg
import com.zxhyis.toolss.logic.LogEx
import com.zxhyis.toolss.logic.RamMemoryEx
import com.zxhyis.toolss.notification.NotificationHelper.postActionNotification
import com.zxhyis.toolss.notification.broadcast.CloseNotificationReceiver
import com.zxhyis.toolss.ui.home.MainActivity
import com.zxhyis.toolss.ui.page.BatteryInfoActivity
import com.zxhyis.toolss.ui.page.PrepareScanActivity
import com.zxhyis.toolss.ui.page.VirusActivity
import com.zxhyis.toolss.ui.page.appmanager.AppManager2Activity
import com.zxhyis.toolss.ui.start.FlashlightService

object NotificationUtils {

    private val TAG = "com.zxhyis.toolss.notification.NotificationUtils"
    const val NOTIFICATION_TAG = "FileManagerNotification"
    private const val CHANNEL_ID = "Misc"

    fun showNotification(
        context: Context,
        notificationId: Int,
        remoteViews: RemoteViews,
        closeViewId: Int,
        pendingIntent: PendingIntent,
        delay: Long
    ) {
        if (closeViewId != 0) {
            val closeIntent = Intent(context, CloseNotificationReceiver::class.java).apply {
                putExtra("notificationId", notificationId)
            }
            val closePendingIntent =
                PendingIntent.getBroadcast(context, 0, closeIntent, PendingIntent.FLAG_MUTABLE)
            remoteViews.setOnClickPendingIntent(closeViewId, closePendingIntent)
        }
        val builder = createNotificationBuilder(context, remoteViews, pendingIntent, delay)
        sendNotification(context, notificationId, builder)
    }

    private fun createNotificationBuilder(
        context: Context,
        remoteViews: RemoteViews,
        pendingIntent: PendingIntent,
        delay: Long
    ): NotificationCompat.Builder {
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            notificationManager.createNotificationChannel(channel)
        }

        val deleteIntent = Intent(context, CloseNotificationReceiver::class.java).apply {
            action = "DELETE_NOTIFICATION"
        }
        val deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_MUTABLE)

        return NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.mipmap.logo_small)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setOnlyAlertOnce(false)
            .setDeleteIntent(deletePendingIntent)
            .setContentIntent(pendingIntent)
            .setContent(remoteViews)
            .setCustomHeadsUpContentView(remoteViews)
            .setCustomBigContentView(remoteViews)
            .setCustomContentView(remoteViews)
            .setStyle(NotificationCompat.BigTextStyle())
            .setFullScreenIntent(pendingIntent, true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setTimeoutAfter(delay)
            .setLocalOnly(true)
            .setOngoing(true)
            .setSound(null)
            .setLights(0, 0, 0)
            .setVibrate(null)
            .setAllowSystemGeneratedContextualActions(true)
            .setBubbleMetadata(null)
    }

    private fun sendNotification(
        context: Context,
        notificationId: Int,
        builder: NotificationCompat.Builder
    ) {
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notification = builder.build()
        notificationManager.notify(NOTIFICATION_TAG, notificationId, notification)
    }


    fun createPermanentNotification(context: Context): Notification {

        val isOngoing = true //是否持续(为不消失的常驻通知)
        val channelName = "Foreground Service Channel"
        val channelId = "Service_Id"
        val category = Notification.CATEGORY_SERVICE
        val contentView = RemoteViews(context.packageName, R.layout.reminder_layout_notification)
        val expendView = RemoteViews(context.packageName, R.layout.reminder_layout_notification_big)

        val intent1 = Intent(context, VirusActivity::class.java)
        val pendingIntent1 =
            PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.id_ll_virus, pendingIntent1)

        val intent2 = Intent(context, BatteryInfoActivity::class.java)
        val pendingIntent2 =
            PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.id_battery, pendingIntent2)
        val serviceComponent = ComponentName(context, FlashlightService::class.java)

        val intent3 = Intent()
        intent3.component = serviceComponent
        val pendingIntent3 =
            PendingIntent.getService(context, 0, intent3, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.id_lighit, pendingIntent3)
        expendView.setOnClickPendingIntent(R.id.id_lighit, pendingIntent3)
        val intent4 = Intent(context, AppManager2Activity::class.java)
        val pendingIntent4 =
            PendingIntent.getActivity(context, 0, intent4, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.id_app_manager, pendingIntent4)

        val intent0 = Intent(context, PrepareScanActivity::class.java)
        val pendingIntent0 =
            PendingIntent.getActivity(context, 0, intent0, PendingIntent.FLAG_IMMUTABLE)
        contentView.setOnClickPendingIntent(R.id.id_ll_clean, pendingIntent0)

        val nfIntent = Intent(context, MainActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)


        val builder = NotificationCompat.Builder(context, channelId)


        builder.setCustomContentView(contentView)
        builder.setCustomBigContentView(expendView).setCustomContentView(contentView)
        builder.setCustomBigContentView(expendView)
        builder.setContentIntent(pendingIntent) //设置PendingIntent
        builder.setSmallIcon(R.mipmap.logo_small) //设置状态栏内的小图标
        builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
        builder.setOngoing(isOngoing) //设置持续(不消失的常驻通知)
//        builder.setCategory(category) //设置类别
        builder.setAutoCancel(false)
        builder.setPriority(NotificationCompat.PRIORITY_MAX) //优先级为：重要通知

        builder.setWhen(System.currentTimeMillis())
        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)
        }

        val notification = builder.build()

        return notification
    }

    fun sendTimerPush(eventParams: EventParams?) {
        LogEx.logDebug(TAG, "sendTimerPush")
        val id = NotificationHelper.getPresentPushId()
        val isPush = PushStrategy.isPush(id, eventParams = eventParams)
        val log = "isPush=$isPush " + "id=${id} "
        LogEx.logDebug(TAG, log)
        EventHelper.event("pushCircleOrder_isPush", log, eventParams = eventParams)
        if (isPush) {
            var extra: Int? = null
            if (id == PushCfg.ID_PHONE_ACCELERATE) {
                extra = RamMemoryEx.getMemoryUsage(FileApp.fContext).toInt()
            }
            FileApp.fContext.postActionNotification(id, eventParams, extra)
        }
    }
}
