package test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestDS implements EntryPoint {

	final ListGrid _yourGrid = new ListGrid();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			
			@Override
			public void onUncaughtException(Throwable e) {
				SC.say("onUncaughtException: You won't see this");
			}
		});
				
			
		
		// /////////////////////////////////////////
		// Grid1: //////////////////////////////
		// ///////////////////////////////////////

		_yourGrid.setDataSource(YourDataSource.getInstance());

		_yourGrid.setWidth(700);
		_yourGrid.setHeight(400);

		ListGridField nombreField2 = new ListGridField("name", "Name");
		ListGridField locationField2 = new ListGridField("location", "Location");
		ListGridField idField2 = new ListGridField("id", "Id");
		_yourGrid.setFields(idField2, nombreField2, locationField2);

		_yourGrid.setAutoFetchData(true);

		IButton botonIncluirRecordEnOtraGrid = new IButton("Add Record");
		botonIncluirRecordEnOtraGrid
				.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {

					public void onClick(
							com.smartgwt.client.widgets.events.ClickEvent event) {

						addRecordToDS();

					}
				});
		
		IButton botonConsola = new IButton("Console");
		botonConsola
				.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {

					public void onClick(
							com.smartgwt.client.widgets.events.ClickEvent event) {
						SC.showConsole();
					}
				});

		IButton botonRecuperarInfo = new IButton("Get Info Record");
		botonRecuperarInfo
				.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {

					public void onClick(
							com.smartgwt.client.widgets.events.ClickEvent event) {

						Record record = _yourGrid.getSelectedRecord();
						if (record != null) {
							YourDataObject yourObject = (YourDataObject) record
									.getAttributeAsObject("Object");
							SC.say("Info in Record: " + yourObject.getName()
									+ ":" + yourObject.getLocation());
						} else
							SC.say("Please, select a record!");

					}
				});

		VLayout vLayaout = new VLayout();
		vLayaout.addMember(botonConsola);
		vLayaout.addMember(botonIncluirRecordEnOtraGrid);
		vLayaout.addMember(botonRecuperarInfo);

		RootPanel.get("nameFieldContainer").add(_yourGrid);
		RootPanel.get("sendButtonContainer").add(vLayaout);
	}

	private void addRecordToDS() {
		// try {
		YourDataObject yourData = new YourDataObject();
		ListGridRecord myRecord = new ListGridRecord();
		myRecord.setAttribute("name", yourData.getName());
		myRecord.setAttribute("location", yourData.getLocation());

		// If I comment this line works!!! if not, it fails in debug mode only
		myRecord.setAttribute("Object", yourData);

		YourDataSource.getInstance().addData(myRecord, new DSCallback() {

			public void execute(DSResponse response, Object rawData,
					DSRequest request) {
				_yourGrid.fetchData();

			}
		});

		// } catch (Exception ex) {
		// SC.say("MyProblem",
		// "This is my problem!!! :(    - Exception:"
		// + ex.getMessage());
		// }
	}

	public static class YourDataSource extends DataSource {

		private static YourDataSource instance = null;

		public static YourDataSource getInstance() {
			if (instance == null) {
				instance = new YourDataSource();
			}
			return instance;
		}

		public YourDataSource() {

			DataSourceIntegerField idField = new DataSourceIntegerField("id");
			idField.setHidden(true);
			idField.setPrimaryKey(true);

			DataSourceTextField nameField = new DataSourceTextField("name",
					"Name");
			nameField.setRequired(true);

			DataSourceTextField locationField = new DataSourceTextField(
					"location", "Location");
			locationField.setRequired(true);

			setFields(idField, nameField, locationField);
			setClientOnly(true);

		}
	}

	public class YourDataObject implements IsSerializable {

		private Integer id;
		private String name;
		private String location;

		public Integer getId() {
			return id;
		}

		public void setId(Integer id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getLocation() {
			return location;
		}

		public void setLocation(String location) {
			this.location = location;
		}

		public YourDataObject() {
			setName("unNombre");
			setLocation("unaLocalizacion");
		}
	}
}
