package app.furmeet.events.system.helper

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat

inline fun <reified KClass : Context> KClass.notificationBuilderCompat(@StringRes notificationChannelString: Int, important: Boolean): NotificationCompat.Builder =
        getNotificationChannelBuilder(this, KClass::class.java.simpleName, notificationChannelString, important)

fun getNotificationChannelBuilder(context: Context, SIMPLE_ID: String, @StringRes notificationChannelString: Int, important: Boolean) =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager = context.getSystemService(NotificationManager::class.java) as NotificationManager
            if (notificationManager.getNotificationChannel(SIMPLE_ID) == null) {
                val channelName = context.getString(notificationChannelString)
                val channel = NotificationChannel(SIMPLE_ID, channelName, if (important) NotificationManager.IMPORTANCE_HIGH else NotificationManager.IMPORTANCE_LOW)
                notificationManager.createNotificationChannel(channel)
            }
            NotificationCompat.Builder(context, SIMPLE_ID)
        } else {
            @Suppress("DEPRECATION") //older API will have this
            NotificationCompat.Builder(context)
        }
