package com.smartgwt.test.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.MiniDateRangeItem;
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.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SmartGwtTest implements EntryPoint {

    public void onModuleLoad() {
        VLayout main = new VLayout();
        main.setWidth100();
        main.setHeight100();
        main.addMember(getTableWithFilters());
        main.draw();
    }

    private ListGrid getTableWithFilters() {
        ListGrid listGrid = new ListGrid();
        listGrid.setWidth100();
        listGrid.setHeight100();
        listGrid.setShowFilterEditor(true);
        
        ListGridField dateField = new ListGridField("date", "Date", 200);
        dateField.setType(ListGridFieldType.DATE);
        MiniDateRangeItem di = new MiniDateRangeItem();
        dateField.setFilterEditorType(di);

        DataSource lookupDs = new LookupTestDataSource();
        
        ListGridField selectField = new ListGridField("key", "Key", 200);
        selectField.setValueField("key");
        selectField.setDisplayField("value");
        selectField.setOptionDataSource(lookupDs);

        ComboBoxItem lookupField = new ComboBoxItem();
        lookupField.setOptionDataSource(lookupDs);
        lookupField.setValueField("key");
        lookupField.setDisplayField("value");
        
        selectField.setEditorType(lookupField);
        selectField.setFilterEditorType(lookupField);
        
        listGrid.setFields(dateField, selectField);

        listGrid.setDataSource(new TestDataSource());
        listGrid.setAutoFetchData(true);

        return listGrid;
    }

    private class TestDataSource extends DataSource {

        private static final int NUM_ROWS = 100;

        TestDataSource() {
            setDataProtocol(DSProtocol.CLIENTCUSTOM);
            setDataFormat(DSDataFormat.CUSTOM);
            setClientOnly(false);
            addField(new DataSourceField("date", FieldType.DATE, "Date", 50, true));
            addField(new DataSourceField("key", FieldType.INTEGER, "Key", 50, true));
            //getField("select").setValueMap(VALUE_MAP);
        }

        @Override
        protected Object transformRequest(DSRequest request) {
            String requestId = request.getRequestId();
            DSResponse response = new DSResponse();
            response.setAttribute("clientContext", request.getAttributeAsObject("clientContext"));
            response.setStatus(0);
            if (request.getOperationType() == DSOperationType.FETCH) {
                response.setTotalRows(NUM_ROWS);
                ListGridRecord[] records = new ListGridRecord[NUM_ROWS];
                for (int i = 0; i < NUM_ROWS; i++) {
                    ListGridRecord record = new ListGridRecord();
                    record.setAttribute("date", new Date());
                    record.setAttribute("key", i%3);
                    records[i] = record;
                }
                response.setData(records);
            }
            processResponse(requestId, response);
            return request.getData();
        }
    }

    private class LookupTestDataSource extends DataSource {

        private static final int NUM_ROWS = 3;

        LookupTestDataSource() {
            setDataProtocol(DSProtocol.CLIENTCUSTOM);
            setDataFormat(DSDataFormat.CUSTOM);
            setClientOnly(false);
            addField(new DataSourceField("key", FieldType.INTEGER, "Key", 50, true));
            addField(new DataSourceField("value", FieldType.TEXT, "Value", 50, true));
        }

        @Override
        protected Object transformRequest(DSRequest request) {
            String requestId = request.getRequestId();
            DSResponse response = new DSResponse();
            response.setAttribute("clientContext", request.getAttributeAsObject("clientContext"));
            response.setStatus(0);
            if (request.getOperationType() == DSOperationType.FETCH) {
                response.setTotalRows(NUM_ROWS);
                ListGridRecord[] records = new ListGridRecord[NUM_ROWS];
                for (int i = 0; i < NUM_ROWS; i++) {
                    ListGridRecord record = new ListGridRecord();
                    record.setAttribute("key", i);
                    record.setAttribute("value", "Value " + i);
                    records[i] = record;
                }
                response.setData(records);
            }
            processResponse(requestId, response);
            return request.getData();
        }
    }
}
