package org.nativescript.plugins.firebase;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;

import java.io.IOException;

public class FirebasePlugin {
  private static final String TAG = "FirebasePlugin";

  private static String cachedToken;
  private static String cachedNotification;

  private static FirebasePluginListener onPushTokenReceivedCallback;
  private static FirebasePluginListener onNotificationReceivedCallback;

  public static void registerForPushNotifications(final String senderId) {
    new Thread() {
      public void run() {
        try {
          FirebaseInstanceId.getInstance().getToken(senderId, "FCM");
        } catch (IOException e) {
          Log.e(TAG, "Error getting a token from FCM: " + e.getMessage(), e);
        }
      }
    }.start();
  }

  public static void getCurrentPushToken(final String senderId, final FirebasePluginListener callback) {
    new Thread() {
      public void run() {
        try {
          callback.success(FirebaseInstanceId.getInstance().getToken(senderId, "FCM"));
        } catch (IOException e) {
          Log.e(TAG, "Error getting a token from FCM: " + e.getMessage(), e);
          callback.error(e.getMessage());
        }
      }
    }.start();
  }

  public static void unregisterForPushNotifications(final String senderId) {
    new Thread() {
      public void run() {
        try {
//          FirebaseInstanceId.getInstance().deleteToken(senderId, "FCM");
          FirebaseInstanceId.getInstance().deleteInstanceId();
        } catch (IOException e) {
          Log.e(TAG, "Error deleting token in FCM: " + e.getMessage(), e);
        }
      }
    }.start();
  }

  public static void setOnPushTokenReceivedCallback(FirebasePluginListener callbacks) {
    onPushTokenReceivedCallback = callbacks;
    if (cachedToken != null) {
      executeOnPushTokenReceivedCallback(cachedToken);
      cachedToken = null;
    }
  }

  public static void setOnNotificationReceivedCallback(FirebasePluginListener callbacks) {
    onNotificationReceivedCallback = callbacks;
    if (cachedNotification != null) {
      executeOnNotificationReceivedCallback(cachedNotification);
      cachedNotification = null;
    }
  }

  public static void executeOnPushTokenReceivedCallback(String token) {
    if (onPushTokenReceivedCallback != null) {
      onPushTokenReceivedCallback.success(token);
    } else {
      cachedToken = token;
    }
  }

  public static void executeOnNotificationReceivedCallback(String notification) {
    if (onNotificationReceivedCallback != null) {
      onNotificationReceivedCallback.success(notification);
    } else {
      cachedNotification = notification;
    }
  }
}

@@ -309,16 +309,86 @@
 ication;
+%0A  private static boolean preventInitialRegisterTokenCallback = false;
 %0A%0A  priv
@@ -658,32 +658,47 @@
  try %7B%0A         
+ String token =
  FirebaseInstanc
@@ -733,32 +733,210 @@
 enderId, %22FCM%22);
+%0A%0A          if (!preventInitialRegisterTokenCallback) %7B%0A            executeOnPushTokenReceivedCallback(token);%0A          %7D%0A%0A          preventInitialRegisterTokenCallback = false;
 %0A        %7D catch
@@ -1654,380 +1654,1152 @@
 y %7B%0A
-//          FirebaseInstanceId.getInstance().deleteToken(senderId, %22FCM%22);%0A          FirebaseInstanceId.getInstance().deleteInstanceId();%0A        %7D catch (IOException e) %7B%0A          Log.e(TAG, %22Error deleting token in FCM: %22 + e.getMessage(), e);%0A        %7D%0A      %7D%0A    %7D.start();%0A  %7D%0A%0A  public static void setOnPushTokenReceivedCallback(FirebasePluginListener callbacks) %7B
+          // No matter the workflow, on unregister we must ensure that subsequent calls to register%0A          // will trigger any callbacks.%0A          preventInitialRegisterTokenCallback = false;%0A%0A          FirebaseInstanceId.getInstance().deleteToken(senderId, %22FCM%22);%0A          // Do not use deleteInstanceId because by default FCM automatically re-initializes%0A          // the token and we get onPushTokenReceivedCallback triggered.%0A//          FirebaseInstanceId.getInstance().deleteInstanceId();%0A        %7D catch (IOException e) %7B%0A          Log.e(TAG, %22Error deleting token in FCM: %22 + e.getMessage(), e);%0A        %7D%0A      %7D%0A    %7D.start();%0A  %7D%0A%0A  public static void setOnPushTokenReceivedCallback(FirebasePluginListener callbacks) %7B%0A    // Workflow 1: User uses the registerForPushNotifications%0A    // In this case we are getting a double callback on initial app start up, since the FB instance%0A    // generates a new token at application startup automatically. So we need to prevent%0A    // the one triggered from the register.%0A    // Workflow 2: Users uses addPushTokenReceivedCallback directly%0A    // In this case we need to emit the token
 %0A   
@@ -2944,32 +2944,82 @@
 edToken = null;%0A
+      preventInitialRegisterTokenCallback = true;%0A
     %7D%0A  %7D%0A%0A  pub

fix android token generation workflow