package com.test.client;

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.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.LogicalOperator;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.events.FilterChangedEvent;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class MyTestPage implements EntryPoint {

    public void onModuleLoad() {

    	FilterBuilderItem triggerFilter = new FilterBuilderItem();
        triggerFilter.setTitle("Trigger");

        DynamicForm form = new DynamicForm();
        form.setFields(triggerFilter);

        VLayout layout = new VLayout();
    	layout.addMember(form);
    	layout.draw();
    }

    public class FilterBuilderItem extends CanvasItem {

        public FilterBuilderItem() {

        	final FilterBuilder filter = new FilterBuilder();

            filter.setTopOperator(LogicalOperator.AND);
            filter.setShowSubClauseButton(true);
            filter.setAnimateMembers(false);
            filter.setValidateOnChange(false);

            FilterBuilderDataSource ds = new FilterBuilderDataSource();
            filter.setFieldDataSource(ds);
            filter.setSortFields(true);
//            filter.addFilterChangedHandler(new com.smartgwt.client.widgets.form.events.FilterChangedHandler() {
//				public void onFilterChanged(FilterChangedEvent event) {
//		        	Help.debug(">>> FilterBuilderItem.filter.onFilterChanged.filter.getCriteria = " + filter.getCriteria().toJSON());
//				}
//            });

            setCanvas(filter);
            setShowTitle(false);
        }
    }

    public abstract class GwtRpcDataSource extends DataSource {

    	public GwtRpcDataSource() {
    		setDataProtocol(DSProtocol.CLIENTCUSTOM);
    		setDataFormat(DSDataFormat.CUSTOM);
    		setClientOnly(false);
    	}

    	@Override
    	protected Object transformRequest(DSRequest request) {
    		String requestId = request.getRequestId();
    		DSResponse response = new DSResponse();
    		response.setAttribute("clientContext", request.getAttributeAsObject("clientContext"));
    		response.setStatus(0);
    		switch (request.getOperationType()) {
    		case FETCH:
    			executeFetch(requestId, request, response);
    			break;
    		case ADD:
    			executeAdd(requestId, request, response);
    			break;
    		case UPDATE:
    			executeUpdate(requestId, request, response);
    			break;
    		case REMOVE:
    			executeRemove(requestId, request, response);
    			break;
    		default:
    			break;
    		}
    		return request.getData();
    	}
    	protected abstract void executeFetch(String requestId, DSRequest request, DSResponse response);
    	protected abstract void executeAdd(String requestId, DSRequest request, DSResponse response);
    	protected abstract void executeUpdate(String requestId, DSRequest request, DSResponse response);
    	protected abstract void executeRemove(String requestId, DSRequest request, DSResponse response);
    }

    public class FilterBuilderDataSource extends GwtRpcDataSource {

        public static final String ID = "id";
        public static final String NAME = "name";
        public static final String TYPE = "type";
        public static final String TITLE = "title";
        public static final String VALID_OPERATORS = "validOperators";

        private final OperatorId[] stringOperators = { OperatorId.ICONTAINS };

        public FilterBuilderDataSource() {

    		DataSourceTextField id = new DataSourceTextField(ID);
    		id.setPrimaryKey(true);
            addField(id);
            DataSourceTextField name = new DataSourceTextField(NAME);
            addField(name);
            DataSourceTextField type = new DataSourceTextField(TYPE);
            addField(type);
            DataSourceTextField voFld = new DataSourceTextField(VALID_OPERATORS);
            addField(voFld);
        }

        @Override
        protected void executeFetch(final String requestId, DSRequest request, final DSResponse response) {

    		try {

				ListGridRecord[] records = new ListGridRecord[1];
				ListGridRecord record = new ListGridRecord();

				record.setAttribute(ID,  1);
				record.setAttribute(NAME, "FilterName");
				record.setAttribute(TITLE, "FilterName");
	            record.setAttribute(TYPE, FieldType.TEXT);
	            record.setAttribute(VALID_OPERATORS, stringOperators);

				records[0] = record;

				response.setData(records);
				response.setStartRow(0);
				response.setEndRow(0);
				response.setTotalRows(1);
				processResponse(requestId, response);

    		} catch (Exception e) {
    			SC.say("unexpectedException: " + e);
    			response.setStatus(RPCResponse.STATUS_FAILURE);
    			processResponse(requestId, response);
    		}
        }

        @Override
        protected void executeAdd(String requestId, DSRequest request, DSResponse response) {
    		response.setStatus(RPCResponse.STATUS_FAILURE);
    		processResponse(requestId, response);
        }

        @Override
        protected void executeUpdate(String requestId, DSRequest request, DSResponse response) {
    		response.setStatus(RPCResponse.STATUS_FAILURE);
    		processResponse(requestId, response);
        }

        @Override
        protected void executeRemove(String requestId, DSRequest request, DSResponse response) {
    		response.setStatus(RPCResponse.STATUS_FAILURE);
    		processResponse(requestId, response);
        }

    }
}
