package com.baulsupp.oksocial.services.facebook;

import com.baulsupp.oksocial.authenticator.AuthUtil;
import com.baulsupp.oksocial.authenticator.SimpleWebServer;
import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token;
import com.baulsupp.oksocial.output.OutputHandler;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;
import okhttp3.OkHttpClient;

import static com.baulsupp.oksocial.authenticator.AuthUtil.makeSimpleGetRequest;
import static java.util.stream.Collectors.joining;

public class FacebookAuthFlow {

  public static Oauth2Token login(OkHttpClient client, OutputHandler outputHandler, String clientId,
      String clientSecret,
      Set<String> scopes) throws IOException {
    try (SimpleWebServer<String> s = SimpleWebServer.forCode()) {

      String serverUri = s.getRedirectUri();

      String loginUrl = "https://www.facebook.com/dialog/oauth"
          + "?client_id=" + clientId
          + "&redirect_uri=" + serverUri
          + "&scope=" + URLEncoder.encode(scopes.stream().collect(joining(",")),
          "UTF-8");

      outputHandler.openLink(loginUrl);

      String code = s.waitForCode();

      String tokenUrl = "https://graph.facebook.com/v2.8/oauth/access_token"
          + "?client_id=" + clientId
          + "&redirect_uri=" + serverUri
          + "&client_secret=" + clientSecret
          + "&code=" + code;

      Map<String, Object> map =
          AuthUtil.makeJsonMapRequest(client, AuthUtil.uriGetRequest(tokenUrl));

      String shortToken = (String) map.get("access_token");

      String exchangeUrl = "https://graph.facebook.com/oauth/access_token"
          + "?grant_type=fb_exchange_token"
          + "&client_id=" + clientId
          + "&client_secret=" + clientSecret
          + "&fb_exchange_token=" + shortToken;

      String longTokenBody = makeSimpleGetRequest(client, exchangeUrl);

      return new Oauth2Token(parseExchangeRequest(longTokenBody));
    }
  }

  private static String parseExchangeRequest(String body) {
    String[] params = body.split("&");

    for (String p : params) {
      String[] parts = p.split("=");

      if (parts[0].equals("access_token")) {
        return parts[1];
      }
    }

    return null;
  }
}

@@ -47,61 +47,8 @@
 k;%0A%0A
-import com.baulsupp.oksocial.authenticator.AuthUtil;%0A
 impo
@@ -406,30 +406,102 @@
 uthUtil.make
-Simple
+JsonMapRequest;%0Aimport static com.baulsupp.oksocial.authenticator.AuthUtil.uri
 GetRequest;%0A
@@ -1468,25 +1468,16 @@
         
-AuthUtil.
 makeJson
@@ -1495,25 +1495,16 @@
 client, 
-AuthUtil.
 uriGetRe
@@ -1839,22 +1839,35 @@
 %0A%0A      
+Map%3C
 String
+, Object%3E
  longTok
@@ -1883,17 +1883,15 @@
 make
-SimpleGet
+JsonMap
 Requ
@@ -1902,16 +1902,30 @@
 client, 
+uriGetRequest(
 exchange
@@ -1924,24 +1924,25 @@
 exchangeUrl)
+)
 ;%0A%0A      ret
@@ -1965,326 +1965,58 @@
 ken(
-parseExchangeRequest(longTokenBody));%0A    %7D%0A  %7D%0A%0A  private static String parseExchangeRequest(String body) %7B%0A    String%5B%5D params = body.split(%22&%22);%0A%0A    for (String p : params) %7B%0A      String%5B%5D parts = p.split(%22=%22);%0A%0A      if (parts%5B0%5D.equals(%22access_token%22)) %7B%0A        return parts%5B1%5D
+(String) longTokenBody.get(%22access_token%22))
 ;%0A    
- 
- %7D%0A    %7D%0A%0A    return null;
+%7D
 %0A  %7D

fix auth flow (#251)