package it.unitn.introsde.persistence.datasource;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
import com.restfb.types.User;
import it.unitn.introsde.persistence.entity.Person;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;

@Component
public class FaceBookDatasource {

    private static final Logger logger = LogManager.getLogger();

    public Person getUser(String accessToken) throws ParseException {
        //getting details mateo has permitted us to access
        FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
        User me = facebookClient.fetchObject("me", User.class);
        logger.info("name>> " + me.getFirstName());
        logger.info("accessToken>> " + accessToken);
        logger.info("me>> " + me);
        logger.info("me.getBirthday>> " + me.getBirthday());
        return new Person(me.getFirstName(), me.getLastName(), new SimpleDateFormat("MM/dd/yyyy").parse(me.getBirthday()));
    }

    public void posttoFacebook(String accessToken) {
        FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
        FacebookType publishMessageResponse = facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", "RestFB test"));
        logger.debug("Published message ID: " + publishMessageResponse.getId());
    }
}

@@ -678,68 +678,8 @@
  %7B%0D%0A
-        //getting details mateo has permitted us to access%0D%0A
     
@@ -768,18 +768,20 @@
    User 
-me
+user
  = faceb
@@ -850,26 +850,28 @@
 %22name%3E%3E %22 + 
-me
+user
 .getFirstNam
@@ -961,18 +961,20 @@
 e%3E%3E %22 + 
-me
+user
 );%0D%0A    
@@ -1011,18 +1011,20 @@
 y%3E%3E %22 + 
-me
+user
 .getBirt
@@ -1059,18 +1059,20 @@
  Person(
-me
+user
 .getFirs
@@ -1080,18 +1080,20 @@
 Name(), 
-me
+user
 .getLast
@@ -1141,18 +1141,20 @@
 ).parse(
-me
+user
 .getBirt
@@ -1197,18 +1197,14 @@
 post
-toFacebook
+ToWall
 (Str

clean up