package com.sbs.jpm.client;

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

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.types.TileLayoutPolicy;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
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.viewer.DetailViewerField;

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 TileGrid tileGrid;

		public Panel() {

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

			this.setMembers(tileGrid);
		}
	}

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

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

			nameField = new Label();
			nameField.setHeight(20);

			taskPanel = new VLayout();
			taskPanel.addMember(nameField);

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

		public TestTile(Record record, Integer tileIndex) {
			this();
			nameField.setContents(record.getAttributeAsString(TestDataSource.NAME_FIELD));
		}

		public String getInnerHTML(){
			return "";
		}
		
		public String getDragTracker() {
			return "<pre>" + nameField.getTitle() + "</pre>";
		}
	}

	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);
			setFields(new DetailViewerField(TestDataSource.NAME_FIELD));
			
			setTileConstructor(TestTile.class.getName());
			setCreateTileCustomizer(new CreateTileCustomizer() {
				@Override
				public Canvas createTile(Record record, Integer tileIndex) {
					return new TestTile(record, tileIndex);
				}
			});

		}
	}

	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;

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

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

			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);
				records.add(node);
			}

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