package com.couchbase.lite.auth;

import com.couchbase.lite.Database;
import com.couchbase.lite.util.Log;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FacebookAuthorizer extends Authorizer {

    public static final String LOGIN_PARAMETER_ACCESS_TOKEN = "access_token";
    public static final String QUERY_PARAMETER = "facebookAccessToken";
    public static final String QUERY_PARAMETER_EMAIL = "email";

    private static Map<List<String>, String> accessTokens;

    private String emailAddress;

    public FacebookAuthorizer(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public boolean usesCookieBasedLogin() {
        return true;
    }

    public Map<String, String> loginParametersForSite(URL site) {
        Map<String, String> loginParameters = new HashMap<String, String>();

        String accessToken = accessTokenForEmailAndSite(this.emailAddress, site);
        if (accessToken != null) {
            loginParameters.put(LOGIN_PARAMETER_ACCESS_TOKEN, accessToken);
            return loginParameters;
        } else {
            return null;
        }

    }

    public String loginPathForSite(URL site) {
        return "/_facebook";
    }

    public synchronized static String registerAccessToken(String accessToken, String email, String origin) {

        List<String> key = new ArrayList<String>();
        key.add(email);
        key.add(origin);

        if (accessTokens == null) {
            accessTokens = new HashMap<List<String>, String>();
        }
        Log.d(Database.TAG, "FacebookAuthorizer registering key: " + key);
        accessTokens.put(key, accessToken);

        return email;

    }


    public static String accessTokenForEmailAndSite(String email, URL site) {
        List<String> key = new ArrayList<String>();
        key.add(email);
        key.add(site.toExternalForm().toLowerCase());
        Log.d(Database.TAG, "FacebookAuthorizer looking up key: " + key + " from list of access tokens");
        return accessTokens.get(key);
    }

}

@@ -901,17 +901,34 @@
 ing%3E();%0A
-%0A
+        try %7B%0A    
         
@@ -1001,32 +1001,36 @@
  site);%0A        
+    
 if (accessToken 
@@ -1052,16 +1052,20 @@
         
+  
+  
 loginPar
@@ -1128,24 +1128,28 @@
             
+    
 return login
@@ -1168,16 +1168,20 @@
         
+    
 %7D else %7B
@@ -1189,24 +1189,28 @@
             
+  
+  
 return null;
@@ -1214,26 +1214,171 @@
 ll;%0A        
-%7D%0A
+    %7D%0A        %7D catch (Exception e) %7B%0A            Log.e(Database.TAG, %22Error looking login parameters for site%22, e);%0A        %7D%0A        return null;
 %0A    %7D%0A%0A    
@@ -1999,32 +1999,50 @@
 il, URL site) %7B%0A
+        try %7B%0A    
         List%3CStr
@@ -2077,32 +2077,36 @@
 ing%3E();%0A        
+   
+ 
 key.add(email);%0A
@@ -2097,32 +2097,36 @@
 key.add(email);%0A
+    
         key.add(
@@ -2163,16 +2163,20 @@
 ase());%0A
+    
         
@@ -2277,24 +2277,28 @@
 %22);%0A        
+  
+  
 return acces
@@ -2311,21 +2311,153 @@
 s.get(key);%0A
+        %7D catch (Exception e) %7B%0A            Log.e(Database.TAG, %22Error looking up access token%22, e);%0A        %7D%0A        return null;%0A
     %7D%0A%0A%7D%0A

Add exception handling so that replicator will log error in this case.