package net.coscolla.comicstrip.push.gcm;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.google.android.gms.gcm.GcmListenerService;

import net.coscolla.comicstrip.R;
import net.coscolla.comicstrip.di.Graph;
import net.coscolla.comicstrip.ui.list.ListStripsActivity;
import net.coscolla.comicstrip.usecases.PushReceiveUseCase;

import javax.inject.Inject;

import timber.log.Timber;

import static rx.schedulers.Schedulers.io;

public class GcmMessageHandler extends GcmListenerService {
  public static final int MESSAGE_NOTIFICATION_ID = 435345;

  @Inject PushReceiveUseCase useCase;

  @Override
  public void onMessageReceived(String from, Bundle data) {
    String comic = data.getString("comic");

    Graph.getInstance().getGcmComponent().inject(this);

    useCase.getLastStrip(comic)
        .subscribe(
            strip -> createNotification(comic, comic, strip.title),
            e -> Timber.e(e, "Could not get the last strip from network"));
  }

  // Creates notification based on title and body received
  private void createNotification(String comic, String title, String body) {
    Context context = getBaseContext();

    createIntent(comic);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title)
        .setContentText(body)
        .setContentIntent(createIntent(comic))
        .setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
  }

  private PendingIntent createIntent(String comic) {
    Intent intent = new Intent(this, ListStripsActivity.class);
    intent.putExtra(ListStripsActivity.INTENT_ARG_COMIC_NAME, comic);

    return PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
  }
}

@@ -560,52 +560,8 @@
 r;%0A%0A
-import static rx.schedulers.Schedulers.io;%0A%0A
 publ
@@ -1098,17 +1098,28 @@
 %0A  /
-/
+**%0A   *
  Create
-s
+ a new
  not
@@ -1132,40 +1132,200 @@
 ion 
-based on title and body received
+announcing to the user a new strip is available%0A   *%0A   * @param comic String of the id of the comic%0A   * @param title title of the notification%0A   * @param body text of the notification%0A   */
 %0A  p
@@ -1590,16 +1590,25 @@
 auncher)
+%0A        
 .setCont
@@ -1898,16 +1898,381 @@
 .notify(
+getNotificationId(comic), mBuilder.build());%0A  %7D%0A%0A  /**%0A   * The notification id is a different for each comic, so we can have multiple notification%0A   * one for each comic%0A   *%0A   * @param comic String that represent one comic%0A   * @return a number that is the same for the same comic but different%0A   */%0A  private int getNotificationId(String comic) %7B%0A    return 
 MESSAGE_
@@ -2286,34 +2286,34 @@
 ATION_ID
-, mBuilder.build()
+ + comic.hashCode(
 );%0A  %7D%0A%0A

Improve now multiple notification can be reach one for each comic