package edu.brown.hackathon.fifteenminutes;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.gson.Gson;

@SuppressWarnings("serial")
public class NewFamousUserServlet extends HttpServlet {
  
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    
    long oldFamousUser = IsFamousServlet.getUserIdOfFamousUser();
    long newFamousUser = selectRandomUserId(oldFamousUser);
    
    // Make the Instagram API calls.
    makeFamous(newFamousUser, oldFamousUser);
    
    // Set the new famous user in the DB.
    Entity famousUser = new Entity("FamousUser");
    famousUser.setProperty("user_id", newFamousUser);
    famousUser.setProperty("current_time", System.currentTimeMillis());

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(famousUser);
    
    resp.setContentType("application/json");
    UserResource ur = new UserResource(newFamousUser);
    resp.getWriter().println(new Gson().toJson(ur));
  }
  
  private static long selectRandomUserId(long oldFamousUser) {
    Query query = new Query("User");
    query.setFilter(new Query.FilterPredicate("user_id", FilterOperator.NOT_EQUAL, oldFamousUser));
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    List<Entity> results = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1000));
    Entity result = results.get(new Random().nextInt(results.size()));
    return UserResource.parseUserIdFromEntity(result);
  }
  
  // TODO(atm): this won't work until we actually have access tokens and real user IDs in the datastore.
  private static void makeFamous(long newFamousUser, long oldFamousUser) throws IOException {
    Query query = new Query("User");
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    List<Entity> results = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1000));
    System.out.println("====> going to make " + oldFamousUser + " unfamous");
    System.out.println("====> going to make " + newFamousUser + " hella famous");
    for (Entity result : results) {
      String accessToken = (String) result.getProperty("access_token");
      String unfollowRequestString = "https://api.instagram.com/v1/users/" + oldFamousUser + "/relationship";
      String followRequestString = "https://api.instagram.com/v1/users/" + newFamousUser + "/relationship";
      Map<String, String> postParams = new HashMap<String, String>();
      postParams.put("access_token", accessToken);
      postParams.put("action", "unfollow");
      System.out.println("====> unfollow result: " + HttpUtil.doHttpPost(unfollowRequestString, postParams));
      postParams.put("action", "follow");
      System.out.println("====> follow result: " + HttpUtil.doHttpPost(followRequestString, postParams));
    }
  }
}

@@ -161,16 +161,49 @@
 .Random;
+%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -792,16 +792,111 @@
 et %7B%0A  %0A
+  private static final Logger log = Logger.getLogger(NewFamousUserServlet.class.getName());%0A  %0A
   public
@@ -2212,113 +2212,8 @@
 %0A  %0A
-  // TODO(atm): this won't work until we actually have access tokens and real user IDs in the datastore.%0A
   pr
@@ -2521,34 +2521,24 @@
 ));%0A    
-System.out.println
+log.info
 (%22====%3E 
@@ -2589,34 +2589,24 @@
 %22);%0A    
-System.out.println
+log.info
 (%22====%3E 
@@ -3154,34 +3154,24 @@
 ;%0A      
-System.out.println
+log.info
 (%22====%3E 
@@ -3300,26 +3300,16 @@
     
-System.out.println
+log.info
 (%22==

Switch to legit appengine logging.