package com.reactnative.module;


import java.util.HashSet;
import java.util.Set;

import com.bugsnag.android.Bugsnag;
import com.bugsnag.android.Configuration;
import com.bugsnag.android.EndpointConfiguration;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.NoSuchKeyException;

import com.reactnative.scenarios.Scenario;

public class BugsnagModule extends ReactContextBaseJavaModule {
  private static ReactApplicationContext reactContext;

  private ScenarioFactory factory = new ScenarioFactory();

  BugsnagModule(ReactApplicationContext context) {
    super(context);
    reactContext = context;
  }

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

  @ReactMethod
  public void runScenario(String scenarioName, Callback completeCallback) {
      Scenario testScenario = factory.testScenarioForName(scenarioName, reactContext);
      testScenario.run();
      completeCallback.invoke();
  }

  @ReactMethod
  public void startBugsnag(ReadableMap options, Callback readyCallback) {
    Configuration bugsnagConfig = createConfiguration(options);
    Bugsnag.start(reactContext, bugsnagConfig);
    readyCallback.invoke();
  }

  private Configuration createConfiguration(ReadableMap options) {
      Configuration config = new Configuration(options.getString("apiKey"));
      config.setEndpoints(new EndpointConfiguration(options.getString("endpoint"), options.getString("endpoint")));
      config.setAutoTrackSessions(options.getBoolean("autoTrackSessions"));

      try {
        String appVersion = null;
        appVersion = options.getString("appVersion");
        config.setAppVersion(appVersion);
      } catch (NoSuchKeyException e) {
        // ignore NoSuchKeyException
      }

      try {
        String appType = options.getString("appType");
        config.setAppType(appType);
      } catch (NoSuchKeyException e) {
        // ignore NoSuchKeyException
      }

      try {
        String releaseStage = options.getString("releaseStage");
        config.setReleaseStage(releaseStage);
      } catch (NoSuchKeyException e) {
        // ignore NoSuchKeyException
      }

      try {
        Set<String> enabledReleaseStages = new HashSet<String>();
        ReadableArray ar = options.getArray("enabledReleaseStages");
        for (int i = 0; i < ar.size(); i++) enabledReleaseStages.add(ar.getString(i));
        config.setEnabledReleaseStages(enabledReleaseStages);
      } catch (NoSuchKeyException e) {
        // ignore NoSuchKeyException
      }

      return config;
  }
}

@@ -76,16 +76,42 @@
 l.Set;%0A%0A
+import android.util.Log;%0A%0A
 import c
@@ -225,24 +225,59 @@
 nfiguration;
+%0Aimport com.bugsnag.android.Logger;
 %0A%0Aimport com
@@ -1421,24 +1421,942 @@
 n(options);%0A
+    bugsnagConfig.setLogger(new Logger() %7B%0A      private static final String TAG = %22Bugsnag%22;%0A%0A      @Override%0A      public void e(String msg) %7B%0A          Log.e(TAG, msg);%0A      %7D%0A%0A      @Override%0A      public void e(String msg, Throwable throwable) %7B%0A          Log.e(TAG, msg, throwable);%0A      %7D%0A%0A      @Override%0A      public void w(String msg) %7B%0A          Log.w(TAG, msg);%0A      %7D%0A%0A      @Override%0A      public void w(String msg, Throwable throwable) %7B%0A          Log.w(TAG, msg, throwable);%0A      %7D%0A%0A      @Override%0A      public void i(String msg) %7B%0A          Log.i(TAG, msg);%0A      %7D%0A%0A      @Override%0A      public void i(String msg, Throwable throwable) %7B%0A          Log.i(TAG, msg, throwable);%0A      %7D%0A%0A      @Override%0A      public void d(String msg) %7B%0A          Log.d(TAG, msg);%0A      %7D%0A%0A      @Override%0A      public void d(String msg, Throwable throwable) %7B%0A          Log.d(TAG, msg, throwable);%0A      %7D%0A    %7D);%0A
     Bugsnag.

add logging for mazerunner android