package com.sbs.jpm.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeGridField;

public class TestCase implements EntryPoint {

	private Panel panel;
	private TestDataSource dataSource;

	public void onModuleLoad() {

		SC.showConsole();

		dataSource = new TestDataSource();

		panel = new Panel();
		panel.setWidth100();
		panel.setHeight100();

		RootPanel.get().add(panel);
	}

	public class Panel extends VLayout {

		private ListGrid grid;
		private TreeGridField nameGridField;
		private DynamicForm form;

		public Panel() {

			nameGridField = new TreeGridField();
			nameGridField.setName(TestDataSource.NAME_FIELD);
			nameGridField.setTitle("Name");

			grid = new ListGrid();
			grid.setWidth(500);
			grid.setHeight(500);
			grid.setDataSource(dataSource);
			grid.setAutoFetchData(true);
			grid.setFields(nameGridField);
			grid.addSelectionChangedHandler(new SelectionChangedHandler() {

				@Override
				public void onSelectionChanged(SelectionEvent event) {
					if (event.getState()) {
						form.editRecord(grid.getSelectedRecord());
					}
				}
			});

			TextItem nameField = new TextItem();
			nameField.setTitle("Name");
			nameField.setName(TestDataSource.NAME_FIELD);

			form = new DynamicForm();
			form.setWidth(500);
			form.setHeight(120);
			form.setIsGroup(true);
			form.setGroupTitle("Details");
			form.setDataSource(dataSource);
			form.setFields(nameField);
			form.addDrawHandler(new DrawHandler() {

				@Override
				public void onDraw(DrawEvent event) {
					AdvancedCriteria criteria = new AdvancedCriteria(TestDataSource.UNIQUE_ID_FIELD, OperatorId.EQUALS,
							2l);
					form.fetchData(criteria);
				}
			});

			Button button = new Button();
			button.setTitle("Update cache");
			button.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					dataSource.updateCacheTest();
				}
			});

			this.setMembers(button, grid, form);
		}
	}

	public class TestDataSource extends DataSource {

		private static final String DS_UNIQUE_ID = "_DataSource";

		public static final String UNIQUE_ID_FIELD = "uniqueIdField" + DS_UNIQUE_ID;
		public static final String NAME_FIELD = "nameField" + DS_UNIQUE_ID;

		private TestDataSource() {

			DataSourceField uniqueId = new DataSourceField();
			uniqueId.setName(UNIQUE_ID_FIELD);
			uniqueId.setPrimaryKey(true);
			uniqueId.setHidden(true);
			uniqueId.setType(FieldType.INTEGER);

			ListGridRecord node1 = new ListGridRecord();
			node1.setAttribute(UNIQUE_ID_FIELD, 2l);
			node1.setAttribute(NAME_FIELD, "Charles Madigen");

			ListGridRecord node2 = new ListGridRecord();
			node2.setAttribute(UNIQUE_ID_FIELD, 3l);
			node2.setAttribute(NAME_FIELD, "Rogine Leger");

			ListGridRecord node3 = new ListGridRecord();
			node3.setAttribute(UNIQUE_ID_FIELD, 4l);
			node3.setAttribute(NAME_FIELD, "Gene Porter");

			this.setID(DS_UNIQUE_ID);
			this.setClientOnly(true);
			this.setFields(uniqueId);
			this.setCacheData(new ListGridRecord[] { node1, node2, node3 });
		}

		public void updateCacheTest() {
			ListGridRecord node1 = new ListGridRecord();
			node1.setAttribute(UNIQUE_ID_FIELD, 2l);
			node1.setAttribute(NAME_FIELD, "Charles Madigen" + new Date().getTime());

			ListGridRecord node2 = new ListGridRecord();
			node2.setAttribute(UNIQUE_ID_FIELD, 3l);
			node2.setAttribute(NAME_FIELD, "Rogine Leger" + new Date().getTime());

			ListGridRecord node3 = new ListGridRecord();
			node3.setAttribute(UNIQUE_ID_FIELD, 4l);
			node3.setAttribute(NAME_FIELD, "Gene Porter" + new Date().getTime());

			DSResponse dsResponse = new DSResponse();
			dsResponse.setData(new ListGridRecord[] { node1, node2, node3 });
			dsResponse.setOperationType(DSOperationType.UPDATE);
			updateCaches(dsResponse);
		}
	}
}
