package com.kickstarter.services.gcm;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.kickstarter.KSApplication;
import com.kickstarter.R;
import com.kickstarter.libs.CurrentUser;
import com.kickstarter.services.ApiClient;

import java.io.IOException;

import javax.inject.Inject;

import timber.log.Timber;

public class RegisterService extends IntentService {
  private static final String WORKER_THREAD_NAME = "RegisterService";
  private static final String[] TOPICS = {"global"};

  @Inject protected ApiClient apiClient;
  @Inject protected CurrentUser currentUser;

  public RegisterService() {
    super(WORKER_THREAD_NAME);
  }

  @Override
  public void onCreate() {
    super.onCreate();
    ((KSApplication) getApplicationContext()).component().inject(this);
  }

  @Override
  protected void onHandleIntent(@NonNull final Intent intent) {
    Timber.d("onHandleIntent");

    try {
      // This initially hits the network to retrieve the token, subsequent calls are local
      final InstanceID instanceID = InstanceID.getInstance(this);

      // R.string.gcm_defaultSenderId is derived from google-services.json
      final String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
      Timber.d("Token: " + token);

      sendTokenToAppServers(token);
      subscribeTopics(token);
    } catch (final Exception e) {
      Timber.e("Failed to complete token refresh: " +  e.toString());
    }
  }

  /**
   * Persist token to app servers.
   *
   * @param token The new token.
   */
  private void sendTokenToAppServers(@NonNull final String token) {
    if (currentUser.observable().take(1).toBlocking().single() != null) {
      apiClient.registerPushToken(token).first().toBlocking().single();
    }
  }

  /**
   * Subscribe to any topics of interest, as defined by the TOPICS constant.
   *
   * @throws IOException if unable to reach the GCM PubSub service
   */
  private void subscribeTopics(@NonNull final String token) throws IOException {
    final GcmPubSub pubSub = GcmPubSub.getInstance(this);
    for (final String topic : TOPICS) {
      pubSub.subscribe(token, "/topics/" + topic, null);
    }
  }
}


@@ -579,132 +579,8 @@
 e %7B%0A
-  private static final String WORKER_THREAD_NAME = %22RegisterService%22;%0A  private static final String%5B%5D TOPICS = %7B%22global%22%7D;%0A%0A
   @I
@@ -705,26 +705,25 @@
 per(
-WORKER_THREAD_NAME
+%22RegisterService%22
 );%0A 
@@ -1444,24 +1444,26 @@
  subscribeTo
+To
 pics(token);
@@ -1768,14 +1768,14 @@
 e().
-take(1
+first(
 ).to
@@ -1916,12 +1916,8 @@
  to 
-any 
 topi
@@ -1934,43 +1934,8 @@
 rest
-, as defined by the TOPICS constant
 .%0A  
@@ -2036,16 +2036,18 @@
 scribeTo
+To
 pics(@No
@@ -2155,50 +2155,8 @@
 s);%0A
-    for (final String topic : TOPICS) %7B%0A  
     
@@ -2192,17 +2192,15 @@
 ics/
-%22 + topic
+global%22
 , nu
@@ -2204,21 +2204,15 @@
  null);%0A
-    %7D%0A
   %7D%0A%7D%0A%0A

Remove a few unused constants