 package com.yieldstar.rms.ui.client;

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

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.ListGridFieldType;
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;
import com.smartgwt.client.widgets.grid.ListGridField;
import java.util.ArrayList;
import java.util.List;

/**
 * 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("property");
        propertyTextItem.setTitle("Property");
        propertyTextItem.setHint("<nobr>Enter Property Code</nobr>");

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

        DateItem endDateItem = new DateItem("endDate");
        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();
                final List<ListGridField> fields = new ArrayList<ListGridField>();
                Criteria criteria = dynamicForm.getValuesAsCriteria();
                executedLeaseListGrid.fetchData(criteria, new DSCallback() {

                    @Override
                    public void execute(com.smartgwt.client.data.DSResponse response,
                            Object rawData, DSRequest request) {
                       Record[] record = response.getData();
                       if(record.length > 0){
                            for (String attribute : record[0].getAttributes()) {
                                Record field = record[0].getAttributeAsRecord(attribute);
                                //For now, Set type for all TEXT
                                fields.add(createField(attribute,
                                        "TEXT",
                                        attribute));

                            }
                        }
                        
                        executedLeaseListGrid.setFields(fields.toArray(new ListGridField[0]));
                        executedLeaseListGrid.markForRedraw();
                    }
                });
            }
        });
        dynamicForm.setFields(propertyTextItem, startDateItem, endDateItem, submitButtonItem);
        executedLeaseListGrid.setWidth(1200);
        executedLeaseListGrid.setHeight(464);
        executedLeaseListGrid.setDataSource(executedLeaseDS);
        executedLeaseListGrid.setUseAllDataSourceFields(true);
        executedLeaseListGrid.setLoadingDataMessage("Loading Report...");  //\${loadingImage}

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

        return main;

    }

    private ListGridField createField(String id, String type, String name) {
        ListGridField field = new ListGridField();
        field.setName(name);
       // field.setTitle(name);
       // field.setValueField(name);
       // field.setDisplayField(name);
        switch (FieldType.valueOf(type.toUpperCase())) {
            case TEXT:
               // field = new DataSourceTextField(id, name);
                field.setType(ListGridFieldType.TEXT);
                break;
            case DATETIME:
               // field = new DataSourceDateTimeField(id, name);
                field.setType(ListGridFieldType.DATE);
                break;
            case BOOLEAN:
               // field = new DataSourceBooleanField(id, name);
                field.setType(ListGridFieldType.BOOLEAN);
                break;
            case INTEGER:
               // field = new DataSourceIntegerField(id, name);
                field.setType(ListGridFieldType.INTEGER);
                break;
            case FLOAT:
               // field = new DataSourceFloatField(id, name);
                field.setType(ListGridFieldType.FLOAT);
                break;
            case DATE:
               // field = new DataSourceDateField(id, name);
                field.setType(ListGridFieldType.DATE);
                break;
            case LINK:
               // field = new DataSourceLinkField(id, name);
                field.setType(ListGridFieldType.LINK);
                break;
            case IMAGE:
               // field = new DataSourceImageField(id, name);
                field.setType(ListGridFieldType.IMAGE);
                break;
            default:
                field.setType(ListGridFieldType.TEXT);
        }
        return field;
    }
}
