package live.videosdk.rnwebrtc;

import android.app.Notification;
import android.app.Service;
import android.content.Context;

import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;


/**
 * Helper class for creating the media projection notification which is used with
 * {@link MediaProjectionService}.
 */
class MediaProjectionNotification {
    private static final String TAG = MediaProjectionNotification.class.getSimpleName();

    static final String ONGOING_CONFERENCE_CHANNEL_ID = "OngoingConferenceChannel";


    static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
        if (context == null) {
            Log.e(TAG, "Cannot create notification channel: no context");
            return;
        }

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);

        NotificationChannel channel =
                notificationManager.getNotificationChannel(ONGOING_CONFERENCE_CHANNEL_ID);

        if (channel != null) {
            // The channel was already created, no need to do it again.
            return;
        }

        channel = new NotificationChannel(
                ONGOING_CONFERENCE_CHANNEL_ID,
                context.getString(R.string.ongoing_notification_channel_name),
                NotificationManager.IMPORTANCE_DEFAULT
        );
        channel.enableLights(false);
        channel.enableVibration(false);
        channel.setShowBadge(false);

        notificationManager.createNotificationChannel(channel);
    }

    // Utility function to get the resource ID for the icon
    static int getResourceIdForResourceName(Context context, String resourceName) {
        int resourceId = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
        if (resourceId == 0) {
            resourceId = context.getResources().getIdentifier(resourceName, "mipmap", context.getPackageName());
        }

        if (resourceId == 0) {
            Log.e(TAG, "Failed to find resource: " + resourceName);
        }
        return resourceId;
    }

    // Builds the notification and makes sure everything is set
    static Notification buildMediaProjectionNotification(Context context, Bundle bundle) {

        // Read title and message from the passed bundle, or use default values
        String title = bundle.getString("title", "Default Title");
        String message = bundle.getString("message", "Default Message");

        // Validate icon resource
        int iconResId = getResourceIdForResourceName(context, "ic_launcher");
        if (iconResId == 0) {
            iconResId = android.R.drawable.ic_menu_info_details;
            Log.e(TAG, "No custom icon found, using default system icon.");
        }

        // Create the notification builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, ONGOING_CONFERENCE_CHANNEL_ID)
                .setCategory(NotificationCompat.CATEGORY_SERVICE)
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setOngoing(true)
                .setUsesChronometer(false)
                .setAutoCancel(false)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setSmallIcon(iconResId)
                .setOnlyAlertOnce(true);

        Notification notification = builder.build();
        Log.d(TAG, "Notification built successfully.");
        return notification;
    }
}
