package com.globo.reactnativeua;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.urbanairship.AirshipReceiver;
import com.urbanairship.push.PushMessage;
import com.urbanairship.actions.Action;
import com.urbanairship.actions.ActionRunRequest;
import com.urbanairship.actions.OpenExternalUrlAction;

import java.util.List;


public class ReactNativeUAReceiver extends AirshipReceiver {

    @Override
    protected void onPushReceived(@NonNull Context context, @NonNull PushMessage message, boolean notificationPosted) {
        boolean isRunning = isApplicationRunning(context);

        if (isRunning && ReactNativeUAEventEmitter.getInstance() != null) {

            if (message.getActions().get("^u") != null && ReactNativeUAReceiverHelper.setup(context).isActionUrl()) {
                ActionRunRequest.createRequest(new OpenExternalUrlAction()).setSituation(Action.SITUATION_MANUAL_INVOCATION).setValue(message.getActions().get("^u")).run();
                return;
            }

            ReactNativeUAEventEmitter.getInstance().sendEvent("onNotificationOpened", message);
        }
    }

    @Override
    protected boolean onNotificationOpened(@NonNull Context context, @NonNull NotificationInfo notificationInfo) {
        Intent intent = new Intent();

        intent.setAction("com.urbanairship.push.RECEIVED");
        intent.putExtra("com.urbanairship.push.EXTRA_PUSH_MESSAGE_BUNDLE", notificationInfo.getMessage().getPushBundle());

        if (ReactNativeUAEventEmitter.getInstance() == null) ReactNativeUAReceiverHelper.setup(context).savePushIntent(intent);
        else context.sendBroadcast(intent);

        return false;
    }

    private boolean isApplicationRunning(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
        
        for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
            if (processInfo.processName.equals(context.getPackageName())) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String d: processInfo.pkgList) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}

@@ -126,16 +126,42 @@
 Intent;%0A
+import android.os.Bundle;%0A
 import a
@@ -1105,32 +1105,130 @@
 %0A            %7D%0A%0A
+            String event = message.getPushBundle().getString(%22Event%22, %22onNotificationReceived%22);%0A%0A
             Reac
@@ -1277,30 +1277,13 @@
 ent(
-%22onNotificationOpened%22
+event
 , me
@@ -1471,24 +1471,152 @@
  Intent();%0A%0A
+        Bundle push = notificationInfo.getMessage().getPushBundle();%0A%0A        push.putString(%22Event%22, %22onNotificationOpened%22);%0A%0A
         inte
@@ -1742,53 +1742,12 @@
 E%22, 
-notificationInfo.getMessage().getPushBundle()
+push
 );%0A%0A
@@ -2693,9 +2693,8 @@
 %0A    %7D%0A%7D
-%0A

add correctly push events identifiers