package com.tgv.malware.business.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.tgv.malware.R
import com.tgv.malware.bean.FeatureBean.Companion.ANTIVIRUS
import com.tgv.malware.bean.FeatureBean.Companion.BATTERY_INFO
import com.tgv.malware.bean.FeatureBean.Companion.JUNK_CLEAN
import com.tgv.malware.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.tgv.malware.bean.FeatureBean.Companion.getFunctionTodayUsed
import com.tgv.malware.ui.main.MainActivity
import com.tgv.malware.ui.splash.SplashActivity
import kotlin.random.Random

object StayNotification {


    private fun customRemoteViews(context: Context, contentView: RemoteViews, expendView: RemoteViews) {


        val img1 = if (getFunctionTodayUsed(JUNK_CLEAN)) R.drawable.icon_clean_push_s else R.drawable.icon_clean_push_s_r
        val img2 = if (getFunctionTodayUsed(ANTIVIRUS)) R.drawable.icon_antivirus_push_s else R.drawable.icon_antivirus_push_s_r
        val img3 = if (getFunctionTodayUsed(BATTERY_INFO)) R.drawable.icon_battery_push_s else R.drawable.icon_battery_push_s_r
        val img4 =
            if (getFunctionTodayUsed(LARGE_FILE_CLEAN)) R.drawable.icon_large_file_clean_push_s else R.drawable.icon_large_file_clean_push_s_r

        contentView.setImageViewResource(R.id.iv1, img1)
        contentView.setImageViewResource(R.id.iv2, img2)
        contentView.setImageViewResource(R.id.iv3, img3)
        contentView.setImageViewResource(R.id.iv4, img4)

        expendView.setImageViewResource(R.id.iv1, img1)
        expendView.setImageViewResource(R.id.iv2, img2)
        expendView.setImageViewResource(R.id.iv3, img3)
        expendView.setImageViewResource(R.id.iv4, img4)


        arrayOf(R.id.ll1, R.id.ll2, R.id.ll3, R.id.ll4).forEach {
            val requestCode = Random.nextInt(1800)
            val intent = Intent(context, SplashActivity::class.java).apply {
                if (it == R.id.ll1) {
                    putExtra("actionId", JUNK_CLEAN)
                }
                if (it == R.id.ll2) {
                    putExtra("actionId", ANTIVIRUS)
                }
                if (it == R.id.ll3) {
                    putExtra("actionId", BATTERY_INFO)
                }
                if (it == R.id.ll4) {
                    putExtra("actionId", LARGE_FILE_CLEAN)
                }
            }
            val pendingIntent =
                PendingIntent.getActivity(
                    context,
                    requestCode,
                    intent,
                    PendingIntent.FLAG_IMMUTABLE
                )
            contentView.setOnClickPendingIntent(it, pendingIntent)
            expendView.setOnClickPendingIntent(it, pendingIntent)
        }
    }


    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)
        val expendView = RemoteViews(context.packageName, R.layout.stay_notification_big)

        customRemoteViews(context, contentView, expendView)

        val builder = NotificationCompat.Builder(context, channelId)
        val smallIcon = IconCompat.createFromIcon(
            context, Icon.createWithResource(
                context, R.mipmap.logo
            )
        )

        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)
        builder.setNumber(0)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
            channel.lockscreenVisibility = 1
            channel.setShowBadge(false)
            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
            builder.setChannelId(channelId)
        }
        return builder.build()
    }


}