package com.acme.main.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.Criteria;
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.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.VLayout;

public class Main implements EntryPoint {

	private RestDataSource otherDataSource;

	@Override
	public void onModuleLoad () {

		otherDataSource = new RestDataSource ();
		otherDataSource.setDataFormat (DSDataFormat.JSON);
		otherDataSource.setFetchDataURL ("gwt/data.js");

		
		//datasource initialization
		final DataSource dataSource = new DataSource ();
		{//datasource fields initialization
			final List<ListGridRecord> testData = new ArrayList<ListGridRecord> ();
			{
				final ListGridRecord record = new ListGridRecord ();
				record.setAttribute ("id", 1);
				record.setAttribute ("name", "1st record");
				testData.add (record);
			}
			{
				final ListGridRecord record = new ListGridRecord ();
				record.setAttribute ("id", 2);
				record.setAttribute ("name", "2nd record");
				testData.add (record);
			}
			dataSource.setTestData (testData.toArray (new ListGridRecord[0]));
		}
		dataSource.setClientOnly (true);
		{
			final DataSourceIntegerField dsfId = new DataSourceIntegerField ("id");
			dsfId.setPrimaryKey (true);
			dataSource.addField (dsfId);
			final DataSourceTextField dsfName = new DataSourceTextField ("name");

			dataSource.addField (dsfName);
		}
		
		//UI initialization
		final VLayout layout = new VLayout ();

		final ListGrid grid = new ListGrid ();
		grid.addRecordDoubleClickHandler (new RecordDoubleClickHandler() {
			
			@Override
			public void onRecordDoubleClick (RecordDoubleClickEvent event) {
				GWT.log ("double click");
				SC.say ("Double click!");
			}
		});
		grid.addSelectionChangedHandler (new SelectionChangedHandler() {
			@Override
			public void onSelectionChanged (SelectionEvent event) {
				fetchData ();
			}
		});
		
//		grid.addSelectionUpdatedHandler (new SelectionUpdatedHandler() {
//			
//			@Override
//			public void onSelectionUpdated (SelectionUpdatedEvent event) {
//				fetchData ();
//			}
//		});
		
		
		final KeyIdentifier debugKey = new KeyIdentifier ();
		debugKey.setCtrlKey (true);
		debugKey.setShiftKey (true);
		debugKey.setKeyName ("D");

		Page.registerKey (debugKey, new KeyCallback () {
			@Override
			public void execute (final String keyName) {
				SC.showConsole ();
			}
		});
		
		grid.setAutoFetchData (true);
		grid.setDataSource (dataSource);
		
		layout.addMember (grid);
		layout.setWidth100 ();
		layout.setHeight100 ();
		layout.show ();

	}

	private void fetchData () {
		otherDataSource.invalidateCache ();
		otherDataSource.fetchData (new Criteria (), new DSCallback() {
			
			@Override
			public void execute (DSResponse response, Object rawData, DSRequest request) {
				GWT.log ("data fetched");
			}
		});
	}
}
