package com.acme.main.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.SortSpecifier;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SpinnerItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;

public class Main implements EntryPoint {

    private SpinnerItem changingRecordItem;

    @Override
    public void onModuleLoad() {

        final NumberFormat numberFormat = NumberFormat.getFormat("00");
        final DataSource dataSource = new DataSource();
        dataSource.setClientOnly(true);

        {// datasource fields initialization
            final DataSourceIntegerField keyField = new DataSourceIntegerField(
                    "id", "ID");
            keyField.setPrimaryKey(true);
            final DataSourceTextField nameField = new DataSourceTextField(
                    "name", "Name");

            dataSource.setFields(keyField, nameField);
        }

        final int maxId = 50;
        final int minId = 0;
        {
            final List<Record> records = new ArrayList<Record>();
            for (int i = minId; i < maxId; i++) {
                final Record record = new Record();
                record.setAttribute("id", i);
                record.setAttribute("name", "row " + numberFormat.format(i));
                records.add(record);
            }

            dataSource.setTestData(records.toArray(new Record[records.size()]));
        }

        // UI initialization
        final VLayout layout = new VLayout();
        layout.setMembersMargin(5);
        layout.setMargin(20);
        layout.setWidth(400);
        layout.setHeight(400);

        final ListGrid listGrid = new ListGrid();
        listGrid.setWidth100();
        listGrid.setHeight100();
        listGrid.setDataSource(dataSource);
        listGrid.setDataPageSize(25);
        listGrid.setDrawAheadRatio(1);
        listGrid.setDrawAllMaxCells(0);
        listGrid.setAutoFetchData(true);

        listGrid.setSort(new SortSpecifier[] { new SortSpecifier("name",
                SortDirection.ASCENDING) });

        layout.addMember(listGrid);

        final DynamicForm dynamicForm = new DynamicForm();
        changingRecordItem = new SpinnerItem();
        changingRecordItem
                .setTitle("Trigger cache update on name field for row");
        changingRecordItem.setValue(45);
        changingRecordItem.setMin(minId);
        changingRecordItem.setMax(maxId);
        dynamicForm.setFields(changingRecordItem);
        dynamicForm.setHeight(20);

        layout.addMember(dynamicForm);
        {
            final Button button = new Button("Scroll to row");
            button.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    listGrid.scrollToRow(getChangingRecordId());
                }
            });
            layout.addMember(button);
        }
        {
            final Button button = new Button("Update caches");
            button.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    final DSResponse dsResponse = new DSResponse();

                    Record record = new Record();
                    record.setAttribute("id", getChangingRecordId());
                    record.setAttribute(
                            "name",
                            "aaarow "
                                    + numberFormat
                                            .format(getChangingRecordId())
                                    + " with changed value!!!");
                    dsResponse.setData(new Record[] { record });
                    dsResponse.setOperationType(DSOperationType.UPDATE);
                    dataSource.updateCaches(dsResponse);

                }
            });
            layout.addMember(button);
        }

        final KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setShiftKey(true);
        debugKey.setKeyName("D");

        Page.registerKey(debugKey, new KeyCallback() {
            @Override
            public void execute(final String keyName) {
                SC.showConsole();
            }
        });

        layout.show();
    }

    protected Integer getChangingRecordId() {
        return (Integer) changingRecordItem.getValue();
    }

}
