import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceImageField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceLinkField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;

import com.smartgwt.client.widgets.form.fields.ButtonItem;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
/**
 *
 * @author dawit
 */
public class LeaseReport implements EntryPoint {

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        RootPanel.get("mainPanel").add(getViewPanel());
    }

    private Canvas getViewPanel() {
         SC.showConsole();
        VLayout main = new VLayout(10);
        final DataSource executedLeaseDS = DataSource.get("executedLeaseReport");
        executedLeaseDS.setDropExtraFields(false);
        final ListGrid executedLeaseListGrid = new ListGrid();
        final DynamicForm dynamicForm = new DynamicForm();
        dynamicForm.setWidth(450);
        dynamicForm.setHeight(150);
        dynamicForm.setIsGroup(true);
        dynamicForm.setGroupTitle("Property & Date Controls");

        TextItem propertyTextItem = new TextItem();
        propertyTextItem.setTitle("Property");
        propertyTextItem.setHint("<nobr>Enter Property Code</nobr>");

        DateItem startDateItem = new DateItem();
        startDateItem.setTitle("Start Date");
        startDateItem.setHint("<nobr>Select the Start Date</nobr>");

        DateItem endDateItem = new DateItem();
        endDateItem.setTitle("End Date");
        endDateItem.setHint("<nobr>Select End Date</nobr>");
        final ButtonItem submitButtonItem = new ButtonItem();
        submitButtonItem.setName("generate");
        submitButtonItem.setTitle("Generate");
        submitButtonItem.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                dynamicForm.validate();
                executedLeaseListGrid.fetchData(null, new DSCallback() {

                    @Override
                    public void execute(com.smartgwt.client.data.DSResponse response,
                            Object rawData, DSRequest request) {
                       // SC.say("attribute count :" + response.getAttributes().length);  // Depending on the length here we cand determine the size here ;
                        executedLeaseDS.addField(createField("test", "TEXT", "some test"));
                        for (Record record : response.getData()) {
                            for (String attribute : record.getAttributes()) {
                                Record field = record.getAttributeAsRecord(attribute);
//                               SC.logWarn("Attribute : " + attribute);
                                SC.logWarn("fieldx: " + field.toString());
                                executedLeaseDS.addField(createField(attribute,
                                        "TEXT",
                                        attribute));

                            }
                        }
                        executedLeaseListGrid.redraw();
                    }
                });
            }
        });
        dynamicForm.setFields(propertyTextItem, startDateItem, endDateItem, submitButtonItem);

        executedLeaseListGrid.setWidth(800);
        executedLeaseListGrid.setHeight(464);
        executedLeaseListGrid.setDataSource(executedLeaseDS);
        // executedLeaseListGrid.setAutoFetchData(true);

        VLayout layout = new VLayout(15);
        layout.addMember(dynamicForm);
        layout.addMember(executedLeaseListGrid);
        main.addMember(layout);
        main.draw();

        return main;

    }

    private DataSourceField createField(String id, String type, String name) {
        DataSourceField field = null;
        switch (FieldType.valueOf(type.toUpperCase())) {
            case TEXT:
                field = new DataSourceTextField(id, name);
                SC.logWarn("Text being used");
                break;
            case DATETIME:
                field = new DataSourceDateTimeField(id, name);
                break;
            case BOOLEAN:
                field = new DataSourceBooleanField(id, name);
                break;
            case INTEGER:
                field = new DataSourceIntegerField(id, name);
                break;
            case FLOAT:
                field = new DataSourceFloatField(id, name);
                break;
            case DATE:
                field = new DataSourceDateField(id, name);
                break;
            case LINK:
                field = new DataSourceLinkField(id, name);
                break;
            case IMAGE:
                field = new DataSourceImageField(id, name);
                break;
        }
        return field;
    }
}