package com.oatsystems.raf.smartgwt.client.chart;

import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SummaryFunctionType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.SummaryFunction;
import com.smartgwt.client.widgets.layout.VLayout;

public class TestGridLayout extends VLayout {
    public ListGrid grid;

    public TestGridLayout() {

        setLayoutMargin(20);
        setHeight100();
        setWidth100();
        setBackgroundColor("#ABBCE1");

        grid = createGrid();
        addMember(grid);
    }

    private ListGrid createGrid() {
        ListGrid grid;

        DataSource dataSource = new DataSource();
        dataSource.setDataFormat(DSDataFormat.JSON);

        DataSourceTextField primaryKeyField = new DataSourceTextField("primaryKey", "Department");
        DataSourceIntegerField accuracyField = new DataSourceIntegerField("accuracy", "Accuracy");

        dataSource.setFields(primaryKeyField, accuracyField);

        dataSource.setDataURL("data/TestData.js");

        ListGridField departmentlgField = new ListGridField("primaryKey", "Department");
        ListGridField accuracylgField = new ListGridField("accuracy", "Accuracy");
        accuracylgField.setShowGridSummary(true);
        accuracylgField.setSummaryFunction(/*SummaryFunctionType.SUM);*/
        new SummaryFunction() {

            public Object getSummaryValue(Record[] records, ListGridField field) {
                Integer totalExpected = 0;
                for (int i = 0; i < records.length; i++) {
                    totalExpected += records[i].getAttributeAsInt("accuracy");
                }

                String value = "";
                try {
                    NumberFormat nf = NumberFormat.getFormat("#,##0");
                    value = nf.format(((Number) totalExpected));
                } catch (Exception e) {
                }

                return value;
            }
        });

        ListGridField improveAccuracylgField = new ListGridField("improveAccuracy",
                "Improve Accuracy");
        improveAccuracylgField.setType(ListGridFieldType.BOOLEAN);
        improveAccuracylgField.setCanToggle(true);

        grid = new ListGrid() {
            protected boolean canEditCell(int row, int col) {
                return col == 2;
            }
        };

        grid.setFields(departmentlgField, accuracylgField, improveAccuracylgField);

        grid.setCanEdit(true);
        grid.setEditEvent(ListGridEditEvent.CLICK);
        grid.setShowGridSummary(true);
        grid.setAutoSaveEdits(false);
        grid.setAutoFitData(Autofit.VERTICAL);

        grid.setDataSource(dataSource);
        grid.setAutoFetchData(true);

        return grid;
    }
}
