package com.cheng.bole.manager

import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.fragment.app.FragmentManager
import com.cheng.bole.R
import com.cheng.bole.ui.dialog.TipDialog
import com.example.base.extensions.toast
import com.example.base.utils.Utils


object NotificationHelper {
    private val notificationManager by lazy { Utils.getApp().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }

    private const val DEFAULT_CHANNEL_ID = "default"
    private const val DOWNLOAD_CHANNEL_ID = "download"
    private const val FOREGROUND_CHANNEL_ID = "foreground"

    private const val DOWNLOAD_GROUP = "download_group"

    private const val DOWNLOAD_CANCEL_CODE = 2000
    private const val DOWNLOAD_RETRY_CODE = 3000

    private var _notificationId = 1000

    /**
     * 创建默认通知通道
     */
    fun createDefaultChannel() {
        createNotificationChannel(DEFAULT_CHANNEL_ID, "Default", NotificationManager.IMPORTANCE_DEFAULT)
    }

    /**
     * 前台通知
     */
    fun startForeground(service: Service) {
        val channelId = createNotificationChannel(FOREGROUND_CHANNEL_ID, "Foreground", NotificationManager.IMPORTANCE_LOW)
        val builder = NotificationCompat.Builder(service, channelId)
        val notification = builder
            .setContentText("文件下载服务运行中")
            .setSmallIcon(R.mipmap.ic_launcher_icon)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build()
        service.startForeground(1, notification)
    }

    /**
     * 取消通知
     */
    fun cancelNotification(id: Int) {
        notificationManager.cancel(id)
    }

    /**
     * 创建通知通道
     *
     * @param channelId
     * @param channelName
     * @return
     */
    private fun createNotificationChannel(channelId: String, channelName: String, importance: Int): String {
        val channel = NotificationChannel(channelId, channelName, importance)
        channel.setSound(null, null)
        notificationManager.createNotificationChannel(channel)
        return channelId
    }

    fun getNotificationId(): Int {
        return ++_notificationId
    }

    /**
     * 判断通知权限是否开启
     */
    fun isNotificationEnabled(context: Context): Boolean {
        return NotificationManagerCompat.from(context).areNotificationsEnabled()
    }

    /**
     * 通知被禁用时显示提示
     */
    fun showDisabledTip(activity: Activity, fm: FragmentManager, isHomePage: Boolean = false) {
        if (isHomePage) {
            toast("平台推送消息需要通知权限，检测到您已关闭相关权限，请前往设置手动打开")
        } else {
            val f = TipDialog.newInstance("请开启通知权限", "为方便查看下载进度，请在设置中打开【允许通知】")
            f.setOnSelectListener {
                if (it == DialogEnum.CLICK_OK) {
                    try {
                        //8.0及以上
                        val intent = Intent()
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
                        intent.putExtra(Settings.EXTRA_APP_PACKAGE, activity.packageName)
                        activity.startActivity(intent)
                    } catch (e: Exception) {
                        val intent = Intent()
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                        intent.setData(Uri.fromParts("package", activity.packageName, null))
                        activity.startActivity(intent)
                    }
                }
            }
            f.show(fm, TipDialog::class.java.simpleName)
        }
    }
}