package com.cloudflare.realtimekit;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;

import com.facebook.react.bridge.Promise;

import java.util.Random;


class NotificationHelper {
    public  static final int NOTIFICATION_ID = new Random().nextInt(99999) + 10000;
    private static final String CHANNEL_ID = "RealtimeKitNotificationChannel";
    private static final String CHANNEL_NAME = "Screen Sharing";
    private static final String CHANNEL_DESC = "Screen Sharing";

    private static NotificationHelper instance = null;
    private NotificationManager mNotificationManager;

    public static synchronized NotificationHelper getInstance(Context context) {
        if (instance == null) {
            instance = new NotificationHelper(context);
        }
        return instance;
    }

    private NotificationHelper(Context context) {
        mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    }

    void createNotificationChannel(Promise promise) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
            boolean enableVibration = false;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, channelImportance);
            channel.setDescription(CHANNEL_DESC);
            channel.enableVibration(enableVibration);
            mNotificationManager.createNotificationChannel(channel);
            promise.resolve(null);
        } else {
            promise.reject(Constants.ERROR_ANDROID_VERSION, "RTKForegroundService: Notification channel can be created on Android O+");
        }
    }

    Notification buildNotification(Context context) {
        if (context == null) {
            return null;
        }
        Intent notificationIntent = new Intent(context, context.getClass());
        PendingIntent pendingIntent;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
        }
        else {
            pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        final PackageManager packageManager = context.getPackageManager();
        int appIconResId = android.R.mipmap.sym_def_app_icon;
        Bitmap largeIcon = null;
        try {
            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            appIconResId=applicationInfo.icon;
            Drawable drawable=applicationInfo.loadIcon(packageManager);
            largeIcon = ((BitmapDrawable)drawable).getBitmap();
        } catch (Exception e) {
            //pass
        }

        builder
                .setCategory(NotificationCompat.CATEGORY_CALL)
                .setContentTitle("Screen Share")
                .setContentText("You are sharing your screen")
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setContentIntent(pendingIntent)
                .setOngoing(false)
                .setUsesChronometer(false)
                .setAutoCancel(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setOnlyAlertOnce(true)
                .setSmallIcon(appIconResId)
                .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE);
        if(largeIcon != null){
            builder.setLargeIcon(largeIcon);
        }

        return builder.build();
    }

    private Class getMainActivityClass(Context context) {
        String packageName = context.getPackageName();
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (launchIntent == null || launchIntent.getComponent() == null) {
            // Log.e("NotificationHelper", "Failed to get launch intent or component");
            return null;
        }
        try {
            return Class.forName(launchIntent.getComponent().getClassName());
        } catch (ClassNotFoundException e) {
            // Log.e("NotificationHelper", "Failed to get main activity class");
            return null;
        }
    }

    private 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());
        }
        return resourceId;
    }
}