package org.azavea.otm.rest;

import java.io.UnsupportedEncodingException;

import org.azavea.otm.data.Plot;
import org.azavea.otm.data.User;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;

public class RequestGenerator {
	private RestClient client;
	
	public RequestGenerator() {
		client = new RestClient();
	}
	
	// Dependency injection to support unit-testing
	public void setClient(RestClient client) {
		this.client = client;
	}
	
	public void getVersion(JsonHttpResponseHandler handler) {
		client.get("/version", null, handler);
	}
	
	public void getPlot(int id, JsonHttpResponseHandler handler) {
		client.get("/plots/" + id, null, handler);
	}
	
	public void updatePlot(Context context, int id, Plot plot, AsyncHttpResponseHandler handler) throws UnsupportedEncodingException {
		client.putWithAuthentication(context, "/plots/", "administrator", "123456", id, plot, handler);
	}
	
	public void addUser(Context context, User user, AsyncHttpResponseHandler handler) throws JSONException, UnsupportedEncodingException {
		client.postWithAuthentication(context, "/user/", "administrator", "123456", user, handler);
	}
	
	public void logIn(Context context, String username, String password, JsonHttpResponseHandler handler) {
		client.getWithAuthentication(context, "/login", username, password, null, handler);
	}
}

@@ -69,16 +69,79 @@
 ption;%0A%0A
+import org.azavea.otm.App;%0Aimport org.azavea.otm.LoginManager;%0A
 import o
@@ -488,16 +488,68 @@
 client;%0A
+%09LoginManager loginManager = App.getLoginManager();%0A
 %09%0A%09publi
@@ -1006,16 +1006,20 @@
 t plot, 
+%0A%09%09%09
 AsyncHtt
@@ -1074,32 +1074,78 @@
 dingException %7B%0A
+%09%09if (loginManager.isLoggedIn()) %7B%0A%09%09%09try %7B%0A%09%09
 %09%09client.putWith
@@ -1183,59 +1183,234 @@
 /%22, 
-%22administrator%22, %22123456%22, id, plot, handler);%0A%09%7D%0A%09
+%0A%09%09%09%09%09%09loginManager.loggedInUser.getUserName(), %0A%09%09%09%09%09%09loginManager.loggedInUser.getPassword(), id, plot, handler);%0A%09%09%09%7D catch (JSONException e) %7B%0A%09%09%09%09handleBadResponse(e);%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09redirectToLoginActivity();%0A%09%09%7D%0A%09%7D%0A
 %0A%09pu
@@ -1485,24 +1485,28 @@
 er handler) 
+%0A%09%09%09
 throws JSONE
@@ -1601,33 +1601,98 @@
 /%22, 
-%22administrator%22, %22123456%22
+%0A%09%09%09%09loginManager.loggedInUser.getUserName(), %0A%09%09%09%09loginManager.loggedInUser.getPassword()
 , us
@@ -1780,16 +1780,20 @@
 ssword, 
+%0A%09%09%09
 JsonHttp
@@ -1908,10 +1908,212 @@
 er);%0A%09%7D%0A
+%09%0A%09private void handleBadResponse(JSONException e) %7B%0A%09%09Log.e(App.LOG_TAG, %22Unable to parse JSON response%22, e);%0A%09%7D%09%0A%09%0A%09private void redirectToLoginActivity() %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09%0A%09%7D%0A
 %7D%0A

Add login manager to request generator