package com.hp.smartee.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.rpc.QueueSentCallback;
import com.smartgwt.client.rpc.RPCManager;
import com.smartgwt.client.rpc.RPCRequest;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.FilterBuilder;
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.layout.VStack;

/**
 * Engineer: Ahmad Igbaria
 * Date: May 19, 2010
 * Package: com.hp.smartee.client
 */

public class SmartEE implements EntryPoint {

    public void onModuleLoad() {
        DataSource dataSource = DataSource.get("worldDS");

        final FilterBuilder advancedFilter = new FilterBuilder();
        advancedFilter.setDataSource(dataSource);

        final ListGrid countryGrid = new ListGrid();
        countryGrid.setWidth(700);
        countryGrid.setHeight(224);
        countryGrid.setAlternateRecordStyles(true);
        countryGrid.setDataSource(dataSource);
        countryGrid.setAutoFetchData(true);
        countryGrid.setShowFilterEditor(true);
        countryGrid.setCanEdit(true);
        countryGrid.setEditEvent(ListGridEditEvent.CLICK);
        countryGrid.setCanRemoveRecords(true);

        ListGridField countryField = new ListGridField("countryName");
        ListGridField countryCodeField = new ListGridField("countryCode");
        ListGridField capitalField = new ListGridField("capital");
        ListGridField governmentField = new ListGridField("government");

        ListGridField continentField = new ListGridField("continent");
        ListGridField populationField = new ListGridField("population");
        ListGridField areaField = new ListGridField("area");
        ListGridField gdpField = new ListGridField("gdp");
        ListGridField independenceField = new ListGridField("independence");

        final NumberFormat nf = NumberFormat.getDecimalFormat();
        populationField.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                if (record == null) return null;
                return nf.format(((Number) value).doubleValue());
            }
        });

        areaField.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                if (record == null) return null;
                return nf.format(((Number) value).doubleValue());
            }
        });

        gdpField.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                if (record == null) return null;
                return nf.format(((Number) value).doubleValue());
            }
        });

        countryGrid.setFields(countryCodeField, countryField, capitalField, governmentField, continentField, independenceField, populationField, areaField, gdpField);

        IButton filterButton = new IButton("Filter");
        filterButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                countryGrid.filterData(advancedFilter.getCriteria());
            }
        });


        IButton editNew = new IButton("New Row");
        editNew.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                countryGrid.startEditingNew();
            }
        });

        IButton saveButton = new IButton("Save");
        editNew.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                countryGrid.saveAllEdits();
            }
        });


        VStack layout = new VStack(10);
        layout.addMember(advancedFilter);
        layout.addMember(filterButton);
        layout.addMember(countryGrid);
        layout.addMember(editNew);
        layout.addMember(saveButton);

        layout.draw();

        RPCManager.setQueueSentCallback(new QueueSentCallback() {
            public void queueSent(RPCRequest[] rpcRequests) {
                SC.say(rpcRequests[0].toString());
            }
        });

    }
}
