package com.sbs.jpm.client;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

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.data.Record;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TileLayoutPolicy;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tile.CreateTileCustomizer;
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 = TestDataSource.getInstance();

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

		panel.draw();
	}

	public class Panel extends VLayout {

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

			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, tileGrid);
		}
	}

	public class TestTile extends SimpleTile {
		private TextItem nameField;
		private VLayout taskPanel;
		private VLayout mainLayout;

		public TestTile() {
			setHeight(1);
			setWidth("100%");
			setOverflow(Overflow.VISIBLE);

			nameField = new TextItem();
			nameField.setHeight(20);
			nameField.setShowTitle(false);
			nameField.addBlurHandler(new BlurHandler() {

				@Override
				public void onBlur(BlurEvent event) {
					// TODO Auto-generated method stub
					SC.logWarn("Enters blur");
				}
			});

			DynamicForm form = new DynamicForm();
			form.setFields(nameField);
			taskPanel = new VLayout();
			taskPanel.addMember(form);

			mainLayout = new VLayout();
			mainLayout.setHeight("1");
			mainLayout.setPadding(15);
			mainLayout.addMember(taskPanel);

			addChild(mainLayout);

		}

		public TestTile(Record record, Integer tileIndex) {
			this();
		}

		public String getInnerHTML() {
			return "";
		}

	}

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

			setTileConstructor(TestTile.class.getName());
			setCreateTileCustomizer(new CreateTileCustomizer() {
				@Override
				public Canvas createTile(Record record, Integer tileIndex) {
					return new TestTile(record, tileIndex);
				}
			});

		}
	}
}

class TestDataSource extends DataSource {

	private static TestDataSource instance = null;

	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;

	public static TestDataSource getInstance() {
		if (instance == null) {
			instance = new TestDataSource();
		}
		return instance;
	}

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

		List<ListGridRecord> records = new LinkedList<ListGridRecord>();
		records.add(node1);
		records.add(node2);
		records.add(node3);
		for (int i = 0; i < 21; i++) {
			ListGridRecord node = new ListGridRecord();
			node.setAttribute(UNIQUE_ID_FIELD, i + 10);
			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() {
		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);
	}
}