package com.hmg.hmgDr.util

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat
import com.hmg.hmgDr.Model.NotificationDataModel
import com.hmg.hmgDr.Model.NotificationVideoModel

object NotificationUtil {

    fun createNotificationChannel(
        context: Context,
        notificationDataModel: NotificationDataModel
    ): String {
        // The id of the channel.
        val channelId: String = notificationDataModel.mChannelId

        // NotificationChannels are required for Notifications on O (API 26) and above.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // The user-visible name of the channel.
            val channelName: CharSequence = notificationDataModel.mChannelName
            // The user-visible description of the channel.
            val channelDescription: String = notificationDataModel.mChannelDescription
            val channelImportance: Int = notificationDataModel.mChannelImportance
            val channelEnableVibrate: Boolean = notificationDataModel.mChannelEnableVibrate
            val channelLockscreenVisibility: Int =
                notificationDataModel.mChannelLockscreenVisibility

            // Initializes NotificationChannel.
            val notificationChannel = NotificationChannel(channelId, channelName, channelImportance)
            notificationChannel.description = channelDescription
            notificationChannel.lightColor = Color.BLUE
            notificationChannel.lockscreenVisibility = channelLockscreenVisibility
            // no vibration
            notificationChannel.vibrationPattern = longArrayOf(0)
            notificationChannel.enableVibration(channelEnableVibrate)

            // Adds NotificationChannel to system. Attempting to create an existing notification
            // channel with its original values performs no operation, so it's safe to perform the
            // below sequence.
            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)
        }
        return channelId
    }

    fun setNotificationBigStyle(notificationData : NotificationVideoModel): NotificationCompat.BigTextStyle {
        return NotificationCompat.BigTextStyle() // Overrides ContentText in the big form of the template.
            .bigText(notificationData.mBigText) // Overrides ContentTitle in the big form of the template.
            .setBigContentTitle(notificationData.mBigContentTitle) // Summary line after the detail section in the big form of the template.
            // Note: To improve readability, don't overload the user with info. If Summary Text
            // doesn't add critical information, you should skip it.
            .setSummaryText(notificationData.mSummaryText)
    }

    /**
     * IMPORTANT NOTE: You should not do this action unless the user takes an action to see your
     * Notifications like this sample demonstrates. Spamming users to re-enable your notifications
     * is a bad idea.
     */
    fun openNotificationSettingsForApp(context: Context) {
        // Links to this app's notification settings.
        val intent = Intent()
        intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
        intent.putExtra("app_package", context.packageName)
        intent.putExtra("app_uid", context.applicationInfo.uid)

        // for Android 8 and above
        intent.putExtra("android.provider.extra.APP_PACKAGE", context.packageName)
        context.startActivity(intent)
    }
}