package com.ibm.smartgwt.defect.client;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.ibm.smartgwt.defect.shared.OrderItem;
import com.ibm.smartgwt.defect.shared.OrderItemLocalDS;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.GroupStartOpen;
import com.smartgwt.client.types.RecordSummaryFunctionType;
import com.smartgwt.client.types.SummaryFunctionType;
import com.smartgwt.client.widgets.grid.CellFormatter;
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.grid.ListGridSummaryField;
import com.smartgwt.client.widgets.grid.SummaryFunction;
import com.smartgwt.client.widgets.grid.events.CellContextClickEvent;
import com.smartgwt.client.widgets.grid.events.CellContextClickHandler;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SmartListGridDefect implements EntryPoint {
	public void onModuleLoad() {
		DataSource dataSource = OrderItemLocalDS.getInstance();

		ListGridField orderIdField = new ListGridField("orderID");
		orderIdField.setIncludeInRecordSummary(false);
		orderIdField.setSummaryFunction(SummaryFunctionType.COUNT);

		ListGridField itemDescriptionField = new ListGridField(
				"itemDescription");

		ListGridField categoryField = new ListGridField("category");
		categoryField.setShowGridSummary(true);
		categoryField.setSummaryFunction(new SummaryFunction() {
			public Object getSummaryValue(Record[] records, ListGridField field) {
				Set<String> uniqueCategories = new HashSet<String>();

				for (int i = 0; i < records.length; i++) {
					// convert each supplied Record to an OrderItem if it's not
					// one already
					OrderItem item = records[i] instanceof OrderItem ? (OrderItem) records[i]
							: new OrderItem(records[i]);
					uniqueCategories.add(item.getCategory());
				}
				return uniqueCategories.size() + " Categories";
			}
		});

		ListGridField shipDateField = new ListGridField("shipDate");
		shipDateField.setShowGroupSummary(true);
		shipDateField.setShowGridSummary(false);
		shipDateField.setSummaryFunction(SummaryFunctionType.MAX);

		ListGridField quantityField = new ListGridField("quantity");
		quantityField.setShowGroupSummary(false);
		quantityField.setShowGridSummary(false);

		ListGridField unitPriceField = new ListGridField("unitPrice");
		unitPriceField.setAlign(Alignment.RIGHT);
		unitPriceField.setCellFormatter(new CellFormatter() {
			public String format(Object value, ListGridRecord record,
					int rowNum, int colNum) {
				if (value == null)
					return null;
				try {
					NumberFormat nf = NumberFormat.getFormat("#,##0.00");
					return "$" + nf.format(((Number) value).doubleValue());
				} catch (Exception e) {
					return value.toString();
				}
			}
		});
		unitPriceField.setShowGroupSummary(false);
		unitPriceField.setShowGridSummary(false);

		ListGridSummaryField totalField = new ListGridSummaryField("total",
				"Total");
		totalField.setAlign(Alignment.RIGHT);
		totalField
				.setRecordSummaryFunction(RecordSummaryFunctionType.MULTIPLIER);
		totalField.setSummaryFunction(SummaryFunctionType.SUM);
		totalField.setShowGridSummary(true);
		totalField.setShowGroupSummary(true);
		totalField.setCellFormatter(new CellFormatter() {
			public String format(Object value, ListGridRecord record,
					int rowNum, int colNum) {
				if (value == null)
					return null;
				try {
					NumberFormat nf = NumberFormat.getFormat("#,##0.00");
					return "$" + nf.format(((Number) value).doubleValue());
				} catch (Exception e) {
					return value.toString();
				}
			}
		});

		final ListGrid listGrid = new ListGrid();

		listGrid.setWidth(600);
		listGrid.setHeight(520);
		listGrid.setAutoFetchData(true);

		listGrid.setShowAllRecords(true);
		listGrid.setDataSource(dataSource);
		listGrid.setCanEdit(true);
		listGrid.setGroupByField("category");
		listGrid.setGroupStartOpen(GroupStartOpen.ALL);
		listGrid.setShowGridSummary(true);
		listGrid.setShowGroupSummary(true);

		listGrid.setFields(orderIdField, itemDescriptionField, categoryField,
				shipDateField, quantityField, unitPriceField, totalField);
		
		listGrid.addCellContextClickHandler(new CellContextClickHandler() {
			
			@Override
			public void onCellContextClick(CellContextClickEvent event) {
				System.out.println("HERE WE GO");
				Menu menu = new Menu();
				MenuItem mi = new MenuItem("ABCD");
				menu.addItem(mi);
				listGrid.setContextMenu(menu);
				
			}
		});

		listGrid.draw();
	}

}