package com.slowpath.hockeyapp;

import android.app.Activity;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import net.hockeyapp.android.FeedbackManager;
import net.hockeyapp.android.UpdateManager;
import net.hockeyapp.android.CrashManager;
import net.hockeyapp.android.CrashManagerListener;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableArray;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.lang.RuntimeException;
import java.lang.Runnable;
import java.lang.Thread;

public class RNHockeyAppModule extends ReactContextBaseJavaModule {
  public static final int RC_HOCKEYAPP_IN = 9200;

  private Activity _activity;
  private static ReactApplicationContext _context;
  public static boolean _initialized = false;
  public static String _token = null;
  public static boolean _autoSend = true;

  public RNHockeyAppModule(ReactApplicationContext _reactContext, Activity activity) {
    super(_reactContext);
    _context = _reactContext;
    _activity = activity;
  }

  @Override
  public String getName() {
    return "RNHockeyApp";
  }

  @ReactMethod
  public void configure(String token, Boolean autoSend) {
    if (!_initialized) {
      _token = token;
      _autoSend = autoSend;
      _initialized = true;
    }
  }

  @ReactMethod
  public void start() {
    if (_initialized) {
      FeedbackManager.register(_activity, _token, null);

      if (_autoSend) {
        CrashManager.register(_activity, _token, new CrashManagerListener() {
          public boolean shouldAutoUploadCrashes() {
            return true;
          }
        });
      } else {
        CrashManager.register(_activity, _token);
      }
    }
  }

  @ReactMethod
  public void checkForUpdate() {
    if (_initialized) {
      UpdateManager.register(_activity, _token);
    }
  }

  @ReactMethod
  public void feedback() {
    if (_initialized) {
      _activity.runOnUiThread(new Runnable() {
        private Activity _activity;

        public Runnable init(Activity activity) {
          _activity = activity;
          return (this);
        }

        @Override
        public void run() {
          FeedbackManager.showFeedbackActivity(_activity);
        }
      }.init(_activity));
    }
  }

  @ReactMethod
  public void generateCrashReport() {
    if (_initialized) {
      new Thread(new Runnable() {
        public void run() {
          Calendar c = Calendar.getInstance();
          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          throw new RuntimeException("Test crash at " + df.format(c.getTime()));
        }
      }).start();
    }
  }

}

@@ -60,55 +60,8 @@
 y;%0A%0A
-import com.facebook.react.bridge.NativeModule;%0A
 impo
@@ -114,16 +114,16 @@
 ontext;%0A
+
 import c
@@ -260,32 +260,75 @@
 eedbackManager;%0A
+import net.hockeyapp.android.LoginManager;%0A
 import net.hocke
@@ -454,147 +454,8 @@
 r;%0A%0A
-import com.facebook.react.bridge.Arguments;%0Aimport com.facebook.react.bridge.WritableMap;%0Aimport com.facebook.react.bridge.WritableArray;%0A%0A
 impo
@@ -719,16 +719,480 @@
  9200;%0A%0A
+  // This wants to be an enum, but cannot translate that to match the JS API properly%0A  public static final int AUTHENTICATION_TYPE_ANONYMOUS = 0;%0A  public static final int AUTHENTICATION_TYPE_EMAIL_SECRET = 1;%0A  public static final int AUTHENTICATION_TYPE_EMAIL_PASSWORD = 2;%0A  public static final int AUTHENTICATION_TYPE_DEVICE_UUID = 3;%0A  public static final int AUTHENTICATION_TYPE_WEB = 4; // Included for consistency, but not supported on Android currently%0A%0A
   privat
@@ -1389,16 +1389,93 @@
  = true;
+%0A  public static int _authType = 0;%0A  public static String _appSecret = null;
 %0A%0A  publ
@@ -1774,32 +1774,64 @@
 Boolean autoSend
+, int apiAuthType, String secret
 ) %7B%0A    if (!_in
@@ -1893,16 +1893,74 @@
 toSend;%0A
+      _authType = apiAuthType;%0A      _appSecret = secret;%0A
       _i
@@ -2378,23 +2378,976 @@
 token);%0A
-
       %7D
+%0A%0A      int authenticationMode;%0A      switch (_authType) %7B%0A        case AUTHENTICATION_TYPE_EMAIL_SECRET: %7B%0A          authenticationMode = LoginManager.LOGIN_MODE_EMAIL_ONLY;%0A          break;%0A        %7D%0A        case AUTHENTICATION_TYPE_EMAIL_PASSWORD: %7B%0A          authenticationMode = LoginManager.LOGIN_MODE_EMAIL_PASSWORD;%0A          break;%0A        %7D%0A        case AUTHENTICATION_TYPE_DEVICE_UUID: %7B%0A          authenticationMode = LoginManager.LOGIN_MODE_VALIDATE;%0A          break;%0A        %7D%0A        case AUTHENTICATION_TYPE_WEB: %7B%0A          throw new IllegalArgumentException(%22Web authentication is not supported!%22);%0A        %7D%0A        case AUTHENTICATION_TYPE_ANONYMOUS:%0A        default: %7B%0A          authenticationMode = LoginManager.LOGIN_MODE_ANONYMOUS;%0A          break;%0A        %7D%0A      %7D%0A%0A      LoginManager.register(_context, _token, _appSecret, authenticationMode, (Class%3C?%3E) null);%0A      LoginManager.verifyLogin(_activity, _activity.getIntent());
 %0A    %7D%0A 

Add authentication support for Android