/*
 * Copyright (c) 2011-2019, Zingaya, Inc. All rights reserved.
 */

package com.dyteMobileSdk;

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 = "DyteNotificationChannel";
    private static final String CHANNEL_NAME = "Ongoing Conference Notifications";
    private static final String CHANNEL_DESC = "Ongoing Conference Notifications";

    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, "DyteForegroundService: 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 = 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("Ongoing Meeting")
                .setContentText("Tap to return to the meeting")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setWhen(System.currentTimeMillis())
                .setUsesChronometer(true)
                .setAutoCancel(false)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setOnlyAlertOnce(true)
                .setSmallIcon(appIconResId);

        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;
    }
}