package com.intentfilter.androidpermissions;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.LocalBroadcastManager;

import com.intentfilter.androidpermissions.helpers.Logger;
import com.intentfilter.androidpermissions.services.NotificationService;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static android.app.PendingIntent.FLAG_ONE_SHOT;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.support.v4.content.ContextCompat.checkSelfPermission;
import static com.intentfilter.androidpermissions.PermissionsActivity.EXTRA_PERMISSIONS_DENIED;
import static com.intentfilter.androidpermissions.PermissionsActivity.EXTRA_PERMISSIONS_GRANTED;


public class PermissionManager extends BroadcastReceiver {

    private Context context;
    private final Logger logger;
    private static PermissionManager permissionManager;
    private PermissionHandler permissionHandler;

    private PermissionManager(Context context) {
        this.context = context;
        this.permissionHandler = new PermissionHandler(this, context);
        this.logger = Logger.loggerFor(PermissionManager.class);
    }

    public static PermissionManager getInstance(Context context) {
        if (permissionManager == null) {
            permissionManager = new PermissionManager(context.getApplicationContext());
        }
        return permissionManager;
    }

    public void checkPermissions(@NonNull Collection<String> permissions, @NonNull final PermissionRequestListener listener) {
        permissionHandler.checkPermissions(permissions, listener);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String[] grantedPermissions = intent.getStringArrayExtra(EXTRA_PERMISSIONS_GRANTED);
        String[] deniedPermissions = intent.getStringArrayExtra(EXTRA_PERMISSIONS_DENIED);
        logPermissionsResponse(grantedPermissions, deniedPermissions);
        permissionHandler.onPermissionsResult(grantedPermissions, deniedPermissions);
    }

    void startPermissionActivity(Set<String> permissions) {
        Intent intent = permissionActivityIntent(permissions);
        context.startActivity(intent);
    }

    void showPermissionNotification(Set<String> permissions, @StringRes int titleResId, @StringRes int messageResId) {
        NotificationService notificationService = new NotificationService(context);
        Notification notification = notificationService.buildNotification(context.getString(titleResId),
                context.getString(messageResId), permissionActivityIntent(permissions), notificationDismissIntent(permissions));
        notificationService.notify(permissions.toString(), permissions.hashCode(), notification);
    }

    @NonNull
    private Intent permissionActivityIntent(Set<String> permissions) {
        Intent intent = new Intent(context, PermissionsActivity.class);
        intent.putExtra(PermissionsActivity.EXTRA_PERMISSIONS, permissions.toArray(new String[permissions.size()]));
        intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    boolean permissionAlreadyGranted(String permission) {
        return checkSelfPermission(context, permission) == PERMISSION_GRANTED;
    }

    void registerBroadcastReceiver(String action) {
        logger.i("Registering for PERMISSIONS_REQUEST broadcast");
        LocalBroadcastManager.getInstance(context).registerReceiver(this, new IntentFilter(action));
    }

    void unregisterBroadcastReceiver() {
        logger.i("Un-registering for PERMISSIONS_REQUEST broadcast");
        LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
    }

    void removePendingPermissionRequests(List<String> permissions) {
        permissionHandler.invalidatePendingPermissionRequests(permissions);
    }

    @NonNull
    private PendingIntent notificationDismissIntent(Set<String> permissions) {
        Intent notificationDeleteIntent = new Intent(context, NotificationDismissReceiver.class);
        notificationDeleteIntent.putExtra(PermissionsActivity.EXTRA_PERMISSIONS, permissions.toArray(new String[permissions.size()]));
        return PendingIntent.getBroadcast(context, PermissionsActivity.PERMISSIONS_REQUEST_CODE, notificationDeleteIntent, FLAG_ONE_SHOT);
    }

    private void logPermissionsResponse(String[] grantedPermissions, String[] deniedPermissions) {
        logger.i(String.format("Received broadcast response for permission(s). \nGranted: %s\nDenied: %s",
                Arrays.toString(grantedPermissions), Arrays.toString(deniedPermissions)));
    }

    public interface PermissionRequestListener {
        void onPermissionGranted();

        void onPermissionDenied();
    }
}
@@ -686,69 +686,8 @@
 OT;%0A
-import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;%0A
 impo
@@ -822,24 +822,113 @@
 Permission;%0A
+import static com.intentfilter.androidpermissions.PermissionsActivity.EXTRA_PERMISSIONS;%0A
 import stati
@@ -3235,31 +3235,22 @@
         
-Intent intent =
+return
  new Int
@@ -3284,25 +3284,24 @@
 ivity.class)
-;
 %0A        int
@@ -3297,22 +3297,24 @@
         
-intent
+        
 .putExtr
@@ -3315,36 +3315,16 @@
 utExtra(
-PermissionsActivity.
 EXTRA_PE
@@ -3378,33 +3378,32 @@
 ssions.size()%5D))
-;
 %0A        intent.
@@ -3395,22 +3395,75 @@
         
-intent
+        .setAction(permissions.toString())%0A                
 .setFlag
@@ -3464,16 +3464,23 @@
 etFlags(
+Intent.
 FLAG_ACT
@@ -3500,31 +3500,8 @@
 K);%0A
-        return intent;%0A
     
@@ -4453,36 +4453,16 @@
 utExtra(
-PermissionsActivity.
 EXTRA_PE

Fix issue due to which dialogs for multiple permission requests were not shown.