package com.pankaj.gwt.example.client.listGrid;

/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.RecordSummaryFunctionType;
import com.smartgwt.client.types.SummaryFunctionType;
import com.smartgwt.client.widgets.grid.*;

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

public class GridSummariesSample implements EntryPoint {

    public void onModuleLoad() {
        DataSource dataSource = OrderItemLocalDS.getInstance();

        ListGridField orderIdField = new ListGridField("orderID");
        orderIdField.setHidden(false);
        orderIdField.setIncludeInRecordSummary(false);
        orderIdField.setSummaryFunction(SummaryFunctionType.COUNT);
        orderIdField.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");
                    return nf.format(((Number) value).intValue());
                } catch (Exception e) {
                    return value.toString();
                }
            }
        });

        orderIdField.setWidth(100);

        ListGridField categoryField = new ListGridField("category");
        categoryField.setHidden(false);
        categoryField.setWidth(100);
        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";
            }
        });

        ListGridSummaryField totalField = new ListGridSummaryField("total", "Total");
        totalField.setHidden(false);
        totalField.setWidth(100);
        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();
                }
            }
        });

        ListGridField itemDescriptionField = new ListGridField("itemDescription");
        itemDescriptionField.setWidth(100);
        itemDescriptionField.setHidden(true);

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

        ListGridField quantityField = new ListGridField("quantity");

        quantityField.setWidth(100);
        quantityField.setShowGroupSummary(false);
        quantityField.setShowGridSummary(false);

        ListGridField unitPriceField = new ListGridField("unitPrice");
        unitPriceField.setHidden(true);
        unitPriceField.setWidth(100);
        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);


//        final MyListGrid listGrid = new MyListGrid();
        final ListGrid listGrid = new ListGrid();
        listGrid.setAutoFitFieldWidths(true);
        listGrid.setCanDragScroll(true);

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


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

//        listGrid.setShowAllRecords(true);
        listGrid.setDataPageSize(58);

        listGrid.setFields(itemDescriptionField, quantityField, shipDateField, unitPriceField, orderIdField, categoryField, totalField);

        listGrid.draw();
    }

}

