package com.jetlab.plinning.console.client.view;

import com.google.gwt.i18n.client.NumberFormat;
import com.jetlab.plinning.console.client.data.WorldXmlDS;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Operator;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.OperatorValueType;
import com.smartgwt.client.types.TopOperatorAppearance;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.FieldOperatorCustomizer;
import com.smartgwt.client.widgets.grid.CellFormatter;
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.VStack;

public class SearchTest extends Canvas {
	
	/*
	 * Method to call to draw panel 
	 */
	public void showPanel() {

		DataSource worldDS = WorldXmlDS.getInstance();

		OperatorId[] operatorValid = new OperatorId[] { OperatorId.EQUALS, OperatorId.NOT_EQUAL, OperatorId.LESS_THAN, OperatorId.GREATER_THAN, OperatorId.LESS_OR_EQUAL, OperatorId.GREATER_OR_EQUAL, OperatorId.NOT_BLANK, OperatorId.IS_BLANK,OperatorId.IS_NULL,OperatorId.NOT_NULL};
		worldDS.setTypeOperators(FieldType.DATE, operatorValid);
		worldDS.setTypeOperators(FieldType.DATETIME, operatorValid);
		Operator newOperator=new Operator();
		newOperator.setTitle("mese corrente");
		newOperator.setID("mese_corrente");
		newOperator.setValueType(OperatorValueType.NONE);
		worldDS.addSearchOperator(newOperator, new FieldType[]{FieldType.DATE,FieldType.DATETIME});
		
		
		final FilterBuilder filterBuilder = new FilterBuilder();
		filterBuilder.setDataSource(worldDS);
		filterBuilder.setTopOperatorAppearance(TopOperatorAppearance.RADIO);
		filterBuilder.setFieldOperatorCustomizer(new FieldOperatorCustomizer() {
			@Override
			public OperatorId[] getFieldOperators(String fieldName, OperatorId[] defaultOperators, FilterBuilder filterBuilder) {
				DataSource ds = filterBuilder.getDataSource();
				DataSourceField field = ds.getField(fieldName);				

				if(field.getType().equals(FieldType.DATE) || field.getType().equals(FieldType.DATETIME)){					
					return defaultOperators;
				}
				
				field.setValidOperators(OperatorId.IS_NULL,OperatorId.NOT_NULL);
				
				if (field.getType().equals(FieldType.INTEGER) || field.getType().equals(FieldType.FLOAT) ) {
					return new OperatorId[] { OperatorId.EQUALS, OperatorId.NOT_EQUAL, OperatorId.LESS_THAN, OperatorId.GREATER_THAN, OperatorId.LESS_OR_EQUAL, OperatorId.GREATER_OR_EQUAL, OperatorId.NOT_BLANK, OperatorId.IS_BLANK,OperatorId.IS_NULL,OperatorId.NOT_NULL};
				}else if (field.getType().equals(FieldType.BOOLEAN)  ) {
					return new OperatorId[] { OperatorId.EQUALS, OperatorId.NOT_EQUAL, OperatorId.IS_BLANK, OperatorId.NOT_BLANK,OperatorId.IS_NULL,OperatorId.NOT_NULL };
				}  
				
				return getValidOperatorId();
				
			}
		});
		
		

		final ListGrid countryGrid = new ListGrid();
		countryGrid.setWidth(550);
		countryGrid.setHeight(224);
		countryGrid.setDataSource(worldDS);
		countryGrid.setAutoFetchData(true);

		ListGridField nameField = new ListGridField("countryName", "Country");
		ListGridField continentField = new ListGridField("continent", "Continent");
		ListGridField memberG8Field = new ListGridField("member_g8", "Member G8");
		memberG8Field.setCanEdit(false);

		ListGridField populationField = new ListGridField("population", "Population");
		populationField.setType(ListGridFieldType.INTEGER);
		populationField.setCellFormatter(new CellFormatter() {
			public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
				if (value == null)
					return null;
				try {
					NumberFormat nf = NumberFormat.getFormat("0,000");
					return nf.format(((Number) value).longValue());
				} catch (Exception e) {
					return value.toString();
				}
			}
		});
		ListGridField independenceField = new ListGridField("independence", "Independence");
		countryGrid.setFields(nameField, continentField, memberG8Field, populationField, independenceField);

		IButton filterButton = new IButton("Filter");
		filterButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				countryGrid.filterData(filterBuilder.getCriteria());
			}
		});

		VStack vStack = new VStack(10);
		vStack.addMember(filterBuilder);
		vStack.addMember(filterButton);
		vStack.addMember(countryGrid);

		vStack.draw();
		
		
		
		setWidth100();
		setHeight100();
		addChild(vStack);
		
		
	}
	
	private OperatorId[] getValidOperatorId(){
		return new OperatorId[] { OperatorId.ICONTAINS, OperatorId.ISTARTS_WITH, OperatorId.IENDS_WITH,
				OperatorId.IEQUALS, OperatorId.INOT_EQUAL,
				OperatorId.INOT_CONTAINS, OperatorId.INOT_STARTS_WITH, OperatorId.INOT_ENDS_WITH,
				OperatorId.EQUALS, OperatorId.NOT_EQUAL, OperatorId.LESS_THAN, OperatorId.GREATER_THAN, OperatorId.LESS_OR_EQUAL, OperatorId.GREATER_OR_EQUAL, OperatorId.NOT_BLANK, OperatorId.IS_BLANK};  //, OperatorId.IS_NULL,OperatorId.NOT_NULL

	}
}
