package org.atlasapi.application.auth;

import static com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.atlasapi.application.users.User;
import org.atlasapi.application.users.UserStore;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.metabroadcast.common.social.auth.credentials.Credentials;
import com.metabroadcast.common.social.auth.credentials.CredentialsStore;
import com.metabroadcast.common.social.auth.facebook.AccessTokenChecker;
import com.metabroadcast.common.social.model.UserRef;
import com.metabroadcast.common.social.model.UserRef.UserNamespace;

public class OAuthTokenUserFetcher implements UserFetcher {

    public static final String OAUTH_PROVIDER_QUERY_PARAMETER = "oauth_provider";
    public static final String OAUTH_TOKEN_QUERY_PARAMETER = "oauth_token";
    
    private final CredentialsStore credentialsStore;
    private final Map<UserNamespace, AccessTokenChecker> accessTokenCheckers;
    private final UserStore userStore;

    public OAuthTokenUserFetcher(CredentialsStore credentialsStore,
            Map<UserNamespace, AccessTokenChecker> accessTokenCheckers,
            UserStore userStore) {
        this.credentialsStore = checkNotNull(credentialsStore);
        this.accessTokenCheckers = ImmutableMap.copyOf(accessTokenCheckers);
        this.userStore = checkNotNull(userStore);
    }
    
    public ImmutableSet<String> getParameterNames() {
        return ImmutableSet.of(OAUTH_PROVIDER_QUERY_PARAMETER,
                OAUTH_TOKEN_QUERY_PARAMETER);
    }

    public Optional<UserRef> userRefFor(HttpServletRequest request) {
        if (!Strings.isNullOrEmpty(request.getParameter(OAUTH_PROVIDER_QUERY_PARAMETER))) {
            UserNamespace oauthProviderNamespace = UserNamespace.valueOf(request.getParameter(OAUTH_PROVIDER_QUERY_PARAMETER).toUpperCase());
            String oauthToken = request.getParameter(OAUTH_TOKEN_QUERY_PARAMETER);
            Optional<Credentials> credentials = credentialsStore.credentialsForToken(oauthProviderNamespace, oauthToken);
            if (accessTokenCheckers.keySet().contains(oauthProviderNamespace) 
                    && credentials.isPresent()
                    && accessTokenCheckers.get(oauthProviderNamespace).check(credentials.get().authToken()).hasValue()) {
                return Optional.of(credentials.get().userRef());                
            }
        }
        return Optional.absent();
    }
    
    public Optional<User> userFor(HttpServletRequest request) {
        Optional<UserRef> userRef = userRefFor(request);
        if (userRef.isPresent()) {
            return userStore.userForRef(userRef.get());
        } else {
            return Optional.absent();
        }
    }
}

@@ -1836,9 +1836,8 @@
 if (
-!
 Stri
@@ -1907,32 +1907,85 @@
 AMETER))) %7B%0A    
+        return Optional.absent();%0A        %7D%0A        %0A
         UserName
@@ -2102,28 +2102,24 @@
 perCase());%0A
-    
         Stri
@@ -2189,28 +2189,24 @@
 R);%0A        
-    
 Optional%3CCre
@@ -2307,28 +2307,24 @@
 n);%0A        
- 
-   
 if (accessTo
@@ -2394,20 +2394,16 @@
         
-    
 && crede
@@ -2437,20 +2437,16 @@
         
-   
- 
 && acces
@@ -2543,36 +2543,32 @@
 ) %7B%0A            
-    
 return Optional.
@@ -2620,28 +2620,24 @@
    %0A        
-    
 %7D%0A        %7D%0A
@@ -2626,33 +2626,32 @@
       %7D%0A        
-%7D
 %0A        return 

switch conditional to a guard