package org.azavea.otm;

import org.azavea.otm.data.Plot;
import org.azavea.otm.data.User;
import org.azavea.otm.data.Version;
import org.azavea.otm.rest.RequestGenerator;
import org.azavea.otm.rest.handlers.RestHandler;
import org.json.JSONException;
import org.json.JSONObject;

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

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class RequestGeneratorUi extends Activity {
	private TextView output;
	private RequestGenerator rg;
	private Plot plot;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_generator);
        output = (TextView)findViewById(R.id.response);
        rg = new RequestGenerator();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_request_generator, menu);
        return true;
    }
    
    public void showVersion(View view) {
    	rg.getVersion(new RestHandler<Version>(new Version()) {
    		@Override
    		public void dataReceived(Version response) {
    			try {
    				output.setText(response.getOtmVersion() + ", " + response.getApiVersion());
    			} catch (JSONException e) {
    				output.setText("Exception: " + e.getMessage());
    			}
    		}
    	});
    }
    
    public void showPlot(View view) throws JSONException {
    	rg.getPlot(329, new RestHandler<Plot>(new Plot()) {
    		@Override
    		public void dataReceived(Plot response) {
    			try {
    				output.setText(response.getWidth() + "\n" + response.getTree().getSpeciesName() + "\n");
    				RequestGeneratorUi.this.plot = response;
    			} catch (JSONException e) {
    				output.setText("Exception: " + e.getMessage());
    			}
    		}
    	});
    }
    
    public void updatePlot(View view) throws JSONException {
    	//plot.setPowerlineConflictPotential("5");
    	plot.setWidth(plot.getWidth()+1);
    	try {
    		rg.updatePlot(this, 329, plot, null);
    	} catch (Exception e) {
    		output.setText(e.getMessage());
    	}
    }
    
    public void addUser(View view) throws JSONException {
    	EditText userName = (EditText)findViewById(R.id.userName);
    	EditText actualName = (EditText)findViewById(R.id.name);
    	EditText email = (EditText)findViewById(R.id.email);
    	EditText password = (EditText)findViewById(R.id.password);
    	
    	String[] names = actualName.getText().toString().split(" ");
    	User user = new User(userName.getText().toString(), names[0], names[1], email.getText().toString(), password.getText().toString(), "19087");
    	try {
    		rg.addUser(this, user, null);
    	} catch (Exception e) {
    		output.setText(e.getMessage());
    	}
    }
}

@@ -210,24 +210,51 @@
 estHandler;%0A
+import org.json.JSONArray;%0A
 import org.j
@@ -268,24 +268,24 @@
 NException;%0A
-
 import org.j
@@ -1354,24 +1354,239 @@
 ersion()) %7B%0A
+%09%09%09@Override%0A%09%09%09public void onSuccess(String resp) %7B%0A%09%09%09%09Log.d(App.LOG_TAG, resp);%0A%09%09%09%7D%0A    %09%09@Override%0A%09%09%09public void onFailure(Throwable e, JSONArray errorResponse)%7B%0A%09%09%09%09Log.e(App.LOG_TAG, %22version bad%22, e);%0A%09%09%09%7D%0A
     %09%09@Overr
@@ -2529,19 +2529,162 @@
  plot, n
-ull
+ew RestHandler%3CPlot%3E(new Plot()) %7B%0A    %09%09%09@Override%0A    %09%09%09public void onSuccess(String resp) %7B%0A    %09%09%09%09Log.d(App.LOG_TAG, resp);%0A    %09%09%09%7D%0A    %09%09%7D
 );%0A    %09
@@ -3332,11 +3332,292 @@
 r, n
-ull
+ew RestHandler%3CUser%3E(new User()) %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void onFailure(Throwable e, JSONArray errorResponse)%7B%0A%09%09%09%09%09Log.e(App.LOG_TAG, %22login bad%22, e);%0A%09%09%09%09%7D%0A    %09%09%09@Override%0A    %09%09%09public void dataReceived(User resp) %7B%0A    %09%09%09%09Log.d(App.LOG_TAG, %22user received%22);%0A    %09%09%09%7D%0A    %09%09%7D
 );%0A 

Add debugging http handler methods