package com.ca.donner.ui.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.ResultSet;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ComboBoxScroll implements EntryPoint
{
	static
	{
		SC.setScreenReaderMode(true);
	}

	private static final String UNIQUE_ID = "uniqueId";
	private static final String ID = "id";
	private static final String NAME = "name";
	private static final String SOURCE_ACCOUNT = "sourceAccount";
	private static final String TYPE = "type";
	private final ComboBoxItem selector;

	public ComboBoxScroll()
	{
		this.selector = createSelector();
	}

	@Override
	public void onModuleLoad()
	{
		Canvas body = new Canvas();

		DynamicForm form = new DynamicForm();
		form.setItems(selector);
		body.addChild(form);

		body.draw();
	}

	private ComboBoxItem createSelector()
	{
		ComboBoxItem comboBox = new ComboBoxItem();
		comboBox.setOptionDataSource(createDataSource());
		comboBox.setCompleteOnTab(true);
		comboBox.setWidth(250);
		comboBox.setTitle("Accounts");
		comboBox.setBrowserSpellCheck(false);
		comboBox.setTextMatchStyle(TextMatchStyle.SUBSTRING);
		comboBox.setFilterLocally(true);
		comboBox.setCanFocus(true);

		ListGridField accountIdField = new ListGridField(ID, "Id", 50);
		ListGridField accountNameField = new ListGridField(NAME, "Name");
		ListGridField accountSourceField = new ListGridField(SOURCE_ACCOUNT, "Source Account", 175);
		ListGridField accountType = new ListGridField(TYPE, "Account Type", 90);

		accountNameField.setEscapeHTML(true);
		accountNameField.setWidth("*");

		comboBox.setPickListProperties(buildPickListProperties());
		comboBox.setPickListWidth(580);
		comboBox.setPickListFields(accountIdField, accountNameField, accountSourceField, accountType);
		comboBox.setFetchDelay(500);
		comboBox.setFilterFields(ID, NAME, SOURCE_ACCOUNT);
		comboBox.setDisplayField(NAME);
		comboBox.setValueField(UNIQUE_ID);

		return comboBox;
	}


	private ListGrid buildPickListProperties()
	{
		ListGrid pickListProperties = new ListGrid();
		pickListProperties.setShowHeaderContextMenu(false);
		pickListProperties.setShowHeader(true);
		pickListProperties.setAlternateRecordStyles(false);
		pickListProperties.setDrawAllMaxCells(800);
		pickListProperties.setDataPageSize(100);
		pickListProperties.setDrawAheadRatio(3);
		pickListProperties.setCanResizeFields(true);

		ResultSet dataProperties = new ResultSet();
		dataProperties.setUseClientFiltering(true);
		pickListProperties.setDataProperties(dataProperties);
		return pickListProperties;
	}

	private DataSource createDataSource()
	{
		DataSource ds = new DataSource();

		DataSourceTextField uniqueIdentifier = new DataSourceTextField("uniqueId");
		uniqueIdentifier.setPrimaryKey(true);
		uniqueIdentifier.setDetail(true);
		uniqueIdentifier.setHidden(true);
		ds.addField(uniqueIdentifier);

		ds.addField(new DataSourceIntegerField(ID));
		ds.addField(new DataSourceTextField(NAME, "AccountName"));
		ds.addField(new DataSourceTextField(SOURCE_ACCOUNT, "SourceAccount"));
		ds.addField(new DataSourceTextField(TYPE, "AccountType"));

		ds.setDataFormat(DSDataFormat.CUSTOM);
		ds.setClientOnly(true);
		ds.setTestData(createRecords());

		return ds;
	}

	private Record[] createRecords()
	{
		int numRecords = 10000;
		Record[] records = new Record[numRecords];
		for (int x = 0; x < numRecords; x++)
		{
			Record record = new ListGridRecord();
			record.setAttribute(UNIQUE_ID, "a-" + x);
			record.setAttribute(ID, x);
			record.setAttribute(NAME, x + "-NORM");
			record.setAttribute(SOURCE_ACCOUNT, "");
			record.setAttribute(TYPE, "Simple");
			records[x] = record;
		}

		return records;
	}
}
