/**
 * Copyright 2012-2013 StackMob
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.stackmob.customcode;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;
import com.stackmob.core.customcode.CustomCodeMethod;
import com.stackmob.core.rest.ProcessedAPIRequest;
import com.stackmob.core.rest.ResponseToProcess;
import com.stackmob.sdkapi.SDKServiceProvider;

import java.net.HttpURLConnection;
import java.util.*;

import com.stackmob.sdkapi.http.Header;
import com.stackmob.sdkapi.http.HttpService;
import com.stackmob.sdkapi.http.request.GetRequest;
import com.stackmob.sdkapi.http.response.HttpResponse;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class HelloWorld implements CustomCodeMethod {

  @Override
  public String getMethodName() {
    return "hello_world";
  }

  @Override
  public List<String> getParams() {
    return Arrays.asList("accessToken");
  }

  @Override
  public ResponseToProcess execute (ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
      Map<String, Object> map = new HashMap<String, Object>();

      String accessToken = "";
      JSONParser parser = new JSONParser();
      try {
          Object obj = parser.parse(request.getBody());
          JSONObject jsonObject = (JSONObject) obj;
          accessToken = (String) jsonObject.get("accessToken");
      } catch (ParseException pe) {

      }

      String token = "CAAGF19ZAX3kUBALLg0HGlC7kfypEMWwDcv7A8HVHeeyghgTYZAArrgcQuxGUJZAJw0PzAN8gvFKPNMKupbvsTad0gikvSSHnc57OxywqjhIuhFZAWDR1JoYZBrjnuzfbyO4ykNf5SijQ3Wf2Ox5kub4BABkNE9A49aWDxoZCrFG4WUCFYwTQuyhrXlo0Q175T0dxlIGHXZBzQZDZD";
      String url = "https://graph.facebook.com/me?access_token=" + token;

      // Formulate request headers
      Header accept = new Header("Accept-Charset", "utf-8");
      Header content = new Header("Content-Type", "application/x-www-form-urlencoded");

      Set<Header> set = new HashSet();
      set.add(accept);
      set.add(content);

      String responseBody = "";
      try {
          HttpService http = serviceProvider.getHttpService();

      /* In this Example we are going to be making a GET request
       * but PUT/POST/DELETE requests are also possible.
       */
          GetRequest req = new GetRequest(url,set);
          HttpResponse resp = http.get(req);

          int responseCode = resp.getCode();
          responseBody = resp.getBody();

      } catch (Exception e) {}

      map.put("msg", responseBody);


      return new ResponseToProcess(HttpURLConnection.HTTP_OK, map);
  }

}

@@ -2262,16 +2262,18 @@
 ring url
+Me
  = %22http
@@ -2297,16 +2297,113 @@
 k.com/me
+/friends?access_token=%22 + token;%0A      String urlFriends = %22https://graph.facebook.com/me/friends
 ?access_
@@ -2717,23 +2717,65 @@
 onseBody
+Me
  = %22%22;%0A
+      String responseBodyFriends = %22%22;%0A%0A
       tr
@@ -2998,16 +2998,18 @@
 uest req
+Me
  = new G
@@ -3021,16 +3021,18 @@
 uest(url
+Me
 ,set);%0A 
@@ -3057,16 +3057,18 @@
 nse resp
+Me
  = http.
@@ -3074,16 +3074,18 @@
 .get(req
+Me
 );%0A%0A    
@@ -3094,11 +3094,128 @@
     
-int
+GetRequest reqFriends = new GetRequest(urlMe,set);%0A          HttpResponse respFriends = http.get(reqFriends);%0A%0A         
  res
@@ -3223,19 +3223,21 @@
 onse
-C
+B
 od
+yM
 e = resp
 .get
@@ -3236,16 +3236,18 @@
 resp
+Me
 .get
-Code
+Body
 ();%0A
@@ -3268,23 +3268,37 @@
 onseBody
- = resp
+Friends = respFriends
 .getBody
@@ -3354,10 +3354,51 @@
 t(%22m
+e%22, responseBodyMe);%0A      map.put(%22friend
 s
-g
 %22, r
@@ -3408,16 +3408,23 @@
 onseBody
+Friends
 );%0A%0A%0A   

add get friends