package fr.plnech.dunbar.notif

import android.app.*
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import fr.plnech.dunbar.model.Friend


const val CHANNEL_ID = "dunbar"

class FriendReminder(private val ctx: Context) {

    private var channelCreated = false

    private fun createNotificationChannel() {
        // 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 channel = NotificationChannel(
                CHANNEL_ID, ctx.getString(fr.plnech.dunbar.R.string.channel_name),
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply { description = ctx.getString(fr.plnech.dunbar.R.string.channel_description) }

            (ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
                .createNotificationChannel(channel)
        }
        channelCreated = true
    }


    fun createNotification(friend: Friend, activity: Activity): Notification? {
        if (!channelCreated) createNotificationChannel()

        val pendingIntent: PendingIntent =
            PendingIntent.getActivity(activity, 0, friend.smsIntent(), 0)

        return NotificationCompat.Builder(ctx, CHANNEL_ID)
            .setContentTitle("What is ${friend.name} up to?")
            .setContentText("Ask them for some news!")
            .setSmallIcon(fr.plnech.dunbar.R.drawable.ic_friend)
//            .setStyle(
//                NotificationCompat.BigTextStyle()
//                    .bigText("Much longer text that cannot fit one line...")
//            )
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .also {
                it.priority = NotificationCompat.PRIORITY_HIGH
            }.build()
    }
}