package com.convenient.android.common.utils.systemui

import android.annotation.TargetApi
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.ContextWrapper
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.core.app.NotificationCompat
import com.convenient.android.common.R


/**
 * @classname：NotificationUtils
 * @author：liujiyuan
 * @date：2019-10-17 15:28
 * @description：通知管理类
 */
class NotificationUtils(base: Context?, id: Int) : ContextWrapper(base) {

    private var CHANNEL_ID = "default"
    private var mManager: NotificationManager? = null
    private var notification_id = 0


    companion object {
        private const val CHANNEL_NAME = "Default Channel"
    }

    init {
        CHANNEL_ID = id.toString()
        notification_id = id
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel() {
        val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW)
        //是否绕过请勿打扰模式
        channel.canBypassDnd()
        //闪光灯
        channel.enableLights(true)
        //锁屏显示通知
        channel.lockscreenVisibility = Notification.VISIBILITY_SECRET
        //闪关灯的灯光颜色
        channel.lightColor = Color.GREEN
        //桌面launcher的消息角标
        channel.canShowBadge()
        //是否允许震动
        channel.enableVibration(true)
        //获取系统通知响铃声音的配置
//        channel.getAudioAttributes();
        //获取通知取到组
        channel.group
        //设置可绕过  请勿打扰模式
        channel.setBypassDnd(true)
        //设置震动模式
//        channel.setVibrationPattern(new long[]{100, 100, 200});
        //是否会有灯光
        channel.shouldShowLights()
        //设置静音
        channel.setSound(null, null)
        manager?.createNotificationChannel(channel)
    }

    private val manager: NotificationManager?
        get() {
            if (mManager == null) {
                mManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            }
            return mManager
        }

    /**
     * 发送通知
     */
    fun sendNotification(title: String, content: String, @DrawableRes smallIcon: Int) {
        val builder = getNotification(title, content, smallIcon)
        manager?.notify(notification_id, builder.build())
    }

    private fun getNotification(title: String, content: String, @DrawableRes smallIcon: Int): NotificationCompat.Builder {
        var builder: NotificationCompat.Builder? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
        } else {
            builder = NotificationCompat.Builder(applicationContext)
            builder.priority = NotificationCompat.PRIORITY_DEFAULT
        }
        //标题
        builder.setContentTitle(title)
        //文本内容
        builder.setContentText(content)
        //小图标
        builder.setSmallIcon(smallIcon)
        //设置点击信息后自动清除通知
        builder.setAutoCancel(true)
        return builder
    }

    /**
     * 发送通知
     */
    fun sendNotification(notifyId: Int, title: String, content: String, @DrawableRes smallIcon: Int) {
        val builder = getNotification(title, content, smallIcon)
        manager?.notify(notifyId, builder.build())
    }
    /**
     * 发送带有进度的通知
     */
    /**
     * 发送带有进度的通知
     */
    @JvmOverloads
    fun sendNotificationProgress(title: String, content: String, @DrawableRes smallIcon: Int, progress: Int, intent: PendingIntent? = null) {
        val builder = getNotificationProgress(title, content, smallIcon, progress, intent)
        manager?.notify(notification_id, builder.build())
    }

    /**
     * 获取带有进度的Notification
     */
    private fun getNotificationProgress(
        title: String, content: String,
        @DrawableRes smallIcon: Int,
        progress: Int, intent: PendingIntent?
    ): NotificationCompat.Builder {
        var builder: NotificationCompat.Builder? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
        } else {
            builder = NotificationCompat.Builder(applicationContext)
            builder.priority = NotificationCompat.PRIORITY_DEFAULT
        }

        //标题
        builder.setContentTitle(title)
        //文本内容
        builder.setContentText(content)
        //小图标
        builder.setSmallIcon(smallIcon)
        //设置大图标，未设置时使用小图标代替，拉下通知栏显示的那个图标
        //设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
        builder.setLargeIcon(BitmapFactory.decodeResource(resources, smallIcon))
        if (progress in 1..99) {
            //一种是有进度刻度的（false）,一种是循环流动的（true）
            //设置为false，表示刻度，设置为true，表示流动
            builder.setProgress(100, progress, false)
        } else {
            //0,0,false,可以将进度条隐藏
            builder.setProgress(0, 0, false)
            builder.setContentText(this.resources.getString(R.string.notification_downloading))
        }
        //设置点击信息后自动清除通知
        builder.setAutoCancel(true)
        //通知的时间
        builder.setWhen(System.currentTimeMillis())
        //设置点击信息后的跳转（意图）
        builder.setContentIntent(intent)
        return builder
    }

    fun cancelNotification() {
        manager?.cancel(notification_id)
    }

}
