package com.xamoom.android.pushnotifications;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;

import com.pushwoosh.PushManager;
import com.pushwoosh.internal.PushManagerImpl;
import com.xamoom.android.xamoomsdk.Resource.Content;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by raphaelseher on 20/06/2017.
 */

public class NotificationReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent == null) {
      return;
    }

    Bundle pushBundle = PushManagerImpl.preHandlePush(context, intent);
    if(pushBundle == null) {
      return;
    }

    //get push bundle as JSON object
    JSONObject dataObject = PushManagerImpl.bundleToJSON(pushBundle);

    Intent customNotificationIntent = new Intent(XamoomPushActivity.PUSH_NOTIFICATION_HANDLER_NAME);
    if (isCustomNotificationHandlerRegistered(context, customNotificationIntent)) {
      sendCustomHandlerBroadcast(context, customNotificationIntent, dataObject);
      PushManagerImpl.postHandlePush(context, intent);
      return;
    }

    openDefaultActivity(context, intent, dataObject, pushBundle);
    PushManagerImpl.postHandlePush(context, intent);
  }

  /**
   * Checks if there is a broadcastreceiver entered for the
   * XamoomPushActivity.PUSH_NOTIFICATION_HANDLER_NAME.
   *
   * @param context Android context
   * @param intent Intent with XamoomPushActivity.PUSH_NOTIFICATION_HANDLER_NAME action
   * @return true if registered
   */
  private boolean isCustomNotificationHandlerRegistered(Context context, Intent intent) {
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> listPkgs = pm.queryBroadcastReceivers(intent, 0);
    if (listPkgs != null && listPkgs.size() > 0){
      for (ResolveInfo resInfo : listPkgs) {
        return true;
      }
    }

    return false;
  }

  private void sendCustomHandlerBroadcast(Context context, Intent intent, JSONObject dataObject) {
    // TODO: send custom broadcast
  }

  private void openDefaultActivity(Context context, Intent intent, JSONObject dataObject, Bundle pushBundle) {
    //Get default launcher intent for clarity
    Intent launchIntent  = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    launchIntent.addCategory("android.intent.category.LAUNCHER");

    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    //Put push notifications payload in Intent
    launchIntent.putExtras(pushBundle);
    launchIntent.putExtra(PushManager.PUSH_RECEIVE_EVENT, dataObject.toString());

    //Start activity!
    context.startActivity(launchIntent);
  }
}

@@ -488,24 +488,55 @@
 e.Content;%0A%0A
+import org.json.JSONException;%0A
 import org.j
@@ -580,59 +580,8 @@
 t;%0A%0A
-/**%0A * Created by raphaelseher on 20/06/2017.%0A */%0A%0A
 publ
@@ -637,16 +637,96 @@
 ceiver %7B
+%0A  private static final String TAG = NotificationReceiver.class.getSimpleName();
 %0A%0A  @Ove
@@ -1582,18 +1582,21 @@
 eceiver 
-en
+regis
 tered fo
@@ -2300,38 +2300,578 @@
     
-// TODO: send custom broadcast
+prepareIntentData(intent, dataObject);%0A    context.sendBroadcast(intent);%0A  %7D%0A%0A  private void prepareIntentData(Intent intent, JSONObject dataObject) %7B%0A    JSONObject userData = null;%0A    try %7B%0A      userData = (JSONObject) dataObject.get(%22u%22);%0A    %7D catch (JSONException e) %7B%0A      Log.i(TAG, %22Userdata is not an JSONObject.%22);%0A    %7D%0A%0A    String contentId = null;%0A    try %7B%0A      contentId = userData.getString(%22content_id%22);%0A    %7D catch (JSONException e) %7B%0A      Log.i(TAG, %22Userdata does not contain content_id.%22);%0A    %7D%0A%0A    intent.putExtra(%22content_id%22, contentId);
 %0A  %7D

Add prepareIntentData