package net.foss.udy.rldemo.client;

import org.restlet.client.Request;
import org.restlet.client.Response;
import org.restlet.client.Uniform;
import org.restlet.client.data.Method;
import org.restlet.client.data.Status;
import org.restlet.client.resource.ClientResource;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTRestlet implements EntryPoint {
	/**
	 * This is the entry point method.
	 */
	private VerticalPanel mainPanel = new VerticalPanel();
	private Button sendRequestButton = new Button("Do Get");
	private TextBox httpMethodText = new TextBox();
	private TextBox urlText = new TextBox();
	private TextArea responseText = new TextArea();
	private Label errorLabel = new Label();

	public void onModuleLoad() {
		responseText.setText("<<Response will be here>>");

		httpMethodText.setText("GET");
		
		mainPanel.add(urlText);
		mainPanel.add(httpMethodText);
		mainPanel.add(responseText);
		mainPanel.add(sendRequestButton);
		
		mainPanel.add(errorLabel);

		// We can add style names to widgets
		sendRequestButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("restRspCont").add(mainPanel);

		// Focus the cursor on the name field when the app loads
		sendRequestButton.setFocus(true);

		// Listen for mouse events on the Add button.
		sendRequestButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//TODO: invoke RESTful here
				
				final ClientResource client = new ClientResource(urlText.getText());
				
				client.setOnResponse(new Uniform() {
					@Override
					public void handle(Request request, Response response) {
						Status status = response.getStatus();
						
						if(status.isSuccess()){
							try 
							{
								if(response.getEntity() != null){
									responseText.setText("Response Received: " + response.getEntity().getText());
								}
								else {
									responseText.setText("Undesirable Response");
								}
							}
							catch(Exception e) 
							{
								responseText.setText("Exception: " + e.getMessage());
							}
						}
						else {
							responseText.setText("Stat Code: " + status.getCode());
						}
					}
				});
				
				client.get();
			}
		});
	}
}