package com.acme.main.client;

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

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
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.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.GroupStartOpen;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;

public class Main implements EntryPoint {

	@Override
	public void onModuleLoad () {

		final DataSource dataSource = new DataSource ();
		dataSource.setClientOnly (true);
		
		final Record changingRecord = new Record ();
		
		final List<Record> testData = new ArrayList<Record> ();
		{//data initialization
			{
				final Record record = new Record ();
				record.setAttribute ("id", "1");
				record.setAttribute ("name", "row 1");
				testData.add (record);
			}
			{
				changingRecord.setAttribute ("id", "2");
				changingRecord.setAttribute ("name", "row 2 (should change)");
				testData.add (changingRecord);
			}

			Record[] listGridRecords = testData.toArray (new Record[testData.size ()]);
			dataSource.setTestData (listGridRecords);
		}

		{//datasource fields initialization
			final DataSourceTextField keyField = new DataSourceTextField ("id", "ID");
			keyField.setPrimaryKey (true);
			final DataSourceTextField nameField = new DataSourceTextField ("name", "Name");
			
			dataSource.setFields (keyField, nameField);
		}
		
		//UI initialization
		final VLayout layout = new VLayout ();
		layout.setMembersMargin (5);
		layout.setMargin (20);
		layout.setWidth (400);
		layout.setHeight (400);
		
		final ListGrid sortedListGrid = createListGrid (dataSource, changingRecord, layout, true);
		final ListGrid listGrid = createListGrid (dataSource, changingRecord, layout, false);
		
		final Label hintsLabel = new Label ("Trigger cache update on row 2 name field");
		hintsLabel.setHeight (20);
		layout.addMember (hintsLabel);
		{
			final Button button = new Button ("Update caches");
			button.addClickHandler (new ClickHandler() {
				
				@Override
				public void onClick (ClickEvent event) {
					final DSResponse dsResponse = new DSResponse ();
					
					Record record = new Record (changingRecord.toMap ());
					record.setAttribute ("name", "row 2 with changed value!!!");
					dsResponse.setData (new Record [] {record});
					dsResponse.setOperationType (DSOperationType.UPDATE);
					dataSource.updateCaches (dsResponse);
					
				}
			});
	        layout.addMember(button);
		}
		
		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 ();
			}
		});
		

		
		layout.show ();
	}

	private ListGrid createListGrid (final DataSource dataSource, final Record changingRecord, final VLayout layout, final boolean groupBy) {
		final ListGrid listGrid = new ListGrid ();
		listGrid.setWidth100 ();
		listGrid.setHeight100 ();
		listGrid.setAutoFetchData (true);
		listGrid.setDataSource (dataSource);
		
        final Label label;
		if (groupBy) {
			label = new Label ("Grouped grid");
			listGrid.setGroupByField ("id");
		} else {
			label = new Label ("Grid with no grouping");
		}
		listGrid.setGroupStartOpen (GroupStartOpen.ALL);
		label.setHeight (20);
		layout.addMember(label);

		layout.addMember (listGrid);
		
		return listGrid;
	}

}
