package com.google.sps.firebase;

import com.google.auth.appengine.AppEngineCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.ThreadManager;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

// Only create app one time
public class FirebaseAppManager {
  private static FirebaseApp app = null;

  public static FirebaseApp getApp() throws IOException {
    if (app == null) {
      List<String> scopes =
          Arrays.asList(
              "https://www.googleapis.com/auth/cloud-platform",
              "https://www.googleapis.com/auth/userinfo.email");

      FirebaseOptions options =
          new FirebaseOptions.Builder()
              .setCredentials(AppEngineCredentials.newBuilder().setScopes(scopes).build())
              .setDatabaseUrl("https://fulfillment-deco-step-2020.firebaseio.com")
              .setProjectId("fulfillment-deco-step-2020")
              .setThreadManager(
                  new ThreadManager() {

                    @Override
                    protected void releaseExecutor(FirebaseApp app, ExecutorService executor) {
                      executor.shutdownNow();
                    }

                    @Override
                    protected ThreadFactory getThreadFactory() {
                      // TODO Auto-generated method stub
                      return com.google.appengine.api.ThreadManager.backgroundThreadFactory();
                    }

                    @Override
                    protected ExecutorService getExecutor(FirebaseApp app) {
                      // TODO Auto-generated method stub
                      return Executors.newCachedThreadPool(
                          com.google.appengine.api.ThreadManager.backgroundThreadFactory());
                    }
                  })
              .build();
      app = FirebaseApp.initializeApp(options);
    }
    return app;
  }
}

@@ -50,30 +50,133 @@
 le.a
-uth.appengine.AppEngin
+ppengine.api.utils.SystemProperty;%0Aimport com.google.auth.appengine.AppEngineCredentials;%0Aimport com.google.auth.oauth2.Googl
 eCre
@@ -626,21 +626,22 @@
 %0A  p
-ublic
+rivate
  static 
 Fire
@@ -640,69 +640,153 @@
 tic 
-FirebaseApp getApp() throws IOException %7B%0A    if (app == null
+GoogleCredentials getCredentials() throws IOException %7B%0A    if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production
 ) %7B%0A
@@ -967,16 +967,284 @@
 mail%22);%0A
+      return AppEngineCredentials.newBuilder().setScopes(scopes).build();%0A    %7D else %7B%0A      // Local development server%0A      return GoogleCredentials.getApplicationDefault();%0A    %7D%0A  %7D%0A%0A  public static FirebaseApp getApp() throws IOException %7B%0A    if (app == null) %7B
 %0A      F
@@ -1338,25 +1338,19 @@
 entials(
-AppEngine
+get
 Credenti
@@ -1356,45 +1356,8 @@
 ials
-.newBuilder().setScopes(scopes).build
 ())%0A

Use correct credentials depending on whether running locally or deployed.