package com.sbs.jpm.client;

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

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
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.HLayout;
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();

		panel.draw();
	}

	public class Panel extends VLayout {

		private ListGrid grid;
		private TreeGridField nameGridField;
		private TreeGridField startDateField;

		private HLayout rollOverCanvas;

		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() {
				protected Canvas getRollOverCanvas(Integer rowNum, Integer colNum) {
					if (rollOverCanvas == null) {
						rollOverCanvas = new HLayout(3);
						rollOverCanvas.setSnapTo("R");
						rollOverCanvas.setWidth(22);
						rollOverCanvas.setHeight(22);

						ImgButton editImg = new ImgButton();
						editImg.setShowDown(false);
						editImg.setShowRollOver(false);
						editImg.setLayoutAlign(Alignment.CENTER);
						editImg.setSrc("Details.png");
						editImg.setHeight(16);
						editImg.setWidth(16);
						editImg.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
							public void onClick(ClickEvent event) {
							}
						});

						rollOverCanvas.addMember(editImg);
					}
					return rollOverCanvas;
				}
			};
			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.setShowRollOverCanvas(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);
		}
	}



	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);

			List<ListGridRecord> records = new LinkedList<ListGridRecord>();
			for (int i = 0; i < 10000; i++) {
				ListGridRecord node = new ListGridRecord();
				node.setAttribute(UNIQUE_ID_FIELD, i);
				node.setAttribute(NAME_FIELD, "Charles Madigen" + i);
				node.setAttribute(START_DATE_FIELD, new Date());
				node.setAttribute("division", "Marketing");
				node.setAttribute("department", 1);
				records.add(node);
			}

			this.setID(DS_UNIQUE_ID);
			this.setClientOnly(true);
			this.setFields(uniqueId, name, startDate);
			this.setCacheData(records.toArray(new ListGridRecord[0]));
		}

		public void updateCacheTest() {
			List<ListGridRecord> records = new LinkedList<ListGridRecord>();
			for (int i = 0; i < 10000; i++) {
				ListGridRecord node = new ListGridRecord();
				node.setAttribute(UNIQUE_ID_FIELD, i);
				node.setAttribute(NAME_FIELD, "Charles Madigen" + i);
				node.setAttribute(START_DATE_FIELD, new Date());
				records.add(node);
			}
			SC.logWarn("before update");
			DSResponse dsResponse = new DSResponse();
			dsResponse.setData(records.toArray(new ListGridRecord[0]));
			dsResponse.setOperationType(DSOperationType.UPDATE);
			updateCaches(dsResponse);
			SC.logWarn("after update");
		}
	}
}
