package com.sbs.jpm.client;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
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.SelectionStyle;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.types.TileLayoutPolicy;
import com.smartgwt.client.util.EventHandler;
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.KeyPressEvent;
import com.smartgwt.client.widgets.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.EditorValueMapFunction;
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;
import com.smartgwt.client.widgets.tile.SimpleTile;
import com.smartgwt.client.widgets.tile.TileGrid;
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 TileGrid tileGrid;
		private TreeGridField nameGridField;
		private TreeGridField startDateField;

		public Panel() {

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

			startDateField = new TreeGridField();
			startDateField.setName(TestDataSource.START_DATE_FIELD);
			startDateField.setTitle("Start date");

			ListGridField divisionField = new ListGridField("division", "Division");
			SelectItem divisionSelectItem = new SelectItem();
			divisionSelectItem.setValueMap("Marketing", "Sales", "Services");
			divisionSelectItem.addChangedHandler(new ChangedHandler() {
				public void onChanged(ChangedEvent event) {
					// calling 'setValueMap()' will force the 'getEditorValueMap' method to be
					// re-evaluated for the department field
					grid.setValueMap("department", new LinkedHashMap());
				}
			});
			divisionField.setEditorProperties(divisionSelectItem);

			ListGridField departmentField = new ListGridField("department", "Department");

			final Map<String, String[]> departments = new HashMap<String, String[]>();
			departments.put("Marketing", new String[] { "Advertising", "Community Relations" });
			departments.put("Sales", new String[] { "Channed Sales", "Direct Sales" });
			departments.put("Manufacturing", new String[] { "Design", "Development", "QA" });
			departments.put("Services", new String[] { "Support", "Consulting" });

			SelectItem departmentSelectItem = new SelectItem();
			departmentSelectItem.setAddUnknownValues(false);
			departmentField.setEditorValueMapFunction(new EditorValueMapFunction() {
				public Map getEditorValueMap(Map values, ListGridField field, ListGrid grid) {
					String division = (String) values.get("division");
					String[] divisions = departments.get(division);

					// convert divisions into ValueMap. In this case we simply create a Map with
					// same key -> value since
					// stored value is the same as user displayable value
					Map<Integer, String> valueMap = new HashMap<Integer, String>();
					for (int i = 0; i < divisions.length; i++) {
						String val = divisions[i];
						valueMap.put(i + 1, val);
					}
					return valueMap;
				}
			});
			departmentField.setEditorProperties(departmentSelectItem);

			grid = new ListGrid();
			grid.setWidth(500);
			grid.setHeight(500);
			grid.setCanEdit(true);
			grid.setEditByCell(true);
			grid.setDataSource(dataSource);
			grid.setAutoFetchData(true);
			grid.setFields(nameGridField, startDateField, divisionField, departmentField);

			grid.addKeyPressHandler(new KeyPressHandler() {
				public void onKeyPress(KeyPressEvent event) {
					if (event.getKeyName().equals("Arrow_Up") && EventHandler.ctrlKeyDown()) {
						event.cancel();
						SC.say(EventHandler.getKey());
					}
				}
			});

			tileGrid = new TestTileGrid();
			tileGrid.setWidth(500);
			tileGrid.setHeight(500);
			tileGrid.setDataSource(dataSource);
			tileGrid.setAutoFetchData(true);

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

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

	public class TestTileGrid extends TileGrid {
		public TestTileGrid() {
			setLayoutPolicy(TileLayoutPolicy.FLOW);
			setTilesPerLine(1);
			setCanReorderTiles(false);
			setHeight("100%");
			setWidth(250);
			setBorder("1px solid grey");
			setSelectionType(SelectionStyle.SINGLE);
			setShowAllRecords(true);
			setAnimateTileChange(true);
			setCanDragTilesOut(true);
			setCanAcceptDroppedRecords(true);
			setAddDropValues(true);
			setAutoFetchTextMatchStyle(TextMatchStyle.EXACT);
		}

		@Override
		public SimpleTile getCurrentTile() {
			SC.say("Enters");
			SimpleTile tile = super.getCurrentTile();

			tile.setBackgroundColor("red");

			return tile;
		}
	}

	public class TestDataSource extends DataSource {

		private static final String DS_UNIQUE_ID = "_TestDataSource";

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

		private TestDataSource() {

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

			DataSourceField name = new DataSourceField();
			name.setName(NAME_FIELD);

			DataSourceField startDate = new DataSourceField();
			startDate.setName(START_DATE_FIELD);
			startDate.setType(FieldType.DATE);

			ListGridRecord node1 = new ListGridRecord();
			node1.setAttribute(UNIQUE_ID_FIELD, 2l);
			node1.setAttribute(NAME_FIELD, "Charles Madigen");
			node1.setAttribute(START_DATE_FIELD, new Date());
			node1.setAttribute("division", "Marketing");
			node1.setAttribute("department", 1);

			ListGridRecord node2 = new ListGridRecord();
			node2.setAttribute(UNIQUE_ID_FIELD, 3l);
			node2.setAttribute(NAME_FIELD, "Rogine Leger");
			node2.setAttribute(START_DATE_FIELD, new Date(new Date().getTime() - 7 * 24 * 3600 * 1000));
			node2.setAttribute("division", "Marketing");
			node2.setAttribute("department", 1);

			ListGridRecord node3 = new ListGridRecord();
			node3.setAttribute(UNIQUE_ID_FIELD, 4l);
			node3.setAttribute(NAME_FIELD, "Gene Porter");
			node3.setAttribute(START_DATE_FIELD, new Date(new Date().getTime() + 7 * 24 * 3600 * 1000));
			node3.setAttribute("division", "Marketing");
			node3.setAttribute("department", 1);

			this.setID(DS_UNIQUE_ID);
			this.setClientOnly(true);
			this.setFields(uniqueId, name, startDate);
			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);
		}
	}
}
