package com.zxhyis.toolss.common.notification

import android.app.Activity
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.provider.Settings
import android.widget.RemoteViews
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.zxhyis.toolss.R
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 OpenNotificationsUtil {
    const val OPEN_APP_NOTIFICATION = 213
    const val OPEN_LISTENER_SETTINGS = 251
    const val OPEN_SERVICE_NOTIFICATION_ID = 67
    const val OPEN_MESSAGE_NOTIFICATION_ID = 8

    /**
     * 是否开启APP通知
     *
     * @param context
     * @return
     */
    fun isNotificationEnabledForApp(context: Context?): Boolean {
        return NotificationManagerCompat.from(context!!).areNotificationsEnabled()
    }

    /**
     * 开启APP通知
     *
     * @param context
     */
    fun openNotificationSettingsForApp(context: Activity) {
        try {
            // 锤子不能单个打开自己APP的 通知设置 界面
            if (Build.BRAND.equals("SMARTISAN", ignoreCase = true)) {
                Toast.makeText(
                    context,
                    "锤子手机请手动打开 设置--通知中心--开启IMFitPro通知开关",
                    Toast.LENGTH_LONG
                ).show()
                return
            }
            // Links to this app's notification settings.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8.0
                val intent = Intent()
                intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                intent.putExtra("android.provider.extra.APP_PACKAGE", context.packageName)
                intent.putExtra("app_package", context.packageName)
                intent.putExtra("app_uid", context.applicationInfo.uid)
                context.startActivityForResult(intent, OPEN_APP_NOTIFICATION)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 是否开启通知使用权限
     *
     * @param context
     * @return
     */
    fun isNotificationListenerEnabled(context: Context): Boolean {
        try {
            val packageNames = NotificationManagerCompat.getEnabledListenerPackages(context)
            if (packageNames.contains(context.packageName)) {
                return true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 开启通知使用权限
     */
    fun openNotificationListenSettings(context: Activity) {
        try {
            val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
            context.startActivityForResult(intent, OPEN_LISTENER_SETTINGS)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 显示通知栏通知
     *
     * @param context
     * @param title
     * @param text
     * @param type    0服务常驻通知， 1普通消息通知
     * @return
     */
    fun createNotification(
        context: Context,
        type: Int
    ): Notification {
        var isSilent = true //是否静音
        val isOngoing = true //是否持续(为不消失的常驻通知)
        val channelName = "服务常驻通知"
        val channelId = "Service_Id"
        val category = Notification.CATEGORY_SERVICE
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val contentView = RemoteViews(context.packageName, R.layout.reminder_layout_notification)
        val expendView = RemoteViews(context.packageName, R.layout.reminder_layout_notification_big)

        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 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 intent3 = Intent()
        val serviceComponent = ComponentName(context, FlashlightService::class.java)
        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 nfIntent = Intent(context, MainActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)

        val builder = NotificationCompat.Builder(context, channelId)
            .setCustomContentView(contentView)
            .setCustomBigContentView(expendView)
            .setContentIntent(pendingIntent) //设置PendingIntent
            .setSmallIcon(R.mipmap.logo_small) //设置状态栏内的小图标
            .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
            .setOngoing(isOngoing) //设置持续(不消失的常驻通知)
            .setCategory(category) //设置类别
            .setPriority(NotificationCompat.PRIORITY_LOW) //优先级为：重要通知
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //安卓8.0以上系统要求通知设置Channel,否则会报错
            val notificationChannel =
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
            notificationChannel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC //锁屏显示通知
            notificationManager.createNotificationChannel(notificationChannel)
            builder.setChannelId(channelId)
        }
        val notification: Notification = builder.build() //获取构建好的Notification
//        if (type == 1) { //普通消息通知，更新通知
//            notificationManager.notify(OPEN_MESSAGE_NOTIFICATION_ID, notification)
//        }
        return notification
    }
}