package com.xm.test.myfilemaster.notification

import android.annotation.SuppressLint
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.Color
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.xm.test.myfilemaster.MyApplication
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.activity.HomeActivity
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_BATTERY_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_BIGFILE_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_BOOST_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_CHARGE
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_COOL_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_DUPLICATE_FILE_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_INSTALL_PACKAGE_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_JUNK_CLEAN_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_LOW_BATTERY_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_LOW_JUNK_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_PHOTO_CLEAN_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_UNINSTALL_PACKAGE_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_VIDEO_CLEAN_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_VIRUS_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_WIFI_PUSH
import com.xm.test.myfilemaster.ad.createByActionId
import com.xm.test.myfilemaster.ad.saveDataStoreBlock
import kotlin.random.Random

/**
 * 动态申请 <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
 */
object NotificationHelper {


    private const val CHANNEL_ID = "msjdhusjdsd"
    private const val CHANNEL_NAME = "File Manager Select"

    @SuppressLint("RemoteViewLayout")
    fun Context.postNotification(actionId: Int) {

//        Log.d("glc", "actionId:" + actionId)
        val intent = Intent(applicationContext, HomeActivity::class.java)
        var remoteViews: RemoteViews

        var myValue = 0
        when (actionId) {
            ID_JUNK_CLEAN_PUSH -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_junk_clean)
            }

            ID_BOOST_PUSH -> {
                return
            }

            ID_VIRUS_PUSH -> {
                return
            }

            ID_BATTERY_PUSH -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_battery)
            }

            ID_COOL_PUSH -> {
                return
            }

            ID_BIGFILE_PUSH -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_large_file)
            }

            ID_DUPLICATE_FILE_PUSH -> {
                return
            }

            ID_VIDEO_CLEAN_PUSH -> {
                return
            }

            ID_PHOTO_CLEAN_PUSH -> {
                return
            }
//==================================下面是被动推送的情况===============================================
            ID_WIFI_PUSH -> {
                return
            }

            ID_INSTALL_PACKAGE_PUSH -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_install)
            }

            ID_UNINSTALL_PACKAGE_PUSH -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_uninstall)
            }

            ID_CHARGE -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_battery)
            }

            ID_LOW_BATTERY_PUSH -> {
                remoteViews = RemoteViews(packageName, R.layout.notification_battery_low)
            }

            ID_LOW_JUNK_PUSH -> {
                return
            }

            else -> {
                return
            }
        }
        myValue = actionId
        val customKey = "type"
        intent.putExtra(customKey, myValue)
        MyApplication.fContext.saveDataStoreBlock(createByActionId(actionId), System.currentTimeMillis())
        showNotification(applicationContext, intent, remoteViews)
    }

    //创建通知通道
    private fun createNotificationChannel(
        context: Context,
        channelId: String = CHANNEL_ID,
        channelName: String = CHANNEL_NAME,
    ) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val channel = NotificationChannel(channelId, channelName, importance).apply {
                enableLights(true)
                enableVibration(false)
                vibrationPattern = longArrayOf(0)
                lightColor = Color.RED
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    @SuppressLint("MissingPermission")
    //Manifest.permission.POST_NOTIFICATIONS
    fun showNotification(context: Context, intent: Intent, remoteViews: RemoteViews) {

        createNotificationChannel(context)

        val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.FLAG_MUTABLE
        } else {
            PendingIntent.FLAG_UPDATE_CURRENT
        }


        //requestCode每次需要改变
        val requestCode = Random.nextInt(0, 1000)
        val pendingIntent = PendingIntent.getActivity(
            context, requestCode, intent, flag
        )

        val builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo24)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_logo24))

            .setCustomContentView(remoteViews)
            .setCustomHeadsUpContentView(remoteViews)
            .setCustomBigContentView(remoteViews)
            .setContent(remoteViews)

            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setColor(Color.TRANSPARENT)
            .setAutoCancel(true)
            .setOngoing(true)
            .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
            .setVibrate(longArrayOf(0))
            .setContentIntent(pendingIntent)


        val notificationId = Random.nextInt(0, 1200)
        with(NotificationManagerCompat.from(context)) {
            notify(notificationId, builder.build())
        }
    }
}