package com.google.sps.authentication;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseToken;
import com.google.sps.firebase.FirebaseAppManager;
import com.google.appengine.api.datastore.Query.FilterPredicate;

public class Authenticator {
  private FirebaseAuth auth;
  private DatastoreService datastore;

  public Authenticator() throws IOException {
    this(FirebaseAuth.getInstance(FirebaseAppManager.getApp()));
  }

  protected Authenticator(FirebaseAuth auth) {
    this.auth = Objects.requireNonNull(auth);
    datastore = DatastoreServiceFactory.getDatastoreService();
  }

  public boolean verifyTaOrOwner(String idToken, Key classKey) {
    try {
      FirebaseToken tok = auth.verifyIdToken(idToken);
      PreparedQuery q = datastore.prepare(new Query("User").setFilter(
        new FilterPredicate("userEmail", FilterOperator.EQUAL, tok.getEmail())
      ));

      if(q.countEntities(FetchOptions.Builder.withLimit(1)) > 0) {
        return ((List<Key>) q.asSingleEntity().getProperty("taClasses")).contains(classKey) ||
               ((List<Key>) q.asSingleEntity().getProperty("ownedClasses")).contains(classKey);
      } else {
        return false;
      }
    } catch (FirebaseAuthException | IllegalArgumentException e) {
      return false;
    }
    
  } 
}
@@ -37,86 +37,8 @@
 n;%0A%0A
-import java.io.IOException;%0Aimport java.util.List;%0Aimport java.util.Objects;%0A%0A
 impo
@@ -487,16 +487,81 @@
 erator;%0A
+import com.google.appengine.api.datastore.Query.FilterPredicate;%0A
 import c
@@ -773,53 +773,157 @@
 gle.
-appengine.api.datastore.Query.FilterPredicate
+sps.workspace.Workspace;%0A%0Aimport java.io.IOException;%0Aimport java.util.List;%0Aimport java.util.Objects;%0Aimport java.util.concurrent.ExecutionException
 ;%0A%0Ap
@@ -1295,16 +1295,162 @@
 );%0A  %7D%0A%0A
+  public boolean verifyTaOrOwner(String idToken, String classCode) %7B%0A    return verifyTaOrOwner(idToken, KeyFactory.stringToKey(classCode));%0A  %7D%0A%0A
   public
@@ -1604,16 +1604,27 @@
 atastore
+%0A          
 .prepare
@@ -1652,25 +1652,16 @@
 tFilter(
-%0A        
 new Filt
@@ -1722,23 +1722,16 @@
 Email())
-%0A      
 ));%0A%0A   
@@ -1735,16 +1735,17 @@
       if
+ 
 (q.count
@@ -1890,11 +1890,8 @@
 Key)
- %7C%7C
 %0A   
@@ -1899,18 +1899,18 @@
         
-  
+%7C%7C
  ((List%3C
@@ -2127,13 +2127,386 @@
 %7D%0A  
-  %0A  %7D 
+%7D%0A%0A  public boolean verifyWorkspace(String idToken, Workspace w) throws InterruptedException, ExecutionException %7B%0A    try %7B%0A      FirebaseToken tok = auth.verifyIdToken(idToken);%0A%0A      return w.getStudentUID().get().equals(tok.getUid()) %7C%7C w.getTaUID().get().equals(tok.getUid());%0A    %7D catch (FirebaseAuthException %7C IllegalArgumentException e) %7B%0A      return false;%0A    %7D%0A  %7D
 %0A%7D
+%0A

Add authenticator method for workspaces.